3 #include "pthread_impl.h"
6 union sigval sigev_value;
17 static void dummy_1(pthread_t self)
20 weak_alias(dummy_1, __pthread_tsd_run_dtors);
22 static void cleanup_fromsig(void *p)
24 pthread_t self = __pthread_self();
25 __pthread_tsd_run_dtors(self);
28 self->canceldisable = 0;
29 self->cancelasync = 0;
30 self->unblock_cancel = 0;
34 static void timer_handler(int sig, siginfo_t *si, void *ctx)
36 pthread_t self = __pthread_self();
38 void (*notify)(union sigval) = (void (*)(union sigval))self->start;
39 union sigval val = { .sival_ptr = self->start_arg };
41 if (!setjmp(jb) && si->si_code == SI_TIMER) {
42 pthread_cleanup_push(cleanup_fromsig, jb);
44 pthread_cleanup_pop(1);
48 static void install_handler()
50 struct sigaction sa = {
51 .sa_sigaction = timer_handler,
52 .sa_flags = SA_SIGINFO | SA_RESTART
54 __libc_sigaction(SIGTIMER, &sa, 0);
55 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
56 SIGTIMER_SET, 0, _NSIG/8);
59 static void *start(void *arg)
61 pthread_t self = __pthread_self();
62 struct start_args *args = arg;
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;
70 pthread_barrier_wait(&args->b);
71 if ((id = self->timer_id) >= 0) {
72 __wait(&self->timer_id, 0, id, 1);
73 __syscall(SYS_timer_delete, id);
78 int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict res)
80 static pthread_once_t once = PTHREAD_ONCE_INIT;
84 struct start_args args;
85 struct ksigevent ksev, *ksevp=0;
89 switch (evp ? evp->sigev_notify : SIGEV_SIGNAL) {
93 ksev.sigev_value = evp->sigev_value;
94 ksev.sigev_signo = evp->sigev_signo;
95 ksev.sigev_notify = evp->sigev_notify;
99 if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0)
101 *res = (void *)(intptr_t)timerid;
104 pthread_once(&once, install_handler);
105 if (evp->sigev_notify_attributes)
106 attr = *evp->sigev_notify_attributes;
108 pthread_attr_init(&attr);
109 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
110 pthread_barrier_init(&args.b, 0, 2);
113 __block_app_sigs(&set);
114 r = pthread_create(&td, &attr, start, &args);
115 __restore_sigs(&set);
121 ksev.sigev_value.sival_ptr = 0;
122 ksev.sigev_signo = SIGTIMER;
123 ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
124 ksev.sigev_tid = td->tid;
125 if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0)
127 td->timer_id = timerid;
128 pthread_barrier_wait(&args.b);
129 if (timerid < 0) return -1;
130 *res = (void *)(INTPTR_MIN | (uintptr_t)td>>1);