summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2016-10-24 02:15:36 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2016-10-24 02:15:36 +0000
commit9ef3a9f8b2704693496af12120ea3ab40389bf7b (patch)
tree9ed582b049881f1271a6b02978f1ed6d28a4f948 /src
parent422d91b2b0a2b8b3a8af510cc55b1400c60be303 (diff)
downloadskalibs-9ef3a9f8b2704693496af12120ea3ab40389bf7b.tar.xz
Add the alarm library, first draft.
Diffstat (limited to 'src')
-rw-r--r--src/include/skalibs/alarm.h13
-rw-r--r--src/include/skalibs/stddjb.h1
-rw-r--r--src/libstddjb/alarm-internal.h14
-rw-r--r--src/libstddjb/alarm_deadline.c47
-rw-r--r--src/libstddjb/alarm_disable.c41
-rw-r--r--src/libstddjb/alarm_milliseconds.c61
-rw-r--r--src/libstddjb/alarm_timeout.c65
-rw-r--r--src/libstddjb/alloc-internal.h4
-rw-r--r--src/sysdeps/tryitimer.c10
-rw-r--r--src/sysdeps/trytimer.c12
10 files changed, 266 insertions, 2 deletions
diff --git a/src/include/skalibs/alarm.h b/src/include/skalibs/alarm.h
new file mode 100644
index 0000000..9c2951a
--- /dev/null
+++ b/src/include/skalibs/alarm.h
@@ -0,0 +1,13 @@
+/* ISC license. */
+
+#ifndef ALARM_H
+#define ALARM_H
+
+#include <skalibs/tai.h>
+
+extern int alarm_milliseconds (unsigned int) ;
+extern int alarm_timeout (tain_t const *) ;
+extern int alarm_deadline (tain_t const *) ;
+extern void alarm_disable (void) ;
+
+#endif
diff --git a/src/include/skalibs/stddjb.h b/src/include/skalibs/stddjb.h
index e265003..3ebd219 100644
--- a/src/include/skalibs/stddjb.h
+++ b/src/include/skalibs/stddjb.h
@@ -15,6 +15,7 @@
#include <skalibs/ip46.h>
#include <skalibs/setgroups.h>
+#include <skalibs/alarm.h>
#include <skalibs/alloc.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/bitarray.h>
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 <time.h>
+
+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 <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
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 <skalibs/sysdeps.h>
+#include <skalibs/alarm.h>
+
+#ifdef SKALIBS_HASTIMER
+
+#include <time.h>
+#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 <sys/time.h>
+
+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 <unistd.h>
+
+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 <skalibs/sysdeps.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_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 <sys/time.h>
+
+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 <unistd.h>
+#include <limits.h>
+
+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 <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_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 <sys/time.h>
+
+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 <unistd.h>
+#include <limits.h>
+
+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 <skalibs/sysdeps.h>
#include <skalibs/alloc.h>
diff --git a/src/sysdeps/tryitimer.c b/src/sysdeps/tryitimer.c
new file mode 100644
index 0000000..4fd3454
--- /dev/null
+++ b/src/sysdeps/tryitimer.c
@@ -0,0 +1,10 @@
+/* ISC license. */
+
+#include <sys/time.h>
+
+int main (void)
+{
+ struct itimerval blah ;
+ if (getitimer(ITIMER_REAL, &blah) < 0) return 111 ;
+ return 0 ;
+}
diff --git a/src/sysdeps/trytimer.c b/src/sysdeps/trytimer.c
new file mode 100644
index 0000000..84cea0d
--- /dev/null
+++ b/src/sysdeps/trytimer.c
@@ -0,0 +1,12 @@
+/* ISC license. */
+
+#include <sys/types.h>
+#include <signal.h>
+#include <time.h>
+
+int main (void)
+{
+ timer_t blah ;
+ if (timer_create(CLOCK_REALTIME, 0, &blah) < 0) return 111 ;
+ return 0 ;
+}