1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-30 00:00:11 +01:00

task: add task_set_rate()

This commit is contained in:
Steffen Vogel 2017-09-05 16:41:05 +02:00
parent edd0c63d13
commit e7cbdf694e
2 changed files with 31 additions and 18 deletions

View file

@ -64,10 +64,12 @@ int task_destroy(struct task *t);
*/ */
uint64_t task_wait_until_next_period(struct task *t); uint64_t task_wait_until_next_period(struct task *t);
int task_set_rate(struct task *t, double rate);
/** Wait until a fixed time in the future is reached /** Wait until a fixed time in the future is reached
* *
* @param until A pointer to a time in the future. * @param until A pointer to a time in the future.
*/ */
int task_wait_until(struct task *t, const struct timespec *until); int task_wait_until(struct task *t, const struct timespec *until);
int task_fd(struct task *t); int task_fd(struct task *t);

View file

@ -35,32 +35,43 @@
int task_init(struct task *t, double rate, int clock) int task_init(struct task *t, double rate, int clock)
{ {
int ret;
t->clock = clock; t->clock = clock;
#if PERIODIC_TASK_IMPL == TIMERFD
t->fd = timerfd_create(t->clock, 0);
if (t->fd < 0)
return -1;
#endif
ret = task_set_rate(t, rate);
if (ret)
return ret;
return 0;
}
int task_set_rate(struct task *t, double rate)
{
t->period = rate ? time_from_double(1.0 / rate) : (struct timespec) { 0, 0 }; t->period = rate ? time_from_double(1.0 / rate) : (struct timespec) { 0, 0 };
#if PERIODIC_TASK_IMPL == CLOCK_NANOSLEEP || PERIODIC_TASK_IMPL == NANOSLEEP #if PERIODIC_TASK_IMPL == CLOCK_NANOSLEEP || PERIODIC_TASK_IMPL == NANOSLEEP
struct timespec now; struct timespec now;
clock_gettime(t->clock, &now); clock_gettime(t->clock, &now);
t->next_period = time_add(&now, &t->period); t->next_period = time_add(&now, &t->period);
#elif PERIODIC_TASK_IMPL == TIMERFD #elif PERIODIC_TASK_IMPL == TIMERFD
int ret; int ret;
struct itimerspec its = { struct itimerspec its = {
.it_interval = t->period, .it_interval = t->period,
.it_value = t->period .it_value = t->period
}; };
t->fd = timerfd_create(t->clock, 0);
if (t->fd < 0)
return -1;
ret = timerfd_settime(t->fd, 0, &its, NULL); ret = timerfd_settime(t->fd, 0, &its, NULL);
if (ret) if (ret)
return ret; return ret;
#else
#error "Invalid period task implementation"
#endif #endif
return 0; return 0;
@ -71,7 +82,7 @@ int task_destroy(struct task *t)
#if PERIODIC_TASK_IMPL == TIMERFD #if PERIODIC_TASK_IMPL == TIMERFD
return close(t->fd); return close(t->fd);
#endif #endif
return 0; return 0;
} }
@ -82,7 +93,7 @@ static int time_lt(const struct timespec *lhs, const struct timespec *rhs)
return lhs->tv_nsec < rhs->tv_nsec; return lhs->tv_nsec < rhs->tv_nsec;
else else
return lhs->tv_sec < rhs->tv_sec; return lhs->tv_sec < rhs->tv_sec;
return 0; return 0;
} }
#endif #endif
@ -94,13 +105,13 @@ uint64_t task_wait_until_next_period(struct task *t)
#if PERIODIC_TASK_IMPL == CLOCK_NANOSLEEP || PERIODIC_TASK_IMPL == NANOSLEEP #if PERIODIC_TASK_IMPL == CLOCK_NANOSLEEP || PERIODIC_TASK_IMPL == NANOSLEEP
ret = task_wait_until(t, &t->next_period); ret = task_wait_until(t, &t->next_period);
struct timespec now; struct timespec now;
ret = clock_gettime(t->clock, &now); ret = clock_gettime(t->clock, &now);
if (ret) if (ret)
return 0; return 0;
for (runs = 0; time_lt(&t->next_period, &now); runs++) for (runs = 0; time_lt(&t->next_period, &now); runs++)
t->next_period = time_add(&t->next_period, &t->period); t->next_period = time_add(&t->next_period, &t->period);
@ -111,7 +122,7 @@ uint64_t task_wait_until_next_period(struct task *t)
#else #else
#error "Invalid period task implementation" #error "Invalid period task implementation"
#endif #endif
return runs; return runs;
} }
@ -135,7 +146,7 @@ retry: ret = clock_nanosleep(t->clock, TIMER_ABSTIME, until, NULL);
ret = nanosleep(&delta, NULL); ret = nanosleep(&delta, NULL);
#elif PERIODIC_TASK_IMPL == TIMERFD #elif PERIODIC_TASK_IMPL == TIMERFD
uint64_t runs; uint64_t runs;
struct itimerspec its = { struct itimerspec its = {
.it_value = *until, .it_value = *until,
.it_interval = { 0, 0 } .it_interval = { 0, 0 }
@ -151,7 +162,7 @@ retry: ret = clock_nanosleep(t->clock, TIMER_ABSTIME, until, NULL);
#else #else
#error "Invalid period task implementation" #error "Invalid period task implementation"
#endif #endif
return 0; return 0;
} }
@ -162,4 +173,4 @@ int task_fd(struct task *t)
#else #else
return -1; return -1;
#endif #endif
} }