run pthread tsd destructors when a timer thread pretends to exit
[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 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         self->cancel = 0;
25         self->cancelbuf = 0;
26         __pthread_tsd_run_dtors(self);
27         longjmp(p, 1);
28 }
29
30 void __sigtimer_handler(pthread_t self)
31 {
32         jmp_buf jb;
33         void (*notify)(union sigval) = (void (*)(union sigval))self->start;
34         union sigval val = { .sival_ptr = self->start_arg };
35
36         if (setjmp(jb)) return;
37         pthread_cleanup_push(cleanup_fromsig, jb);
38         notify(val);
39         pthread_cleanup_pop(0);
40 }
41
42 static void *start(void *arg)
43 {
44         pthread_t self = __pthread_self();
45         struct start_args *args = arg;
46
47         /* Reuse no-longer-needed thread structure fields to avoid
48          * needing the timer address in the signal handler. */
49         self->start = (void *(*)(void *))args->sev->sigev_notify_function;
50         self->start_arg = args->sev->sigev_value.sival_ptr;
51         self->result = (void *)-1;
52
53         pthread_barrier_wait(&args->b);
54         __wait(&self->delete_timer, 0, 0, 1);
55         __syscall(SYS_timer_delete, self->result);
56         return 0;
57 }
58
59 int timer_create(clockid_t clk, struct sigevent *evp, timer_t *res)
60 {
61         pthread_t td;
62         pthread_attr_t attr;
63         int r;
64         struct start_args args;
65         struct ksigevent ksev, *ksevp=0;
66         int timerid;
67
68         switch (evp ? evp->sigev_notify : SIGEV_SIGNAL) {
69         case SIGEV_NONE:
70         case SIGEV_SIGNAL:
71                 if (evp) {
72                         ksev.sigev_value = evp->sigev_value;
73                         ksev.sigev_signo = evp->sigev_signo;
74                         ksev.sigev_notify = evp->sigev_notify;
75                         ksev.sigev_tid = 0;
76                         ksevp = &ksev;
77                 }
78                 if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0)
79                         return -1;
80                 *res = (void *)timerid;
81                 break;
82         case SIGEV_THREAD:
83                 if (evp->sigev_notify_attributes)
84                         attr = *evp->sigev_notify_attributes;
85                 else
86                         pthread_attr_init(&attr);
87                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
88                 pthread_barrier_init(&args.b, 0, 2);
89                 args.sev = evp;
90                 r = pthread_create(&td, &attr, start, &args);
91                 if (r) {
92                         errno = r;
93                         return -1;
94                 }
95                 ksev.sigev_value.sival_ptr = 0;
96                 ksev.sigev_signo = SIGCANCEL;
97                 ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
98                 ksev.sigev_tid = td->tid;
99                 r = syscall(SYS_timer_create, clk, &ksev, &timerid);
100                 pthread_barrier_wait(&args.b);
101                 if (r < 0) {
102                         pthread_cancel(td);
103                         return -1;
104                 }
105                 td->result = (void *)timerid;
106                 *res = td;
107                 break;
108         default:
109                 errno = EINVAL;
110                 return -1;
111         }
112
113         return 0;
114 }