From 9ef3a9f8b2704693496af12120ea3ab40389bf7b Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Mon, 24 Oct 2016 02:15:36 +0000 Subject: Add the alarm library, first draft. --- src/libstddjb/alarm-internal.h | 14 ++++++++ src/libstddjb/alarm_deadline.c | 47 +++++++++++++++++++++++++++ src/libstddjb/alarm_disable.c | 41 ++++++++++++++++++++++++ src/libstddjb/alarm_milliseconds.c | 61 +++++++++++++++++++++++++++++++++++ src/libstddjb/alarm_timeout.c | 65 ++++++++++++++++++++++++++++++++++++++ src/libstddjb/alloc-internal.h | 4 +-- 6 files changed, 230 insertions(+), 2 deletions(-) create mode 100644 src/libstddjb/alarm-internal.h create mode 100644 src/libstddjb/alarm_deadline.c create mode 100644 src/libstddjb/alarm_disable.c create mode 100644 src/libstddjb/alarm_milliseconds.c create mode 100644 src/libstddjb/alarm_timeout.c (limited to 'src/libstddjb') diff --git a/src/libstddjb/alarm-internal.h b/src/libstddjb/alarm-internal.h new file mode 100644 index 0000000..027a0dc --- /dev/null +++ b/src/libstddjb/alarm-internal.h @@ -0,0 +1,14 @@ +/* ISC license. */ + +#ifndef ALARM_INTERNAL_H +#define ALARM_INTERNAL_H + +#ifdef SKALIBS_HASTIMER + +#include + +extern timer_t timer_here ; + +#endif + +#endif 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 +#include +#include + +#ifdef SKALIBS_HASTIMER + +#include +#include +#include +#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 diff --git a/src/libstddjb/alarm_disable.c b/src/libstddjb/alarm_disable.c new file mode 100644 index 0000000..a383411 --- /dev/null +++ b/src/libstddjb/alarm_disable.c @@ -0,0 +1,41 @@ +/* ISC license. */ + +#include +#include + +#ifdef SKALIBS_HASTIMER + +#include +#include "alarm-internal.h" + +timer_t timer_here ; + +void alarm_disable () +{ + struct itimerspec stopit = { .it_value = { .tv_sec = 0, .tv_nsec = 0 }, .it_interval = { .tv_sec = 0, .tv_nsec = 0 } } ; + timer_settime(timer_here, 0, &stopit, 0) ; + timer_delete(timer_here) ; +} + +#else +#ifdef SKALIBS_HASITIMER + +#include + +void alarm_disable () +{ + struct itimerval stopit = { .it_value = { .tv_sec = 0, .tv_usec = 0 }, .it_interval = { .tv_sec = 0, .tv_usec = 0 } } ; + setitimer(ITIMER_REAL, &stopit, 0) ; +} + +#else + +#include + +void alarm_disable () +{ + alarm(0) ; +} + +#endif +#endif diff --git a/src/libstddjb/alarm_milliseconds.c b/src/libstddjb/alarm_milliseconds.c new file mode 100644 index 0000000..0628518 --- /dev/null +++ b/src/libstddjb/alarm_milliseconds.c @@ -0,0 +1,61 @@ +/* ISC license. */ + +#include +#include + +#ifdef SKALIBS_HASTIMER + +#include +#include +#include +#include "alarm-internal.h" + +#undef MYCLOCK +#ifdef SKALIBS_HASCLOCKMON +# define MYCLOCK CLOCK_MONOTONIC +#else +# define MYCLOCK CLOCK_REALTIME +#endif + +int alarm_milliseconds (unsigned int t) +{ + struct itimerspec it = { .it_interval = { .tv_sec = 0, .tv_nsec = 0 }, .it_value = { .tv_sec = t / 1000, .tv_nsec = 1000000 * (t % 1000) } } ; + struct sigevent se = { .sigev_notify = SIGEV_SIGNAL, .sigev_signo = SIGALRM, .sigev_value = { .sival_int = 0 }, .sigev_notify_function = 0, .sigev_notify_attributes = 0 } ; + if (timer_create(MYCLOCK, &se, &timer_here) < 0) return 0 ; + if (timer_settime(timer_here, 0, &it, 0) < 0) + { + int e = errno ; + timer_delete(timer_here) ; + errno = e ; + return 0 ; + } + return 1 ; +} + +#else +#ifdef SKALIBS_HASITIMER + +#include + +int alarm_milliseconds (unsigned int t) +{ + struct itimerval it = { .it_interval = { .tv_sec = 0, .tv_usec = 0 }, .it_value = { .tv_sec = t / 1000, .tv_usec = 1000 * (t % 1000) } } ; + if (setitimer(ITIMER_REAL, &it, 0) < 0) return 0 ; + return 1 ; +} + +#else + +#include +#include + +int alarm_milliseconds (unsigned int t) +{ + if (t > UINT_MAX - 999) return 0 ; + t = (t + 999) / 1000 ; + alarm(t) ; + return 1 ; +} + +#endif +#endif diff --git a/src/libstddjb/alarm_timeout.c b/src/libstddjb/alarm_timeout.c new file mode 100644 index 0000000..630b524 --- /dev/null +++ b/src/libstddjb/alarm_timeout.c @@ -0,0 +1,65 @@ +/* ISC license. */ + +#include +#include +#include + +#ifdef SKALIBS_HASTIMER + +#include +#include +#include +#include "alarm-internal.h" + +#undef MYCLOCK +#ifdef SKALIBS_HASCLOCKMON +# define MYCLOCK CLOCK_MONOTONIC +#else +# define MYCLOCK CLOCK_REALTIME +#endif + +int alarm_timeout (tain_t const *tto) +{ + 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_relative(&it.it_value, tto)) return 0 ; + if (timer_create(MYCLOCK, &se, &timer_here) < 0) return 0 ; + if (timer_settime(timer_here, 0, &it, 0) < 0) + { + int e = errno ; + timer_delete(timer_here) ; + errno = e ; + return 0 ; + } + return 1 ; +} + +#else +#ifdef SKALIBS_HASITIMER + +#include + +int alarm_timeout (tain_t const *tto) +{ + struct itimerval it = { .it_interval = { .tv_sec = 0, .tv_usec = 0 } } ; + if (!timeval_from_tain_relative(&it.it_value, tto)) return 0 ; + if (setitimer(ITIMER_REAL, &it, 0) < 0) return 0 ; + return 1 ; +} + +#else + +#include +#include + +int alarm_timeout (tain_t const *tto) +{ + int t = tain_to_millisecs(tto) ; + if (t < 0 || t > INT_MAX - 999) return 0 ; + t = (t + 999) / 1000 ; + alarm(t) ; + return 1 ; +} + +#endif +#endif diff --git a/src/libstddjb/alloc-internal.h b/src/libstddjb/alloc-internal.h index 918b5df..6fd312e 100644 --- a/src/libstddjb/alloc-internal.h +++ b/src/libstddjb/alloc-internal.h @@ -1,7 +1,7 @@ /* ISC license. */ -#ifndef ALLOC_0_H -#define ALLOC_0_H +#ifndef ALLOC_INTERNAL_H +#define ALLOC_INTERNAL_H #include #include -- cgit v1.2.3