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