3 #include "pthread_impl.h"
8 __attribute__((__visibility__("hidden")))
10 long __cancel(), __syscall_cp_asm(), __syscall_cp_c();
14 pthread_t self = __pthread_self();
15 if (self->canceldisable == PTHREAD_CANCEL_ENABLE || self->cancelasync)
16 pthread_exit(PTHREAD_CANCELED);
17 self->canceldisable = PTHREAD_CANCEL_DISABLE;
21 long __syscall_cp_asm(volatile void *, syscall_arg_t,
22 syscall_arg_t, syscall_arg_t, syscall_arg_t,
23 syscall_arg_t, syscall_arg_t, syscall_arg_t);
25 long __syscall_cp_c(syscall_arg_t nr,
26 syscall_arg_t u, syscall_arg_t v, syscall_arg_t w,
27 syscall_arg_t x, syscall_arg_t y, syscall_arg_t z)
33 if ((st=(self=__pthread_self())->canceldisable)
34 && (st==PTHREAD_CANCEL_DISABLE || nr==SYS_close))
35 return __syscall(nr, u, v, w, x, y, z);
37 r = __syscall_cp_asm(&self->cancel, nr, u, v, w, x, y, z);
38 if (r==-EINTR && nr!=SYS_close && self->cancel &&
39 self->canceldisable != PTHREAD_CANCEL_DISABLE)
44 static void _sigaddset(sigset_t *set, int sig)
47 set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1);
51 __attribute__((__visibility__("hidden")))
53 extern const char __cp_begin[1], __cp_end[1], __cp_cancel[1];
55 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
57 pthread_t self = __pthread_self();
59 uintptr_t pc = uc->uc_mcontext.MC_PC;
62 if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return;
64 _sigaddset(&uc->uc_sigmask, SIGCANCEL);
66 if (self->cancelasync || pc >= (uintptr_t)__cp_begin && pc < (uintptr_t)__cp_end) {
67 uc->uc_mcontext.MC_PC = (uintptr_t)__cp_cancel;
71 __syscall(SYS_tkill, self->tid, SIGCANCEL);
76 pthread_t self = __pthread_self();
77 if (self->cancel && !self->canceldisable)
81 static void init_cancellation()
83 struct sigaction sa = {
84 .sa_flags = SA_SIGINFO | SA_RESTART,
85 .sa_sigaction = cancel_handler
87 memset(&sa.sa_mask, -1, _NSIG/8);
88 __libc_sigaction(SIGCANCEL, &sa, 0);
91 int pthread_cancel(pthread_t t)
98 a_store(&t->cancel, 1);
99 return pthread_kill(t, SIGCANCEL);