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