2 #include "pthread_impl.h"
5 union sigval sigev_value;
17 static void sighandler(int sig, siginfo_t *si, void *ctx)
20 pthread_t self = __pthread_self();
21 void (*notify)(union sigval) = (void (*)(union sigval))self->start;
22 union sigval val = { .sival_ptr = self->start_arg };
24 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &st);
26 pthread_setcancelstate(st, 0);
29 static void killtimer(void *arg)
32 if (t->timerid >= 0) __syscall(SYS_timer_delete, t->timerid);
35 static void *start(void *arg)
37 pthread_t self = __pthread_self();
38 struct start_args *args = arg;
39 struct __timer t = { .timerid = -1 };
41 /* Reuse no-longer-needed thread structure fields to avoid
42 * needing the timer address in the signal handler. */
43 self->start = (void *(*)(void *))args->sev->sigev_notify_function;
44 self->start_arg = args->sev->sigev_value.sival_ptr;
47 pthread_cleanup_push(killtimer, &t);
48 pthread_barrier_wait(&args->b);
49 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
50 /* Loop on async-signal-safe cancellation point */
52 pthread_cleanup_pop(1);
56 int timer_create(clockid_t clk, struct sigevent *evp, timer_t *res)
58 struct sigevent sev = {
59 .sigev_notify = SIGEV_SIGNAL,
60 .sigev_signo = SIGALRM
65 struct start_args args;
67 struct ksigevent ksev;
72 switch (sev.sigev_notify) {
75 ksev.sigev_value = evp ? sev.sigev_value
76 : (union sigval){.sival_ptr=t};
77 ksev.sigev_signo = sev.sigev_signo;
78 ksev.sigev_notify = sev.sigev_notify;
80 if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0)
82 if (!(t = calloc(1, sizeof *t))) {
83 syscall(SYS_timer_delete, timerid);
89 if (!libc.sigtimer) libc.sigtimer = sighandler;
90 if (sev.sigev_notify_attributes)
91 attr = *sev.sigev_notify_attributes;
93 pthread_attr_init(&attr);
94 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
95 pthread_barrier_init(&args.b, 0, 2);
97 r = pthread_create(&td, &attr, start, &args);
102 ksev.sigev_value.sival_ptr = 0;
103 ksev.sigev_signo = SIGCANCEL;
104 ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
105 ksev.sigev_tid = td->tid;
106 r = syscall(SYS_timer_create, clk, &ksev, &timerid);
107 pthread_barrier_wait(&args.b);
113 t->timerid = timerid;