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`
# 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}

18
spt.c
View File

@ -5,6 +5,7 @@
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#ifdef NOTIFY
#include <libnotify/notify.h>
#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;
}
}