[337] | 1 | #include <time.h>
|
---|
| 2 | #include <setjmp.h>
|
---|
| 3 | #include "pthread_impl.h"
|
---|
| 4 |
|
---|
| 5 | struct ksigevent {
|
---|
| 6 | union sigval sigev_value;
|
---|
| 7 | int sigev_signo;
|
---|
| 8 | int sigev_notify;
|
---|
| 9 | int sigev_tid;
|
---|
| 10 | };
|
---|
| 11 |
|
---|
| 12 | struct start_args {
|
---|
| 13 | pthread_barrier_t b;
|
---|
| 14 | struct sigevent *sev;
|
---|
| 15 | };
|
---|
| 16 |
|
---|
| 17 | #ifndef __c2__
|
---|
| 18 | static void dummy_1(pthread_t self)
|
---|
| 19 | {
|
---|
| 20 | }
|
---|
| 21 | weak_alias(dummy_1, __pthread_tsd_run_dtors);
|
---|
| 22 | #else
|
---|
| 23 | extern void __pthread_tsd_run_dtors(pthread_t self);
|
---|
| 24 | #endif
|
---|
| 25 |
|
---|
| 26 | void __reset_tls();
|
---|
| 27 |
|
---|
| 28 | static void cleanup_fromsig(void *p)
|
---|
| 29 | {
|
---|
| 30 | pthread_t self = __pthread_self();
|
---|
| 31 | __pthread_tsd_run_dtors(self);
|
---|
| 32 | self->cancel = 0;
|
---|
| 33 | self->cancelbuf = 0;
|
---|
| 34 | self->canceldisable = 0;
|
---|
| 35 | self->cancelasync = 0;
|
---|
| 36 | self->unblock_cancel = 0;
|
---|
| 37 | __reset_tls();
|
---|
| 38 | longjmp(p, 1);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | static void timer_handler(int sig, siginfo_t *si, void *ctx)
|
---|
| 42 | {
|
---|
| 43 | pthread_t self = __pthread_self();
|
---|
| 44 | jmp_buf jb;
|
---|
| 45 | void (*notify)(union sigval) = (void (*)(union sigval))self->start;
|
---|
| 46 | union sigval val = { .sival_ptr = self->start_arg };
|
---|
| 47 |
|
---|
| 48 | if (!setjmp(jb) && si->si_code == SI_TIMER) {
|
---|
| 49 | pthread_cleanup_push(cleanup_fromsig, jb);
|
---|
| 50 | notify(val);
|
---|
| 51 | pthread_cleanup_pop(1);
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | static void install_handler()
|
---|
| 56 | {
|
---|
| 57 | struct sigaction sa = {
|
---|
| 58 | .sa_sigaction = timer_handler,
|
---|
| 59 | .sa_flags = SA_SIGINFO | SA_RESTART
|
---|
| 60 | };
|
---|
| 61 | __libc_sigaction(SIGTIMER, &sa, 0);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | static void *start(void *arg)
|
---|
| 65 | {
|
---|
| 66 | pthread_t self = __pthread_self();
|
---|
| 67 | struct start_args *args = arg;
|
---|
| 68 | int id;
|
---|
| 69 |
|
---|
| 70 | /* Reuse no-longer-needed thread structure fields to avoid
|
---|
| 71 | * needing the timer address in the signal handler. */
|
---|
| 72 | self->start = (void *(*)(void *))args->sev->sigev_notify_function;
|
---|
| 73 | self->start_arg = args->sev->sigev_value.sival_ptr;
|
---|
| 74 |
|
---|
| 75 | pthread_barrier_wait(&args->b);
|
---|
| 76 | if ((id = self->timer_id) >= 0) {
|
---|
| 77 | __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
|
---|
| 78 | SIGTIMER_SET, 0, _NSIG/8);
|
---|
| 79 | __wait(&self->timer_id, 0, id, 1);
|
---|
| 80 | __syscall(SYS_timer_delete, id);
|
---|
| 81 | }
|
---|
| 82 | return 0;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict res)
|
---|
| 86 | {
|
---|
| 87 | static pthread_once_t once = PTHREAD_ONCE_INIT;
|
---|
| 88 | pthread_t td;
|
---|
| 89 | pthread_attr_t attr;
|
---|
| 90 | int r;
|
---|
| 91 | struct start_args args;
|
---|
| 92 | struct ksigevent ksev, *ksevp=0;
|
---|
| 93 | int timerid;
|
---|
| 94 | sigset_t set;
|
---|
| 95 |
|
---|
| 96 | switch (evp ? evp->sigev_notify : SIGEV_SIGNAL) {
|
---|
| 97 | case SIGEV_NONE:
|
---|
| 98 | case SIGEV_SIGNAL:
|
---|
| 99 | if (evp) {
|
---|
| 100 | ksev.sigev_value = evp->sigev_value;
|
---|
| 101 | ksev.sigev_signo = evp->sigev_signo;
|
---|
| 102 | ksev.sigev_notify = evp->sigev_notify;
|
---|
| 103 | ksev.sigev_tid = 0;
|
---|
| 104 | ksevp = &ksev;
|
---|
| 105 | }
|
---|
| 106 | if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0)
|
---|
| 107 | return -1;
|
---|
| 108 | *res = (void *)(intptr_t)timerid;
|
---|
| 109 | break;
|
---|
| 110 | case SIGEV_THREAD:
|
---|
| 111 | pthread_once(&once, install_handler);
|
---|
| 112 | if (evp->sigev_notify_attributes)
|
---|
| 113 | attr = *evp->sigev_notify_attributes;
|
---|
| 114 | else
|
---|
| 115 | pthread_attr_init(&attr);
|
---|
| 116 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
---|
| 117 | pthread_barrier_init(&args.b, 0, 2);
|
---|
| 118 | args.sev = evp;
|
---|
| 119 |
|
---|
| 120 | __block_app_sigs(&set);
|
---|
| 121 | r = pthread_create(&td, &attr, start, &args);
|
---|
| 122 | __restore_sigs(&set);
|
---|
| 123 | if (r) {
|
---|
| 124 | errno = r;
|
---|
| 125 | return -1;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | ksev.sigev_value.sival_ptr = 0;
|
---|
| 129 | ksev.sigev_signo = SIGTIMER;
|
---|
| 130 | ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
|
---|
| 131 | ksev.sigev_tid = td->tid;
|
---|
| 132 | if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0)
|
---|
| 133 | timerid = -1;
|
---|
| 134 | td->timer_id = timerid;
|
---|
| 135 | pthread_barrier_wait(&args.b);
|
---|
| 136 | if (timerid < 0) return -1;
|
---|
| 137 | *res = (void *)(INTPTR_MIN | (uintptr_t)td>>1);
|
---|
| 138 | break;
|
---|
| 139 | default:
|
---|
| 140 | errno = EINVAL;
|
---|
| 141 | return -1;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | return 0;
|
---|
| 145 | }
|
---|