From f9324000fa761ae63d418a2274ac97688b9290f9 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Wed, 30 Aug 2017 00:22:58 +0200 Subject: [PATCH] add new functions to retrieve file descriptors from nodes, io formats and tasks --- include/villas/io.h | 3 +++ include/villas/io_format.h | 3 +++ include/villas/node.h | 2 ++ include/villas/node_type.h | 3 +++ include/villas/task.h | 4 +++- lib/io.c | 20 ++++++++++++++++++++ lib/node.c | 5 +++++ lib/task.c | 10 +++++++++- 8 files changed, 48 insertions(+), 2 deletions(-) diff --git a/include/villas/io.h b/include/villas/io.h index 969daca15..c22976bbf 100644 --- a/include/villas/io.h +++ b/include/villas/io.h @@ -86,6 +86,7 @@ void io_rewind(struct io *io); int io_flush(struct io *io); +int io_fd(struct io *io); int io_stream_open(struct io *io, const char *uri); @@ -95,4 +96,6 @@ int io_stream_eof(struct io *io); void io_stream_rewind(struct io *io); +int io_stream_fd(struct io *io); + int io_stream_flush(struct io *io); diff --git a/include/villas/io_format.h b/include/villas/io_format.h index d82bd5c17..7bca9954f 100644 --- a/include/villas/io_format.h +++ b/include/villas/io_format.h @@ -70,6 +70,9 @@ struct io_format { * @see rewind() */ void (*rewind)(struct io *io); + + /** Get a file descriptor which can be used with select / poll */ + int (*fd)(struct io *io); /** Flush buffered data to disk. * diff --git a/include/villas/node.h b/include/villas/node.h index ed49c6098..0eeeb7cbc 100644 --- a/include/villas/node.h +++ b/include/villas/node.h @@ -125,6 +125,8 @@ int node_read(struct node *n, struct sample *smps[], unsigned cnt); int node_write(struct node *n, struct sample *smps[], unsigned cnt); +int node_fd(struct node *n); + /** Parse an array or single node and checks if they exist in the "nodes" section. * * Examples: diff --git a/include/villas/node_type.h b/include/villas/node_type.h index 3b00957a3..09330eb4a 100644 --- a/include/villas/node_type.h +++ b/include/villas/node_type.h @@ -146,6 +146,9 @@ struct node_type { * @param n A pointer to the node object. */ int (*reverse)(struct node *n); + + /** Return a file descriptor which can be used by poll / select to detect the availability of new data. */ + int (*fd)(struct node *n); }; /** Initialize all registered node type subsystems. diff --git a/include/villas/task.h b/include/villas/task.h index 518ae46ba..71d1b3a1c 100644 --- a/include/villas/task.h +++ b/include/villas/task.h @@ -68,4 +68,6 @@ uint64_t task_wait_until_next_period(struct task *t); * * @param until A pointer to a time in the future. */ -int task_wait_until(struct task *t, const struct timespec *until); \ No newline at end of file +int task_wait_until(struct task *t, const struct timespec *until); + +int task_fd(struct task *t); \ No newline at end of file diff --git a/lib/io.c b/lib/io.c index 6abf70bff..4dedc1873 100644 --- a/lib/io.c +++ b/lib/io.c @@ -177,6 +177,19 @@ void io_stream_rewind(struct io *io) } } +int io_stream_fd(struct io *io) +{ + switch (io->mode) { + case IO_MODE_ADVIO: + return afileno(io->advio.input); + case IO_MODE_STDIO: + return fileno(io->stdio.input); + case IO_MODE_CUSTOM: + return -1; + } +} + + int io_open(struct io *io, const char *uri) { return io->_vt->open @@ -212,6 +225,13 @@ void io_rewind(struct io *io) : io_stream_rewind(io); } +int io_fd(struct io *io) +{ + return io->_vt->fd + ? io->_vt->fd(io) + : io_stream_fd(io); +} + int io_print(struct io *io, struct sample *smps[], unsigned cnt) { int ret; diff --git a/lib/node.c b/lib/node.c index a7bcf971d..a2be6ab0f 100644 --- a/lib/node.c +++ b/lib/node.c @@ -288,6 +288,11 @@ int node_reverse(struct node *n) return n->_vt->reverse ? n->_vt->reverse(n) : -1; } +int node_fd(struct node *n) +{ + return n->_vt->fd ? n->_vt->fd(n) : -1; +} + int node_parse_list(struct list *list, json_t *cfg, struct list *all) { struct node *node; diff --git a/lib/task.c b/lib/task.c index d6e46ee16..bf2dcf20b 100644 --- a/lib/task.c +++ b/lib/task.c @@ -115,7 +115,6 @@ uint64_t task_wait_until_next_period(struct task *t) return runs; } - int task_wait_until(struct task *t, const struct timespec *until) { int ret; @@ -154,4 +153,13 @@ retry: ret = clock_nanosleep(t->clock, TIMER_ABSTIME, until, NULL); #endif return 0; +} + +int task_fd(struct task *t) +{ +#if PERIODIC_TASK_IMPL == TIMERFD + return t->fd; +#else + return -1; +#endif } \ No newline at end of file