From 770deae6cd0f25d7fa7f5e74f730f15cd6c0c323 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Tue, 19 Jul 2022 21:45:36 +0200 Subject: [PATCH] Force blocking stdout Signed-off-by: David Oberhollenzer --- shadermeh.c | 40 ++++++++++++++++++++++++++++++++++++++++ shadermeh.h | 1 + 2 files changed, 41 insertions(+) diff --git a/shadermeh.c b/shadermeh.c index 6249922..c2be88c 100644 --- a/shadermeh.c +++ b/shadermeh.c @@ -48,6 +48,39 @@ static void convert_for_ffmpeg(const uint8_t *in, uint8_t *out, } } +static int wait_fd_event(int fd, int events) +{ + struct pollfd pfd; + int ret; + + for (;;) { + pfd.fd = fd; + pfd.events = events; + pfd.revents = 0; + + ret = poll(&pfd, 1, -1); + + if (ret > 0) { + if (pfd.revents & events) + break; + + if (pfd.revents & (POLLERR | POLLHUP)) { + fputs("poll reported error\n", stderr); + return -1; + } + } + + if (ret < 0) { + if (errno == EINTR) + continue; + perror("poll"); + return -1; + } + } + + return 0; +} + static int write_retry(int fd, const void *buffer, size_t size) { while (size > 0) { @@ -56,6 +89,13 @@ static int write_retry(int fd, const void *buffer, size_t size) if (ret < 0) { if (errno == EINTR) continue; + + if (errno == EAGAIN) { + if (wait_fd_event(fd, POLLOUT)) + return -1; + continue; + } + perror("write"); return -1; } diff --git a/shadermeh.h b/shadermeh.h index aa5c5cd..df43001 100644 --- a/shadermeh.h +++ b/shadermeh.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #define AUDIO_SAMPLES (512)