Skip to content

Commit

Permalink
Fix #106: Patch timer nsec handling
Browse files Browse the repository at this point in the history
When using timers, a small window exists where they can end up in a non
running state.  This is caused by how the nsec value in struct timespec
is handled when restarting the timers.

When a timer is less than a second from expiring it is not restarted
correctly, resulting in timers that never expire.

Signed-off-by: Jonas Holmberg <jonas.holmberg@westermo.se>
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
  • Loading branch information
Jonas Holmberg authored and troglobit committed Jan 9, 2019
1 parent fafab8f commit 417c65c
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ static void set(struct timer *t, struct timespec *now)

static int expired(struct timer *t, struct timespec *now)
{
long round_nsec = now->tv_nsec + 250000000;
round_nsec = round_nsec > 999999999 ? 999999999 : round_nsec;

if (t->timeout.tv_sec < now->tv_sec)
return 1;

if (t->timeout.tv_sec == now->tv_sec && t->timeout.tv_nsec <= now->tv_nsec)
if (t->timeout.tv_sec == now->tv_sec && t->timeout.tv_nsec <= round_nsec)
return 1;

return 0;
Expand Down Expand Up @@ -108,7 +111,7 @@ static int start(struct timespec *now)

memset(&it, 0, sizeof(it));
it.it_value.tv_sec = next->timeout.tv_sec - now->tv_sec;
it.it_value.tv_nsec = 0;
it.it_value.tv_nsec = next->timeout.tv_nsec - now->tv_nsec;
timer_settime(timer, 0, &it, NULL);

return 0;
Expand Down

0 comments on commit 417c65c

Please sign in to comment.