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
This commit is contained in:
Simon Lieb 2016-10-25 20:54:16 +02:00 committed by Ivan Tham
parent b5d373cbb4
commit 44395654d8
2 changed files with 11 additions and 9 deletions

View File

@ -16,7 +16,7 @@ INCS+= `pkg-config --cflags libnotify`
LIBS+= `pkg-config --libs libnotify` LIBS+= `pkg-config --libs libnotify`
# flags # 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} CFLAGS += -g -std=c99 -pedantic -Wall -Os ${INCS} ${DEFS} ${CPPFLAGS}
LDFLAGS += -g ${LIBS} LDFLAGS += -g ${LIBS}

18
spt.c
View File

@ -5,6 +5,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <time.h>
#ifdef NOTIFY #ifdef NOTIFY
#include <libnotify/notify.h> #include <libnotify/notify.h>
#endif /* NOTIFY */ #endif /* NOTIFY */
@ -112,9 +113,10 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct timespec remaining;
struct sigaction sa; struct sigaction sa;
sigset_t emptymask; sigset_t emptymask;
int i, timecount; int i;
ARGBEGIN { ARGBEGIN {
case 'e': case 'e':
@ -149,17 +151,17 @@ main(int argc, char *argv[])
for (i = 0; ; i = (i + 1) % LEN(timers)) { for (i = 0; ; i = (i + 1) % LEN(timers)) {
notify_send(timers[i].cmt); notify_send(timers[i].cmt);
timecount = 0; remaining.tv_sec = timers[i].tmr;
while (timecount < timers[i].tmr) { remaining.tv_nsec = 0;
while (remaining.tv_sec) {
if (display) if (display)
display_time(timecount); display_time(remaining.tv_sec);
if (suspend) if (suspend)
sigsuspend(&emptymask); sigsuspend(&emptymask);
else { else
sleep(1); if (nanosleep(&remaining, &remaining) == 0)
timecount++; remaining.tv_sec = remaining.tv_nsec = 0;
}
} }
} }