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