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