2 #include "pthread_impl.h"
5 union sigval sigev_value;
16 void __sigtimer_handler(pthread_t self)
19 void (*notify)(union sigval) = (void (*)(union sigval))self->start;
20 union sigval val = { .sival_ptr = self->start_arg };
22 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &st);
24 pthread_setcancelstate(st, 0);
27 static void cleanup(void *p)
30 __syscall(SYS_timer_delete, self->result);
33 static void *start(void *arg)
35 pthread_t self = __pthread_self();
36 struct start_args *args = arg;
38 /* Reuse no-longer-needed thread structure fields to avoid
39 * needing the timer address in the signal handler. */
40 self->start = (void *(*)(void *))args->sev->sigev_notify_function;
41 self->start_arg = args->sev->sigev_value.sival_ptr;
42 self->result = (void *)-1;
44 pthread_cleanup_push(cleanup, self);
45 pthread_barrier_wait(&args->b);
46 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
47 /* Loop on async-signal-safe cancellation point */
49 pthread_cleanup_pop(0);
53 int timer_create(clockid_t clk, struct sigevent *evp, timer_t *res)
55 struct sigevent sev = {
56 .sigev_notify = SIGEV_SIGNAL,
57 .sigev_signo = SIGALRM
62 struct start_args args;
64 struct ksigevent ksev;
69 switch (sev.sigev_notify) {
72 ksev.sigev_value = evp ? sev.sigev_value
73 : (union sigval){.sival_ptr=t};
74 ksev.sigev_signo = sev.sigev_signo;
75 ksev.sigev_notify = sev.sigev_notify;
77 if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0)
79 *res = (void *)(2*timerid+1);
82 if (sev.sigev_notify_attributes)
83 attr = *sev.sigev_notify_attributes;
85 pthread_attr_init(&attr);
86 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
87 pthread_barrier_init(&args.b, 0, 2);
89 r = pthread_create(&td, &attr, start, &args);
94 ksev.sigev_value.sival_ptr = 0;
95 ksev.sigev_signo = SIGCANCEL;
96 ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
97 ksev.sigev_tid = td->tid;
98 r = syscall(SYS_timer_create, clk, &ksev, &timerid);
99 pthread_barrier_wait(&args.b);
104 td->result = (void *)timerid;