summaryrefslogtreecommitdiff
path: root/src/libstddjb
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstddjb')
-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
6 files changed, 230 insertions, 2 deletions
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>