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, __SYSCALL_SSLEN);
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;
69 self->result = (void *)-1;
72 pthread_sigmask(SIG_BLOCK, &set, 0);
74 pthread_barrier_wait(&args->b);
75 __wait(&self->delete_timer, 0, 0, 1);
76 __syscall(SYS_timer_delete, self->result);
80 int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict res)
82 static pthread_once_t once = PTHREAD_ONCE_INIT;
86 struct start_args args;
87 struct ksigevent ksev, *ksevp=0;
90 switch (evp ? evp->sigev_notify : SIGEV_SIGNAL) {
94 ksev.sigev_value = evp->sigev_value;
95 ksev.sigev_signo = evp->sigev_signo;
96 ksev.sigev_notify = evp->sigev_notify;
100 if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0)
102 *res = (void *)timerid;
105 pthread_once(&once, install_handler);
106 if (evp->sigev_notify_attributes)
107 attr = *evp->sigev_notify_attributes;
109 pthread_attr_init(&attr);
110 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
111 pthread_barrier_init(&args.b, 0, 2);
113 r = pthread_create(&td, &attr, start, &args);
118 ksev.sigev_value.sival_ptr = 0;
119 ksev.sigev_signo = SIGTIMER;
120 ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
121 ksev.sigev_tid = td->tid;
122 r = syscall(SYS_timer_create, clk, &ksev, &timerid);
123 pthread_barrier_wait(&args.b);
128 td->result = (void *)timerid;