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;
30 self->unblock_cancel = 0;
35 static void timer_handler(int sig, siginfo_t *si, void *ctx)
39 static void install_handler()
41 struct sigaction sa = {
42 .sa_sigaction = timer_handler,
43 .sa_flags = SA_SIGINFO | SA_RESTART
45 __libc_sigaction(SIGTIMER, &sa, 0);
48 static void *start(void *arg)
50 pthread_t self = __pthread_self();
51 struct start_args *args = arg;
52 int id = self->timer_id;
55 void (*notify)(union sigval) = args->sev->sigev_notify_function;
56 union sigval val = args->sev->sigev_value;
58 pthread_barrier_wait(&args->b);
61 while (sigwaitinfo(SIGTIMER_SET, &si) < 0);
62 if (si.si_code == SI_TIMER && !setjmp(jb)) {
63 pthread_cleanup_push(cleanup_fromsig, jb);
65 pthread_cleanup_pop(1);
67 if (self->timer_id < 0) break;
69 __syscall(SYS_timer_delete, id);
73 int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict res)
75 static pthread_once_t once = PTHREAD_ONCE_INIT;
79 struct start_args args;
80 struct ksigevent ksev, *ksevp=0;
84 switch (evp ? evp->sigev_notify : SIGEV_SIGNAL) {
88 ksev.sigev_value = evp->sigev_value;
89 ksev.sigev_signo = evp->sigev_signo;
90 ksev.sigev_notify = evp->sigev_notify;
94 if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0)
96 *res = (void *)(intptr_t)timerid;
99 pthread_once(&once, install_handler);
100 if (evp->sigev_notify_attributes)
101 attr = *evp->sigev_notify_attributes;
103 pthread_attr_init(&attr);
104 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
105 pthread_barrier_init(&args.b, 0, 2);
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);
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)
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);