mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
io: support non blocking streams
This commit is contained in:
parent
0ba5034954
commit
212021404c
2 changed files with 23 additions and 1 deletions
|
@ -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 {
|
||||
|
|
21
lib/io.c
21
lib/io.c
|
@ -23,6 +23,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#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)
|
||||
|
|
Loading…
Add table
Reference in a new issue