OSDN Git Service

d9fbaee5692ade5cfa74e8a108c2b2a103901ea5
[android-x86/external-musl-libc.git] / 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_0()
18 {
19 }
20 weak_alias(dummy_0, __pthread_tsd_run_dtors);
21
22 static void cleanup_fromsig(void *p)
23 {
24         pthread_t self = __pthread_self();
25         __pthread_tsd_run_dtors();
26         self->cancel = 0;
27         self->cancelbuf = 0;
28         self->canceldisable = 0;
29         self->cancelasync = 0;
30         self->unblock_cancel = 0;
31         __reset_tls();
32         longjmp(p, 1);
33 }
34
35 static void timer_handler(int sig, siginfo_t *si, void *ctx)
36 {
37         pthread_t self = __pthread_self();
38         jmp_buf jb;
39         void (*notify)(union sigval) = (void (*)(union sigval))self->start;
40         union sigval val = { .sival_ptr = self->start_arg };
41
42         if (!setjmp(jb) && si->si_code == SI_TIMER) {
43                 pthread_cleanup_push(cleanup_fromsig, jb);
44                 notify(val);
45                 pthread_cleanup_pop(1);
46         }
47 }
48
49 static void install_handler()
50 {
51         struct sigaction sa = {
52                 .sa_sigaction = timer_handler,
53                 .sa_flags = SA_SIGINFO | SA_RESTART
54         };
55         __libc_sigaction(SIGTIMER, &sa, 0);
56 }
57
58 static void *start(void *arg)
59 {
60         pthread_t self = __pthread_self();
61         struct start_args *args = arg;
62         int id;
63
64         /* Reuse no-longer-needed thread structure fields to avoid
65          * needing the timer address in the signal handler. */
66         self->start = (void *(*)(void *))args->sev->sigev_notify_function;
67         self->start_arg = args->sev->sigev_value.sival_ptr;
68
69         pthread_barrier_wait(&args->b);
70         if ((id = self->timer_id) >= 0) {
71                 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
72                         SIGTIMER_SET, 0, _NSIG/8);
73                 __wait(&self->timer_id, 0, id, 1);
74                 __syscall(SYS_timer_delete, id);
75         }
76         return 0;
77 }
78
79 int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict res)
80 {
81         static pthread_once_t once = PTHREAD_ONCE_INIT;
82         pthread_t td;
83         pthread_attr_t attr;
84         int r;
85         struct start_args args;
86         struct ksigevent ksev, *ksevp=0;
87         int timerid;
88         sigset_t set;
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 *)(intptr_t)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
114                 __block_app_sigs(&set);
115                 r = pthread_create(&td, &attr, start, &args);
116                 __restore_sigs(&set);
117                 if (r) {
118                         errno = r;
119                         return -1;
120                 }
121
122                 ksev.sigev_value.sival_ptr = 0;
123                 ksev.sigev_signo = SIGTIMER;
124                 ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
125                 ksev.sigev_tid = td->tid;
126                 if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0)
127                         timerid = -1;
128                 td->timer_id = timerid;
129                 pthread_barrier_wait(&args.b);
130                 if (timerid < 0) return -1;
131                 *res = (void *)(INTPTR_MIN | (uintptr_t)td>>1);
132                 break;
133         default:
134                 errno = EINVAL;
135                 return -1;
136         }
137
138         return 0;
139 }