skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

alarm_deadline.c (1078B)


      1 /* ISC license. */
      2 
      3 #include <skalibs/sysdeps.h>
      4 #include <skalibs/tai.h>
      5 #include <skalibs/alarm.h>
      6 
      7 #ifdef SKALIBS_HASTIMER
      8 
      9 #include <errno.h>
     10 #include <signal.h>
     11 #include <time.h>
     12 #include "alarm-internal.h"
     13 
     14 #undef MYCLOCK
     15 #ifdef SKALIBS_HASCLOCKMON
     16 # define MYCLOCK CLOCK_MONOTONIC
     17 #else
     18 # define MYCLOCK CLOCK_REALTIME
     19 #endif
     20 
     21 int alarm_deadline (tain const *deadline)
     22 {
     23   struct itimerspec it = { .it_interval = { .tv_sec = 0, .tv_nsec = 0 } } ;
     24   struct sigevent se = { .sigev_notify = SIGEV_SIGNAL, .sigev_signo = SIGALRM, .sigev_value = { .sival_int = 0 }, .sigev_notify_function = 0, .sigev_notify_attributes = 0 } ;
     25   if (!timespec_from_tain(&it.it_value, deadline)) return 0 ;
     26   if (timer_create(MYCLOCK, &se, &timer_here) < 0) return 0 ;
     27   if (timer_settime(timer_here, TIMER_ABSTIME, &it, 0) < 0)
     28   {
     29     int e = errno ;
     30     timer_delete(timer_here) ;
     31     errno = e ;
     32     return 0 ;
     33   }
     34   return 1 ;
     35 }
     36 
     37 #else
     38 
     39 int alarm_deadline (tain const *deadline)
     40 {
     41   tain tto ;
     42   tain_now(&tto) ;
     43   tain_sub(&tto, deadline, &tto) ;
     44   return alarm_timeout(&tto) ;
     45 }
     46 
     47 #endif