summaryrefslogtreecommitdiffstats
path: root/lib/monotonic.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/monotonic.c')
-rw-r--r--lib/monotonic.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/monotonic.c b/lib/monotonic.c
index 3d4a4438e..a124debba 100644
--- a/lib/monotonic.c
+++ b/lib/monotonic.c
@@ -3,6 +3,7 @@
* -lrt on systems with old libc.
*/
#include <time.h>
+#include <signal.h>
#include <sys/sysinfo.h>
#include <sys/time.h>
@@ -66,3 +67,36 @@ int gettime_monotonic(struct timeval *tv)
return gettimeofday(tv, NULL);
#endif
}
+
+int setup_timer(timer_t * t_id, struct itimerval *timeout,
+ void (*timeout_handler)(void))
+{
+ struct sigaction sig_a;
+ static struct sigevent sig_e = {
+ .sigev_notify = SIGEV_SIGNAL,
+ .sigev_signo = SIGALRM
+ };
+ struct itimerspec val = {
+ .it_value.tv_sec = timeout->it_value.tv_sec,
+ .it_value.tv_nsec = timeout->it_value.tv_usec * 1000,
+ .it_interval.tv_sec = 0,
+ .it_interval.tv_nsec = 0
+ };
+
+ if (sigemptyset(&sig_a.sa_mask))
+ return 1;
+ sig_a.sa_flags = SA_SIGINFO;
+ sig_a.sa_handler = timeout_handler;
+ if (sigaction(SIGALRM, &sig_a, 0))
+ return 1;
+ if (timer_create(CLOCK_MONOTONIC, &sig_e, t_id))
+ return 1;
+ if (timer_settime(*t_id, SA_SIGINFO, &val, NULL))
+ return 1;
+ return 0;
+}
+
+void cancel_timer(timer_t *t_id)
+{
+ timer_delete(*t_id);
+}