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) {
75 ksev.sigev_value = evp->sigev_value;
76 ksev.sigev_signo = evp->sigev_signo;
77 ksev.sigev_notify = evp->sigev_notify;
81 if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0)
83 *res = (void *)(intptr_t)timerid;
87 struct sigaction sa = { .sa_handler = SIG_DFL };
88 __libc_sigaction(SIGTIMER, &sa, 0);
91 if (evp->sigev_notify_attributes)
92 attr = *evp->sigev_notify_attributes;
94 pthread_attr_init(&attr);
95 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
96 pthread_barrier_init(&args.b, 0, 2);
99 __block_app_sigs(&set);
100 __syscall(SYS_rt_sigprocmask, SIG_BLOCK, SIGTIMER_SET, 0, _NSIG/8);
101 r = pthread_create(&td, &attr, start, &args);
102 __restore_sigs(&set);
108 ksev.sigev_value.sival_ptr = 0;
109 ksev.sigev_signo = SIGTIMER;
110 ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
111 ksev.sigev_tid = td->tid;
112 if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0)
114 td->timer_id = timerid;
115 pthread_barrier_wait(&args.b);
116 if (timerid < 0) return -1;
117 *res = (void *)(INTPTR_MIN | (uintptr_t)td>>1);