4 #include "pthread_impl.h"
8 union sigval sigev_value;
22 weak_alias(dummy_0, __pthread_tsd_run_dtors);
24 static void cleanup_fromsig(void *p)
26 pthread_t self = __pthread_self();
27 __pthread_tsd_run_dtors();
30 self->canceldisable = 0;
31 self->cancelasync = 0;
36 static void *start(void *arg)
38 pthread_t self = __pthread_self();
39 struct start_args *args = arg;
42 void (*notify)(union sigval) = args->sev->sigev_notify_function;
43 union sigval val = args->sev->sigev_value;
45 pthread_barrier_wait(&args->b);
48 while (sigwaitinfo(SIGTIMER_SET, &si) < 0);
49 if (si.si_code == SI_TIMER && !setjmp(jb)) {
50 pthread_cleanup_push(cleanup_fromsig, jb);
52 pthread_cleanup_pop(1);
54 if (self->timer_id < 0) break;
56 __syscall(SYS_timer_delete, self->timer_id & INT_MAX);
60 int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict res)
62 volatile static int init = 0;
66 struct start_args args;
67 struct ksigevent ksev, *ksevp=0;
71 switch (evp ? evp->sigev_notify : SIGEV_SIGNAL) {
76 ksev.sigev_value = evp->sigev_value;
77 ksev.sigev_signo = evp->sigev_signo;
78 ksev.sigev_notify = evp->sigev_notify;
79 if (evp->sigev_notify == SIGEV_THREAD_ID)
80 ksev.sigev_tid = evp->sigev_notify_thread_id;
85 if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0)
87 *res = (void *)(intptr_t)timerid;
91 struct sigaction sa = { .sa_handler = SIG_DFL };
92 __libc_sigaction(SIGTIMER, &sa, 0);
95 if (evp->sigev_notify_attributes)
96 attr = *evp->sigev_notify_attributes;
98 pthread_attr_init(&attr);
99 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
100 pthread_barrier_init(&args.b, 0, 2);
103 __block_app_sigs(&set);
104 __syscall(SYS_rt_sigprocmask, SIG_BLOCK, SIGTIMER_SET, 0, _NSIG/8);
105 r = pthread_create(&td, &attr, start, &args);
106 __restore_sigs(&set);
112 ksev.sigev_value.sival_ptr = 0;
113 ksev.sigev_signo = SIGTIMER;
114 ksev.sigev_notify = SIGEV_THREAD_ID;
115 ksev.sigev_tid = td->tid;
116 if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0)
118 td->timer_id = timerid;
119 pthread_barrier_wait(&args.b);
120 if (timerid < 0) return -1;
121 *res = (void *)(INTPTR_MIN | (uintptr_t)td>>1);