reorder timer initialization so that timer_create does not depend on free
[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         int timerid;
65
66         if (evp) sev = *evp;
67
68         switch (sev.sigev_notify) {
69         case SIGEV_NONE:
70         case SIGEV_SIGNAL:
71                 ksev.sigev_value = evp ? sev.sigev_value
72                         : (union sigval){.sival_ptr=t};
73                 ksev.sigev_signo = sev.sigev_signo;
74                 ksev.sigev_notify = sev.sigev_notify;
75                 ksev.sigev_tid = 0;
76                 if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0)
77                         return -1;
78                 if (!(t = calloc(1, sizeof *t))) {
79                         syscall(SYS_timer_delete, timerid);
80                         return -1;
81                 }
82                 t->timerid = timerid;
83                 break;
84         case SIGEV_THREAD:
85                 if (!libc.sigtimer) libc.sigtimer = sighandler;
86                 if (sev.sigev_notify_attributes)
87                         attr = *sev.sigev_notify_attributes;
88                 else
89                         pthread_attr_init(&attr);
90                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
91                 pthread_barrier_init(&args.b, 0, 2);
92                 args.sev = &sev;
93                 r = pthread_create(&td, &attr, start, &args);
94                 if (r) {
95                         errno = r;
96                         return -1;
97                 }
98                 pthread_barrier_wait(&args.b);
99                 t = args.t;
100                 t->thread = td;
101                 ksev.sigev_value.sival_ptr = t;
102                 ksev.sigev_signo = SIGCANCEL;
103                 ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
104                 ksev.sigev_tid = td->tid;
105                 if (syscall(SYS_timer_create, clk, &ksev, &t->timerid) < 0) {
106                         t->timerid = -1;
107                         pthread_cancel(td);
108                         return -1;
109                 }
110                 break;
111         default:
112                 errno = EINVAL;
113                 return -1;
114         }
115
116         *res = t;
117         return 0;
118 }