OSDN Git Service

9421957462737bb63722e7d8eff020ce019acf83
[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 }
38
39 static void install_handler()
40 {
41         struct sigaction sa = {
42                 .sa_sigaction = timer_handler,
43                 .sa_flags = SA_SIGINFO | SA_RESTART
44         };
45         __libc_sigaction(SIGTIMER, &sa, 0);
46 }
47
48 static void *start(void *arg)
49 {
50         pthread_t self = __pthread_self();
51         struct start_args *args = arg;
52         int id = self->timer_id;
53         jmp_buf jb;
54
55         void (*notify)(union sigval) = args->sev->sigev_notify_function;
56         union sigval val = args->sev->sigev_value;
57
58         pthread_barrier_wait(&args->b);
59         for (;;) {
60                 siginfo_t si;
61                 while (sigwaitinfo(SIGTIMER_SET, &si) < 0);
62                 if (si.si_code == SI_TIMER && !setjmp(jb)) {
63                         pthread_cleanup_push(cleanup_fromsig, jb);
64                         notify(val);
65                         pthread_cleanup_pop(1);
66                 }
67                 if (self->timer_id < 0) break;
68         }
69         __syscall(SYS_timer_delete, id);
70         return 0;
71 }
72
73 int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict res)
74 {
75         static pthread_once_t once = PTHREAD_ONCE_INIT;
76         pthread_t td;
77         pthread_attr_t attr;
78         int r;
79         struct start_args args;
80         struct ksigevent ksev, *ksevp=0;
81         int timerid;
82         sigset_t set;
83
84         switch (evp ? evp->sigev_notify : SIGEV_SIGNAL) {
85         case SIGEV_NONE:
86         case SIGEV_SIGNAL:
87                 if (evp) {
88                         ksev.sigev_value = evp->sigev_value;
89                         ksev.sigev_signo = evp->sigev_signo;
90                         ksev.sigev_notify = evp->sigev_notify;
91                         ksev.sigev_tid = 0;
92                         ksevp = &ksev;
93                 }
94                 if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0)
95                         return -1;
96                 *res = (void *)(intptr_t)timerid;
97                 break;
98         case SIGEV_THREAD:
99                 pthread_once(&once, install_handler);
100                 if (evp->sigev_notify_attributes)
101                         attr = *evp->sigev_notify_attributes;
102                 else
103                         pthread_attr_init(&attr);
104                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
105                 pthread_barrier_init(&args.b, 0, 2);
106                 args.sev = evp;
107
108                 __block_app_sigs(&set);
109                 __syscall(SYS_rt_sigprocmask, SIG_BLOCK, SIGTIMER_SET, 0, _NSIG/8);
110                 r = pthread_create(&td, &attr, start, &args);
111                 __restore_sigs(&set);
112                 if (r) {
113                         errno = r;
114                         return -1;
115                 }
116
117                 ksev.sigev_value.sival_ptr = 0;
118                 ksev.sigev_signo = SIGTIMER;
119                 ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
120                 ksev.sigev_tid = td->tid;
121                 if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0)
122                         timerid = -1;
123                 td->timer_id = timerid;
124                 pthread_barrier_wait(&args.b);
125                 if (timerid < 0) return -1;
126                 *res = (void *)(INTPTR_MIN | (uintptr_t)td>>1);
127                 break;
128         default:
129                 errno = EINVAL;
130                 return -1;
131         }
132
133         return 0;
134 }