diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2016-10-24 02:15:36 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2016-10-24 02:15:36 +0000 |
commit | 9ef3a9f8b2704693496af12120ea3ab40389bf7b (patch) | |
tree | 9ed582b049881f1271a6b02978f1ed6d28a4f948 /src/libstddjb/alarm_deadline.c | |
parent | 422d91b2b0a2b8b3a8af510cc55b1400c60be303 (diff) | |
download | skalibs-9ef3a9f8b2704693496af12120ea3ab40389bf7b.tar.xz |
Add the alarm library, first draft.
Diffstat (limited to 'src/libstddjb/alarm_deadline.c')
-rw-r--r-- | src/libstddjb/alarm_deadline.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/libstddjb/alarm_deadline.c b/src/libstddjb/alarm_deadline.c new file mode 100644 index 0000000..768ee5c --- /dev/null +++ b/src/libstddjb/alarm_deadline.c @@ -0,0 +1,47 @@ +/* ISC license. */ + +#include <skalibs/sysdeps.h> +#include <skalibs/tai.h> +#include <skalibs/alarm.h> + +#ifdef SKALIBS_HASTIMER + +#include <errno.h> +#include <signal.h> +#include <time.h> +#include "alarm-internal.h" + +#undef MYCLOCK +#ifdef SKALIBS_HASCLOCKMON +# define MYCLOCK CLOCK_MONOTONIC +#else +# define MYCLOCK CLOCK_REALTIME +#endif + +int alarm_deadline (tain_t const *deadline) +{ + struct itimerspec it = { .it_interval = { .tv_sec = 0, .tv_nsec = 0 } } ; + struct sigevent se = { .sigev_notify = SIGEV_SIGNAL, .sigev_signo = SIGALRM, .sigev_value = { .sival_int = 0 }, .sigev_notify_function = 0, .sigev_notify_attributes = 0 } ; + if (!timespec_from_tain(&it.it_value, deadline)) return 0 ; + if (timer_create(MYCLOCK, &se, &timer_here) < 0) return 0 ; + if (timer_settime(timer_here, TIMER_ABSTIME, &it, 0) < 0) + { + int e = errno ; + timer_delete(timer_here) ; + errno = e ; + return 0 ; + } + return 1 ; +} + +#else + +int alarm_deadline (tain_t const *deadline) +{ + tain_t tto ; + tain_now(&tto) ; + tain_sub(&tto, deadline, &tto) ; + return alarm_timeout(&tto) ; +} + +#endif |