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