1 #include "pthread_impl.h"
3 long __syscall_cp_asm(volatile void *, long, long, long, long, long, long, long);
5 long (__syscall_cp)(long nr, long u, long v, long w, long x, long y, long z)
8 uintptr_t old_sp, old_ip;
11 if (!libc.lock || (self = __pthread_self())->canceldisable)
12 return __syscall(nr, u, v, w, x, y, z);
18 r = __syscall_cp_asm(&self->cp_sp, nr, u, v, w, x, y, z);
21 if (r == -EINTR && self->cancel) pthread_exit(PTHREAD_CANCELED);
25 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
27 pthread_t self = __pthread_self();
29 uintptr_t sp = ((uintptr_t *)&uc->uc_mcontext)[CANCEL_REG_SP];
30 uintptr_t ip = ((uintptr_t *)&uc->uc_mcontext)[CANCEL_REG_IP];
32 if (!self->cancel || self->canceldisable) return;
34 if (self->cancelasync) pthread_exit(PTHREAD_CANCELED);
36 if (sp != self->cp_sp) {
38 sigaddset(&uc->uc_sigmask, SIGCANCEL);
39 __syscall(SYS_tgkill, self->pid, self->tid, SIGCANCEL);
43 if (ip <= self->cp_ip) pthread_exit(PTHREAD_CANCELED);
46 static void testcancel()
48 pthread_t self = __pthread_self();
49 if (self->cancel && !self->canceldisable)
50 pthread_exit(PTHREAD_CANCELED);
53 static void init_cancellation()
55 struct sigaction sa = {
56 .sa_flags = SA_SIGINFO | SA_RESTART,
57 .sa_sigaction = cancel_handler
59 sigfillset(&sa.sa_mask);
60 __libc_sigaction(SIGCANCEL, &sa, 0);
61 libc.testcancel = testcancel;
64 int pthread_cancel(pthread_t t)
66 static pthread_once_t once;
67 pthread_once(&once, init_cancellation);
68 a_store(&t->cancel, 1);
69 return pthread_kill(t, SIGCANCEL);