From 44395654d8691c800abf0421f4b8270ace288529 Mon Sep 17 00:00:00 2001 From: Simon Lieb Date: Tue, 25 Oct 2016 20:54:16 +0200 Subject: [PATCH] Fix signal bursts blocking the counter As sleep() would only detect a minimum of 1 second of sleeping, using nanosleep() solves this problem. But we now required to define POSIX_C_SOURCE >= 199309 --- config.mk | 2 +- spt.c | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/config.mk b/config.mk index 0c96010..0371286 100644 --- a/config.mk +++ b/config.mk @@ -16,7 +16,7 @@ INCS+= `pkg-config --cflags libnotify` LIBS+= `pkg-config --libs libnotify` # flags -CPPFLAGS = -DVERSION=\"${VERSION}\" -D_POSIX_C_SOURCE +CPPFLAGS = -DVERSION=\"${VERSION}\" -D_POSIX_C_SOURCE=199309 CFLAGS += -g -std=c99 -pedantic -Wall -Os ${INCS} ${DEFS} ${CPPFLAGS} LDFLAGS += -g ${LIBS} diff --git a/spt.c b/spt.c index dfe2728..15b83f1 100644 --- a/spt.c +++ b/spt.c @@ -5,6 +5,7 @@ #include #include #include +#include #ifdef NOTIFY #include #endif /* NOTIFY */ @@ -112,9 +113,10 @@ usage(void) int main(int argc, char *argv[]) { + struct timespec remaining; struct sigaction sa; sigset_t emptymask; - int i, timecount; + int i; ARGBEGIN { case 'e': @@ -149,17 +151,17 @@ main(int argc, char *argv[]) for (i = 0; ; i = (i + 1) % LEN(timers)) { notify_send(timers[i].cmt); - timecount = 0; - while (timecount < timers[i].tmr) { + remaining.tv_sec = timers[i].tmr; + remaining.tv_nsec = 0; + while (remaining.tv_sec) { if (display) - display_time(timecount); + display_time(remaining.tv_sec); if (suspend) sigsuspend(&emptymask); - else { - sleep(1); - timecount++; - } + else + if (nanosleep(&remaining, &remaining) == 0) + remaining.tv_sec = remaining.tv_nsec = 0; } }