drop use of pthread_once in timer_create
[musl] / src / time / timer_create.c
1 #include <time.h>
2 #include <setjmp.h>
3 #include <limits.h>
4 #include "pthread_impl.h"
5 #include "atomic.h"
6
7 struct ksigevent {
8         union sigval sigev_value;
9         int sigev_signo;
10         int sigev_notify;
11         int sigev_tid;
12 };
13
14 struct start_args {
15         pthread_barrier_t b;
16         struct sigevent *sev;
17 };
18
19 static void dummy_0()
20 {
21 }
22 weak_alias(dummy_0, __pthread_tsd_run_dtors);
23
24 static void cleanup_fromsig(void *p)
25 {
26         pthread_t self = __pthread_self();
27         __pthread_tsd_run_dtors();
28         self->cancel = 0;
29         self->cancelbuf = 0;
30         self->canceldisable = 0;
31         self->cancelasync = 0;
32         __reset_tls();
33         longjmp(p, 1);
34 }
35
36 static void *start(void *arg)
37 {
38         pthread_t self = __pthread_self();
39         struct start_args *args = arg;
40         jmp_buf jb;
41
42         void (*notify)(union sigval) = args->sev->sigev_notify_function;
43         union sigval val = args->sev->sigev_value;
44
45         pthread_barrier_wait(&args->b);
46         for (;;) {
47                 siginfo_t si;
48                 while (sigwaitinfo(SIGTIMER_SET, &si) < 0);
49                 if (si.si_code == SI_TIMER && !setjmp(jb)) {
50                         pthread_cleanup_push(cleanup_fromsig, jb);
51                         notify(val);
52                         pthread_cleanup_pop(1);
53                 }
54                 if (self->timer_id < 0) break;
55         }
56         __syscall(SYS_timer_delete, self->timer_id & INT_MAX);
57         return 0;
58 }
59
60 int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict res)
61 {
62         volatile static int init = 0;
63         pthread_t td;
64         pthread_attr_t attr;
65         int r;
66         struct start_args args;
67         struct ksigevent ksev, *ksevp=0;
68         int timerid;
69         sigset_t set;
70
71         switch (evp ? evp->sigev_notify : SIGEV_SIGNAL) {
72         case SIGEV_NONE:
73         case SIGEV_SIGNAL:
74                 if (evp) {
75                         ksev.sigev_value = evp->sigev_value;
76                         ksev.sigev_signo = evp->sigev_signo;
77                         ksev.sigev_notify = evp->sigev_notify;
78                         ksev.sigev_tid = 0;
79                         ksevp = &ksev;
80                 }
81                 if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0)
82                         return -1;
83                 *res = (void *)(intptr_t)timerid;
84                 break;
85         case SIGEV_THREAD:
86                 if (!init) {
87                         struct sigaction sa = { .sa_handler = SIG_DFL };
88                         __libc_sigaction(SIGTIMER, &sa, 0);
89                         a_store(&init, 1);
90                 }
91                 if (evp->sigev_notify_attributes)
92                         attr = *evp->sigev_notify_attributes;
93                 else
94                         pthread_attr_init(&attr);
95                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
96                 pthread_barrier_init(&args.b, 0, 2);
97                 args.sev = evp;
98
99                 __block_app_sigs(&set);
100                 __syscall(SYS_rt_sigprocmask, SIG_BLOCK, SIGTIMER_SET, 0, _NSIG/8);
101                 r = pthread_create(&td, &attr, start, &args);
102                 __restore_sigs(&set);
103                 if (r) {
104                         errno = r;
105                         return -1;
106                 }
107
108                 ksev.sigev_value.sival_ptr = 0;
109                 ksev.sigev_signo = SIGTIMER;
110                 ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
111                 ksev.sigev_tid = td->tid;
112                 if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0)
113                         timerid = -1;
114                 td->timer_id = timerid;
115                 pthread_barrier_wait(&args.b);
116                 if (timerid < 0) return -1;
117                 *res = (void *)(INTPTR_MIN | (uintptr_t)td>>1);
118                 break;
119         default:
120                 errno = EINVAL;
121                 return -1;
122         }
123
124         return 0;
125 }