2 #include "pthread_impl.h"
5 union sigval sigev_value;
16 static void dummy_1(pthread_t self)
19 weak_alias(dummy_1, __pthread_tsd_run_dtors);
21 static void cleanup_fromsig(void *p)
23 pthread_t self = __pthread_self();
24 __pthread_tsd_run_dtors(self);
27 self->canceldisable = 0;
28 self->cancelasync = 0;
29 self->unblock_cancel = 0;
33 static void timer_handler(int sig, siginfo_t *si, void *ctx)
35 pthread_t self = __pthread_self();
37 void (*notify)(union sigval) = (void (*)(union sigval))self->start;
38 union sigval val = { .sival_ptr = self->start_arg };
40 if (!setjmp(jb) && si->si_code == SI_TIMER) {
41 pthread_cleanup_push(cleanup_fromsig, jb);
43 pthread_cleanup_pop(1);
47 static void install_handler()
49 struct sigaction sa = {
50 .sa_sigaction = timer_handler,
51 .sa_flags = SA_SIGINFO | SA_RESTART
53 __libc_sigaction(SIGTIMER, &sa, 0);
54 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
55 SIGTIMER_SET, 0, __SYSCALL_SSLEN);
58 static void *start(void *arg)
60 pthread_t self = __pthread_self();
61 struct start_args *args = arg;
64 /* Reuse no-longer-needed thread structure fields to avoid
65 * needing the timer address in the signal handler. */
66 self->start = (void *(*)(void *))args->sev->sigev_notify_function;
67 self->start_arg = args->sev->sigev_value.sival_ptr;
68 self->result = (void *)-1;
71 pthread_sigmask(SIG_BLOCK, &set, 0);
73 pthread_barrier_wait(&args->b);
74 __wait(&self->delete_timer, 0, 0, 1);
75 __syscall(SYS_timer_delete, self->result);
79 int timer_create(clockid_t clk, struct sigevent *evp, timer_t *res)
81 static pthread_once_t once = PTHREAD_ONCE_INIT;
85 struct start_args args;
86 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 *)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);
112 r = pthread_create(&td, &attr, start, &args);
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 r = syscall(SYS_timer_create, clk, &ksev, &timerid);
122 pthread_barrier_wait(&args.b);
127 td->result = (void *)timerid;