diff --git a/lib/task.c b/lib/task.c index 4617391dc..482c94687 100644 --- a/lib/task.c +++ b/lib/task.c @@ -135,18 +135,6 @@ int task_destroy(struct task *t) return 0; } -#if PERIODIC_TASK_IMPL == CLOCK_NANOSLEEP || PERIODIC_TASK_IMPL == NANOSLEEP -static int time_lt(const struct timespec *lhs, const struct timespec *rhs) -{ - if (lhs->tv_sec == rhs->tv_sec) - return lhs->tv_nsec < rhs->tv_nsec; - else - return lhs->tv_sec < rhs->tv_sec; - - return 0; -} -#endif - uint64_t task_wait(struct task *t) { uint64_t runs; @@ -155,6 +143,13 @@ uint64_t task_wait(struct task *t) int ret; struct timespec now; + ret = clock_gettime(t->clock, &now); + if (ret) + return ret; + + for (runs = 0; time_cmp(&t->next, &now) < 0; runs++) + t->next = time_add(&t->next, &t->period); + #if PERIODIC_TASK_IMPL == CLOCK_NANOSLEEP do { ret = clock_nanosleep(t->clock, TIMER_ABSTIME, &t->next, NULL); @@ -162,23 +157,11 @@ uint64_t task_wait(struct task *t) #elif PERIODIC_TASK_IMPL == NANOSLEEP struct timespec delta; - ret = clock_gettime(t->clock, &now); - if (ret) - return ret; - delta = time_diff(&now, &t->next); - ret = nanosleep(&delta, NULL); #endif if (ret < 0) return 0; - - ret = clock_gettime(t->clock, &now); - if (ret) - return 0; - - for (runs = 0; time_lt(&t->next, &now); runs++) - t->next = time_add(&t->next, &t->period); #elif PERIODIC_TASK_IMPL == TIMERFD int ret;