1 #include "pthread_impl.h"
5 pthread_t self = __pthread_self();
6 self->canceldisable = 1;
8 pthread_exit(PTHREAD_CANCELED);
11 long __syscall_cp_asm(volatile void *, long, long, long, long, long, long, long);
13 long (__syscall_cp)(long nr, long u, long v, long w, long x, long y, long z)
16 uintptr_t old_sp, old_ip;
19 if (!libc.lock || (self = __pthread_self())->canceldisable)
20 return __syscall(nr, u, v, w, x, y, z);
26 r = __syscall_cp_asm(&self->cp_sp, nr, u, v, w, x, y, z);
29 if (r == -EINTR && self->cancel) __cancel();
33 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
35 pthread_t self = __pthread_self();
37 uintptr_t sp = ((uintptr_t *)&uc->uc_mcontext)[CANCEL_REG_SP];
38 uintptr_t ip = ((uintptr_t *)&uc->uc_mcontext)[CANCEL_REG_IP];
40 if (!self->cancel || self->canceldisable) return;
42 sigaddset(&uc->uc_sigmask, SIGCANCEL);
44 if (self->cancelasync || sp == self->cp_sp && ip <= self->cp_ip) {
45 self->canceldisable = 1;
46 pthread_sigmask(SIG_SETMASK, &uc->uc_sigmask, 0);
51 __syscall(SYS_tgkill, self->pid, self->tid, SIGCANCEL);
54 static void testcancel()
56 pthread_t self = __pthread_self();
57 if (self->cancel && !self->canceldisable)
61 static void init_cancellation()
63 struct sigaction sa = {
64 .sa_flags = SA_SIGINFO | SA_RESTART,
65 .sa_sigaction = cancel_handler
67 sigfillset(&sa.sa_mask);
68 __libc_sigaction(SIGCANCEL, &sa, 0);
69 libc.testcancel = testcancel;
72 int pthread_cancel(pthread_t t)
74 static pthread_once_t once;
75 pthread_once(&once, init_cancellation);
76 a_store(&t->cancel, 1);
77 return pthread_kill(t, SIGCANCEL);