1 #include "pthread_impl.h"
6 pthread_t self = __pthread_self();
7 self->canceldisable = 1;
9 pthread_exit(PTHREAD_CANCELED);
12 long __syscall_cp_asm(volatile void *, syscall_arg_t,
13 syscall_arg_t, syscall_arg_t, syscall_arg_t,
14 syscall_arg_t, syscall_arg_t, syscall_arg_t);
16 long (__syscall_cp)(syscall_arg_t nr,
17 syscall_arg_t u, syscall_arg_t v, syscall_arg_t w,
18 syscall_arg_t x, syscall_arg_t y, syscall_arg_t z)
23 if (!libc.has_thread_pointer || (self = __pthread_self())->canceldisable)
24 return __syscall(nr, u, v, w, x, y, z);
26 r = __syscall_cp_asm(&self->cancel, nr, u, v, w, x, y, z);
27 if (r==-EINTR && nr!=SYS_close && self->cancel && !self->canceldisable)
32 static void _sigaddset(sigset_t *set, int sig)
35 set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1);
38 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
40 pthread_t self = __pthread_self();
42 const char *ip = ((char **)&uc->uc_mcontext)[CANCEL_REG_IP];
43 extern const char __cp_begin[1], __cp_end[1];
45 if (!self->cancel || self->canceldisable) return;
47 _sigaddset(&uc->uc_sigmask, SIGCANCEL);
49 if (self->cancelasync || ip >= __cp_begin && ip < __cp_end) {
50 self->canceldisable = 1;
51 pthread_sigmask(SIG_SETMASK, &uc->uc_sigmask, 0);
55 __syscall(SYS_tgkill, self->pid, self->tid, SIGCANCEL);
60 if (!libc.has_thread_pointer) return;
61 pthread_t self = pthread_self();
62 if (self->cancel && !self->canceldisable)
66 static void init_cancellation()
68 struct sigaction sa = {
69 .sa_flags = SA_SIGINFO | SA_RESTART,
70 .sa_sigaction = cancel_handler
72 sigfillset(&sa.sa_mask);
73 __libc_sigaction(SIGCANCEL, &sa, 0);
76 int pthread_cancel(pthread_t t)
83 a_store(&t->cancel, 1);
84 return pthread_kill(t, SIGCANCEL);