optimize timer creation and possibly protect against some minor races
[musl] / 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         pthread_t self = __pthread_self();
21         void (*notify)(union sigval) = (void (*)(union sigval))self->start;
22         union sigval val = { .sival_ptr = self->start_arg };
23
24         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &st);
25         notify(val);
26         pthread_setcancelstate(st, 0);
27 }
28
29 static void killtimer(void *arg)
30 {
31         timer_t t = arg;
32         if (t->timerid >= 0) __syscall(SYS_timer_delete, t->timerid);
33 }
34
35 static void *start(void *arg)
36 {
37         pthread_t self = __pthread_self();
38         struct start_args *args = arg;
39         struct __timer t = { .timerid = -1 };
40
41         /* Reuse no-longer-needed thread structure fields to avoid
42          * needing the timer address in the signal handler. */
43         self->start = (void *(*)(void *))args->sev->sigev_notify_function;
44         self->start_arg = args->sev->sigev_value.sival_ptr;
45         args->t = &t;
46
47         pthread_cleanup_push(killtimer, &t);
48         pthread_barrier_wait(&args->b);
49         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
50         /* Loop on async-signal-safe cancellation point */
51         for (;;) sleep(1);
52         pthread_cleanup_pop(1);
53         return 0;
54 }
55
56 int timer_create(clockid_t clk, struct sigevent *evp, timer_t *res)
57 {
58         struct sigevent sev = { 
59                 .sigev_notify = SIGEV_SIGNAL,
60                 .sigev_signo = SIGALRM
61         };
62         pthread_t td;
63         pthread_attr_t attr;
64         int r;
65         struct start_args args;
66         timer_t t;
67         struct ksigevent ksev;
68         int timerid;
69
70         if (evp) sev = *evp;
71
72         switch (sev.sigev_notify) {
73         case SIGEV_NONE:
74         case SIGEV_SIGNAL:
75                 ksev.sigev_value = evp ? sev.sigev_value
76                         : (union sigval){.sival_ptr=t};
77                 ksev.sigev_signo = sev.sigev_signo;
78                 ksev.sigev_notify = sev.sigev_notify;
79                 ksev.sigev_tid = 0;
80                 if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0)
81                         return -1;
82                 if (!(t = calloc(1, sizeof *t))) {
83                         syscall(SYS_timer_delete, timerid);
84                         return -1;
85                 }
86                 t->timerid = timerid;
87                 break;
88         case SIGEV_THREAD:
89                 if (!libc.sigtimer) libc.sigtimer = sighandler;
90                 if (sev.sigev_notify_attributes)
91                         attr = *sev.sigev_notify_attributes;
92                 else
93                         pthread_attr_init(&attr);
94                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
95                 pthread_barrier_init(&args.b, 0, 2);
96                 args.sev = &sev;
97                 r = pthread_create(&td, &attr, start, &args);
98                 if (r) {
99                         errno = r;
100                         return -1;
101                 }
102                 ksev.sigev_value.sival_ptr = 0;
103                 ksev.sigev_signo = SIGCANCEL;
104                 ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
105                 ksev.sigev_tid = td->tid;
106                 r = syscall(SYS_timer_create, clk, &ksev, &timerid);
107                 pthread_barrier_wait(&args.b);
108                 t = args.t;
109                 if (r < 0) {
110                         pthread_cancel(td);
111                         return -1;
112                 }
113                 t->timerid = timerid;
114                 t->thread = td;
115                 break;
116         default:
117                 errno = EINVAL;
118                 return -1;
119         }
120
121         *res = t;
122         return 0;
123 }