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