implement POSIX timers
[oweals/musl.git] / src / time / timer_create.c
1 #include <time.h>
2 #include "pthread_impl.h"
3
4 struct ksigevent {
5         union sigval sigev_value;
6         int sigev_signo;
7         int sigev_notify;
8         int sigev_tid;
9 };
10
11 struct start_args {
12         pthread_barrier_t b;
13         struct sigevent *sev;
14         timer_t t;
15 };
16
17 static void sighandler(int sig, siginfo_t *si, void *ctx)
18 {
19         int st;
20         timer_t t = si->si_value.sival_ptr;
21         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &st);
22         t->notify(t->val);
23         pthread_setcancelstate(st, 0);
24 }
25
26 static void killtimer(void *arg)
27 {
28         timer_t t = arg;
29         if (t->timerid >= 0) __syscall(SYS_timer_delete, t->timerid);
30 }
31
32 static void *start(void *arg)
33 {
34         struct start_args *args = arg;
35         struct __timer t = {
36                 .notify = args->sev->sigev_notify_function,
37                 .val = args->sev->sigev_value,
38         };
39
40         args->t = &t;
41
42         pthread_barrier_wait(&args->b);
43
44         pthread_cleanup_push(killtimer, &t);
45         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
46         /* Loop on async-signal-safe cancellation point */
47         for (;;) sleep(1);
48         pthread_cleanup_pop(1);
49         return 0;
50 }
51
52 int timer_create(clockid_t clk, struct sigevent *evp, timer_t *res)
53 {
54         struct sigevent sev = { 
55                 .sigev_notify = SIGEV_SIGNAL,
56                 .sigev_signo = SIGALRM
57         };
58         pthread_t td;
59         pthread_attr_t attr;
60         int r;
61         struct start_args args;
62         timer_t t;
63         struct ksigevent ksev;
64
65         if (evp) sev = *evp;
66
67         switch (sev.sigev_notify) {
68         case SIGEV_NONE:
69         case SIGEV_SIGNAL:
70                 if (!(t = calloc(1, sizeof *t)))
71                         return -1;
72                 ksev.sigev_value = evp ? sev.sigev_value
73                         : (union sigval){.sival_ptr=t};
74                 ksev.sigev_signo = sev.sigev_signo;
75                 ksev.sigev_notify = sev.sigev_notify;
76                 ksev.sigev_tid = 0;
77                 break;
78         case SIGEV_THREAD:
79                 if (sev.sigev_notify_attributes)
80                         attr = *sev.sigev_notify_attributes;
81                 else
82                         pthread_attr_init(&attr);
83                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
84                 pthread_barrier_init(&args.b, 0, 2);
85                 args.sev = &sev;
86                 r = pthread_create(&td, &attr, start, &args);
87                 if (r) {
88                         errno = r;
89                         return -1;
90                 }
91                 pthread_barrier_wait(&args.b);
92                 t = args.t;
93                 t->thread = td;
94                 ksev.sigev_value.sival_ptr = t;
95                 ksev.sigev_signo = SIGCANCEL;
96                 ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
97                 ksev.sigev_tid = td->tid;
98                 if (!libc.sigtimer) libc.sigtimer = sighandler;
99                 break;
100         }
101
102         t->timerid = -1;
103         if (syscall(SYS_timer_create, clk, &ksev, &t->timerid) < 0) {
104                 timer_delete(t);
105                 return -1;
106         }
107
108         *res = t;
109         return 0;
110 }