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:
parent
edd0c63d13
commit
e7cbdf694e
2 changed files with 31 additions and 18 deletions
|
@ -64,6 +64,8 @@ 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.
|
||||||
|
|
25
lib/task.c
25
lib/task.c
|
@ -35,7 +35,25 @@
|
||||||
|
|
||||||
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
|
||||||
|
@ -46,21 +64,14 @@ int task_init(struct task *t, double rate, int clock)
|
||||||
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;
|
||||||
|
|
Loading…
Add table
Reference in a new issue