diff --git a/include/villas/nodes/file.h b/include/villas/nodes/file.h index 8977eaf21..624a73ee5 100644 --- a/include/villas/nodes/file.h +++ b/include/villas/nodes/file.h @@ -33,7 +33,7 @@ #include "io.h" #include "node.h" -#include "periodic_task.h" +#include "task.h" #define FILE_MAX_PATHLEN 512 @@ -46,7 +46,7 @@ struct file { char *mode; /**< File access mode. */ int flush; /**< Flush / upload file contents after each write. */ - struct periodic_task timer; /**< Timer file descriptor. Blocks until 1 / rate seconds are elapsed. */ + struct task task; /**< Timer file descriptor. Blocks until 1 / rate seconds are elapsed. */ double rate; /**< The read rate. */ enum epoch_mode { diff --git a/include/villas/nodes/ngsi.h b/include/villas/nodes/ngsi.h index b48547489..a64ac459a 100644 --- a/include/villas/nodes/ngsi.h +++ b/include/villas/nodes/ngsi.h @@ -41,7 +41,7 @@ #include "list.h" #include "super_node.h" #include "node.h" -#include "periodic_task.h" +#include "task.h" struct node; @@ -54,7 +54,7 @@ struct ngsi { double timeout; /**< HTTP timeout in seconds */ double rate; /**< Rate used for polling. */ - struct periodic_task timer; /**< Timer for periodic events. */ + struct task task; /**< Timer for periodic events. */ int ssl_verify; /**< Boolean flag whether SSL server certificates should be verified or not. */ struct curl_slist *headers; /**< List of HTTP request headers for libcurl */ diff --git a/include/villas/nodes/signal.h b/include/villas/nodes/signal.h index 78290d379..3d0e07521 100644 --- a/include/villas/nodes/signal.h +++ b/include/villas/nodes/signal.h @@ -30,7 +30,7 @@ #pragma once #include "timing.h" -#include "periodic_task.h" +#include "task.h" /* Forward declarations */ struct node; @@ -49,7 +49,7 @@ enum signal_type { * @see node_type */ struct signal { - struct periodic_task timer; /**< Timer for periodic events. */ + struct task task; /**< Timer for periodic events. */ int rt; /**< Real-time mode? */ enum signal_type type; /**< Signal type */ diff --git a/include/villas/nodes/test_rtt.h b/include/villas/nodes/test_rtt.h index d8cb5d7a6..8c38a1cbe 100644 --- a/include/villas/nodes/test_rtt.h +++ b/include/villas/nodes/test_rtt.h @@ -32,9 +32,10 @@ #include "node.h" #include "list.h" #include "io.h" +#include "task.h" struct test_rtt_case { - int tfd; + struct task task; double rate; int values; int counter; diff --git a/include/villas/periodic_task.h b/include/villas/task.h similarity index 72% rename from include/villas/periodic_task.h rename to include/villas/task.h index 4239e1616..518ae46ba 100644 --- a/include/villas/periodic_task.h +++ b/include/villas/task.h @@ -28,7 +28,7 @@ #include -/** We can choose between two periodic timer implementations */ +/** We can choose between two periodic task implementations */ //#define PERIODIC_TASK_IMPL NANOSLEEP #define TIMERFD 1 #define CLOCK_NANOSLEEP 2 @@ -37,10 +37,10 @@ #if defined(__MACH__) #define PERIODIC_TASK_IMPL NANOSLEEP #else - #define PERIODIC_TASK_IMPL CLOCK_NANOSLEEP + #define PERIODIC_TASK_IMPL TIMERFD #endif -struct periodic_task { +struct task { struct timespec period; #if PERIODIC_TASK_IMPL == CLOCK_NANOSLEEP || PERIODIC_TASK_IMPL == NANOSLEEP struct timespec next_period; @@ -49,22 +49,23 @@ struct periodic_task { #else #error "Invalid period task implementation" #endif + int clock; }; -/** Create a new timer with the given rate. */ -int periodic_task_init(struct periodic_task *t, double rate); +/** Create a new task with the given rate. */ +int task_init(struct task *t, double rate, int clock); -int periodic_task_destroy(struct periodic_task *t); +int task_destroy(struct task *t); -/** Wait until timer elapsed +/** Wait until task elapsed * - * @retval 0 An error occured. Maybe the timer was stopped. - * @retval >0 The nummer of runs this timer already fired. + * @retval 0 An error occured. Maybe the task was stopped. + * @retval >0 The nummer of runs this task already fired. */ -uint64_t periodic_task_wait_until_next_period(struct periodic_task *t); +uint64_t task_wait_until_next_period(struct task *t); /** Wait until a fixed time in the future is reached * * @param until A pointer to a time in the future. */ -int periodic_task_wait_until(struct periodic_task *t, const struct timespec *until); \ No newline at end of file +int task_wait_until(struct task *t, const struct timespec *until); \ No newline at end of file diff --git a/lib/nodes/file.c b/lib/nodes/file.c index d81b922a4..061f546d4 100644 --- a/lib/nodes/file.c +++ b/lib/nodes/file.c @@ -218,7 +218,7 @@ int file_start(struct node *n) return ret; /* Create timer */ - ret = periodic_task_init(&f->timer, f->rate); + ret = task_init(&f->task, f->rate, CLOCK_REALTIME); if (ret) serror("Failed to create timer"); @@ -226,9 +226,8 @@ int file_start(struct node *n) if (f->epoch_mode != FILE_EPOCH_ORIGINAL) { io_rewind(&f->io); - struct sample s; + struct sample s = { .capacity = 0 }; struct sample *smps[] = { &s }; - s.capacity = 0; ret = io_scan(&f->io, smps, 1); if (ret != 1) @@ -248,7 +247,7 @@ int file_stop(struct node *n) struct file *f = n->_vd; int ret; - periodic_task_destroy(&f->timer); + task_destroy(&f->task); ret = io_close(&f->io); if (ret) @@ -310,14 +309,14 @@ retry: ret = io_scan(&f->io, smps, cnt); return cnt; if (f->rate) { - steps = periodic_task_wait_until_next_period(&f->timer); + steps = task_wait_until_next_period(&f->task); smps[0]->ts.origin = time_now(); } else { smps[0]->ts.origin = time_add(&smps[0]->ts.origin, &f->offset); - steps = periodic_task_wait_until(&f->timer, &smps[0]->ts.origin); + steps = task_wait_until(&f->task, &smps[0]->ts.origin); } /* Check for overruns */ diff --git a/lib/nodes/ngsi.c b/lib/nodes/ngsi.c index e87eb8ac9..0c2ba042d 100644 --- a/lib/nodes/ngsi.c +++ b/lib/nodes/ngsi.c @@ -488,13 +488,13 @@ int ngsi_start(struct node *n) i->headers = curl_slist_append(i->headers, buf); } - /* Create timer */ + /* Create task */ if (i->timeout > 1 / i->rate) warn("Timeout is to large for given rate: %f", i->rate); - ret = periodic_task_init(&i->timer, i->rate); + ret = task_init(&i->task, i->rate, CLOCK_MONOTONIC); if (ret) - serror("Failed to create timer"); + serror("Failed to create task"); i->headers = curl_slist_append(i->headers, "Accept: application/json"); i->headers = curl_slist_append(i->headers, "Content-Type: application/json"); @@ -539,8 +539,8 @@ int ngsi_read(struct node *n, struct sample *smps[], unsigned cnt) struct ngsi *i = n->_vd; int ret; - if (periodic_task_wait_until_next_period(&i->timer) == 0) - perror("Failed to wait for timer"); + if (task_wait_until_next_period(&i->task) == 0) + perror("Failed to wait for task"); json_t *rentity; json_t *entity = ngsi_build_entity(i, NULL, 0, 0); diff --git a/lib/nodes/signal.c b/lib/nodes/signal.c index 61239cca6..cba5dff57 100644 --- a/lib/nodes/signal.c +++ b/lib/nodes/signal.c @@ -163,9 +163,9 @@ int signal_open(struct node *n) s->counter = 0; s->started = time_now(); - /* Setup timer */ + /* Setup task */ if (s->rt) { - ret = periodic_task_init(&s->timer, s->rate); + ret = task_init(&s->task, s->rate, CLOCK_MONOTONIC); if (ret) return ret; } @@ -179,7 +179,7 @@ int signal_close(struct node *n) struct signal* s = n->_vd; if (s->rt) { - ret = periodic_task_destroy(&s->timer); + ret = task_destroy(&s->task); if (ret) return ret; } @@ -200,7 +200,7 @@ int signal_read(struct node *n, struct sample *smps[], unsigned cnt) /* Throttle output if desired */ if (s->rt) { /* Block until 1/p->rate seconds elapsed */ - steps = periodic_task_wait_until_next_period(&s->timer); + steps = task_wait_until_next_period(&s->task); if (steps > 1) warn("Missed steps: %u", steps); diff --git a/lib/nodes/test_rtt.c b/lib/nodes/test_rtt.c index 3a4a6e8e3..31e01769d 100644 --- a/lib/nodes/test_rtt.c +++ b/lib/nodes/test_rtt.c @@ -38,8 +38,8 @@ static int test_rtt_case_start(struct test_rtt *t) return ret; /* Start timer. */ - c->tfd = timerfd_create_rate(c->rate); - if (c->tfd < 0) + ret = task_init(&t->timer, c->rate); + if (ret) serror("Failed to create timer"); return 0; @@ -51,7 +51,7 @@ static int test_rtt_case_stop(struct test_rtt_case *c) io_close(&t->io); /* Stop timer. */ - close(c->tfd); + task_destroy(&c->task); return 0; } @@ -189,10 +189,7 @@ int test_rtt_read(struct node *n, struct sample *smps[], unsigned cnt) struct test_rtt_case *c = t->current; /* Wait */ - ret = read(c->tfd, &steps, sizeof(steps)); - if (ret != sizeof(steps)) - return -1; - + steps = task_wait_until_next_period(&c->task); if (steps > 1) warn("Skipped %zu samples", steps - 1);