2 #include "pthread_impl.h"
5 union sigval sigev_value;
16 static void sighandler(int sig, siginfo_t *si, void *ctx)
19 pthread_t self = __pthread_self();
20 void (*notify)(union sigval) = (void (*)(union sigval))self->start;
21 union sigval val = { .sival_ptr = self->start_arg };
23 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &st);
25 pthread_setcancelstate(st, 0);
28 static void *start(void *arg)
30 pthread_t self = __pthread_self();
31 struct start_args *args = arg;
33 /* Reuse no-longer-needed thread structure fields to avoid
34 * needing the timer address in the signal handler. */
35 self->start = (void *(*)(void *))args->sev->sigev_notify_function;
36 self->start_arg = args->sev->sigev_value.sival_ptr;
37 self->result = (void *)-1;
39 pthread_barrier_wait(&args->b);
40 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
41 /* Loop on async-signal-safe cancellation point */
46 int timer_create(clockid_t clk, struct sigevent *evp, timer_t *res)
48 struct sigevent sev = {
49 .sigev_notify = SIGEV_SIGNAL,
50 .sigev_signo = SIGALRM
55 struct start_args args;
57 struct ksigevent ksev;
62 switch (sev.sigev_notify) {
65 ksev.sigev_value = evp ? sev.sigev_value
66 : (union sigval){.sival_ptr=t};
67 ksev.sigev_signo = sev.sigev_signo;
68 ksev.sigev_notify = sev.sigev_notify;
70 if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0)
72 *res = (void *)(2*timerid+1);
75 if (!libc.sigtimer) libc.sigtimer = sighandler;
76 if (sev.sigev_notify_attributes)
77 attr = *sev.sigev_notify_attributes;
79 pthread_attr_init(&attr);
80 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
81 pthread_barrier_init(&args.b, 0, 2);
83 r = pthread_create(&td, &attr, start, &args);
88 ksev.sigev_value.sival_ptr = 0;
89 ksev.sigev_signo = SIGCANCEL;
90 ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
91 ksev.sigev_tid = td->tid;
92 r = syscall(SYS_timer_create, clk, &ksev, &timerid);
93 pthread_barrier_wait(&args.b);
98 td->result = (void *)timerid;