3 #include "pthread_impl.h"
6 union sigval sigev_value;
20 weak_alias(dummy_0, __pthread_tsd_run_dtors);
22 static void cleanup_fromsig(void *p)
24 pthread_t self = __pthread_self();
25 __pthread_tsd_run_dtors();
28 self->canceldisable = 0;
29 self->cancelasync = 0;
34 static void timer_handler(int sig, siginfo_t *si, void *ctx)
38 static void install_handler()
40 struct sigaction sa = {
41 .sa_sigaction = timer_handler,
42 .sa_flags = SA_SIGINFO | SA_RESTART
44 __libc_sigaction(SIGTIMER, &sa, 0);
47 static void *start(void *arg)
49 pthread_t self = __pthread_self();
50 struct start_args *args = arg;
51 int id = self->timer_id;
54 void (*notify)(union sigval) = args->sev->sigev_notify_function;
55 union sigval val = args->sev->sigev_value;
57 pthread_barrier_wait(&args->b);
60 while (sigwaitinfo(SIGTIMER_SET, &si) < 0);
61 if (si.si_code == SI_TIMER && !setjmp(jb)) {
62 pthread_cleanup_push(cleanup_fromsig, jb);
64 pthread_cleanup_pop(1);
66 if (self->timer_id < 0) break;
68 __syscall(SYS_timer_delete, id);
72 int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict res)
74 static pthread_once_t once = PTHREAD_ONCE_INIT;
78 struct start_args args;
79 struct ksigevent ksev, *ksevp=0;
83 switch (evp ? evp->sigev_notify : SIGEV_SIGNAL) {
87 ksev.sigev_value = evp->sigev_value;
88 ksev.sigev_signo = evp->sigev_signo;
89 ksev.sigev_notify = evp->sigev_notify;
93 if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0)
95 *res = (void *)(intptr_t)timerid;
98 pthread_once(&once, install_handler);
99 if (evp->sigev_notify_attributes)
100 attr = *evp->sigev_notify_attributes;
102 pthread_attr_init(&attr);
103 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
104 pthread_barrier_init(&args.b, 0, 2);
107 __block_app_sigs(&set);
108 __syscall(SYS_rt_sigprocmask, SIG_BLOCK, SIGTIMER_SET, 0, _NSIG/8);
109 r = pthread_create(&td, &attr, start, &args);
110 __restore_sigs(&set);
116 ksev.sigev_value.sival_ptr = 0;
117 ksev.sigev_signo = SIGTIMER;
118 ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
119 ksev.sigev_tid = td->tid;
120 if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0)
122 td->timer_id = timerid;
123 pthread_barrier_wait(&args.b);
124 if (timerid < 0) return -1;
125 *res = (void *)(INTPTR_MIN | (uintptr_t)td>>1);