diff --git a/include/villas/io.h b/include/villas/io.h index c22976bbf..6e3ac98e3 100644 --- a/include/villas/io.h +++ b/include/villas/io.h @@ -31,7 +31,8 @@ struct sample; struct io_format; enum io_flags { - IO_FLUSH = (1 << 8) /**< Flush the output stream after each chunk of samples. */ + IO_FLUSH = (1 << 8), /**< Flush the output stream after each chunk of samples. */ + IO_NONBLOCK = (1 << 9) /**< Dont block io_read() while waiting for new samples. */ }; struct io { diff --git a/lib/io.c b/lib/io.c index 28fbb0adb..0a4409a6b 100644 --- a/lib/io.c +++ b/lib/io.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "io.h" #include "io_format.h" @@ -91,6 +92,26 @@ stdio: io->mode = IO_MODE_STDIO; io->stdio.output = stdout; } + /* Make stream non-blocking if desired */ + if (io->flags & IO_NONBLOCK) { + int ret, fd, flags; + + fd = io_fd(io); + if (fd < 0) + return fd; + + flags = fcntl(fd, F_GETFL); + if (flags < 0) + return flags; + + flags |= O_NONBLOCK; + + ret = fcntl(fd, F_SETFL, flags); + if (ret) + return ret; + } + + /* Enable line buffering on stdio */ if (io->mode == IO_MODE_STDIO) { ret = setvbuf(io->stdio.input, NULL, _IOLBF, BUFSIZ); if (ret)