1 #include "pthread_impl.h"
5 pthread_t self = __pthread_self();
7 self->canceldisable = 1;
9 pthread_exit(PTHREAD_CANCELED);
12 long __syscall_cp_asm(volatile void *, long, long, long, long, long, long, long);
14 long (__syscall_cp)(long nr, long u, long v, long w, long x, long y, long z)
17 uintptr_t old_sp, old_ip;
20 if (!libc.threaded || (self = __pthread_self())->canceldisable)
21 return __syscall(nr, u, v, w, x, y, z);
27 r = __syscall_cp_asm(&self->cp_sp, nr, u, v, w, x, y, z);
30 if (r == -EINTR && self->cancel && !self->canceldisable) __cancel();
34 static void _sigaddset(sigset_t *set, int sig)
37 set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1);
40 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
42 pthread_t self = __pthread_self();
44 uintptr_t sp = ((uintptr_t *)&uc->uc_mcontext)[CANCEL_REG_SP];
45 uintptr_t ip = ((uintptr_t *)&uc->uc_mcontext)[CANCEL_REG_IP];
47 if (!self->cancel || self->canceldisable) return;
49 _sigaddset(&uc->uc_sigmask, SIGCANCEL);
51 if (self->cancelasync || sp == self->cp_sp && ip <= self->cp_ip) {
52 self->canceldisable = 1;
53 pthread_sigmask(SIG_SETMASK, &uc->uc_sigmask, 0);
58 __syscall(SYS_tgkill, self->pid, self->tid, SIGCANCEL);
63 pthread_t self = __pthread_self();
64 if (self->cancel && !self->canceldisable)
68 static void init_cancellation()
70 struct sigaction sa = {
71 .sa_flags = SA_SIGINFO | SA_RESTART,
72 .sa_sigaction = cancel_handler
74 sigfillset(&sa.sa_mask);
75 __libc_sigaction(SIGCANCEL, &sa, 0);
78 int pthread_cancel(pthread_t t)
85 a_store(&t->cancel, 1);
86 return pthread_kill(t, SIGCANCEL);