2 #include "pthread_impl.h"
7 __attribute__((__visibility__("hidden")))
9 long __cancel(), __cp_cancel(), __syscall_cp_asm(), __syscall_cp_c();
13 pthread_t self = __pthread_self();
14 if (self->canceldisable == PTHREAD_CANCEL_ENABLE || self->cancelasync)
15 pthread_exit(PTHREAD_CANCELED);
16 self->canceldisable = PTHREAD_CANCEL_DISABLE;
20 /* If __syscall_cp_asm has adjusted the stack pointer, it must provide a
21 * definition of __cp_cancel to undo those adjustments and call __cancel.
22 * Otherwise, __cancel provides a definition for __cp_cancel. */
24 weak_alias(__cancel, __cp_cancel);
26 long __syscall_cp_asm(volatile void *, syscall_arg_t,
27 syscall_arg_t, syscall_arg_t, syscall_arg_t,
28 syscall_arg_t, syscall_arg_t, syscall_arg_t);
30 long __syscall_cp_c(syscall_arg_t nr,
31 syscall_arg_t u, syscall_arg_t v, syscall_arg_t w,
32 syscall_arg_t x, syscall_arg_t y, syscall_arg_t z)
38 if ((st=(self=__pthread_self())->canceldisable)
39 && (st==PTHREAD_CANCEL_DISABLE || nr==SYS_close))
40 return __syscall(nr, u, v, w, x, y, z);
42 r = __syscall_cp_asm(&self->cancel, nr, u, v, w, x, y, z);
43 if (r==-EINTR && nr!=SYS_close && self->cancel &&
44 self->canceldisable != PTHREAD_CANCEL_DISABLE)
49 static void _sigaddset(sigset_t *set, int sig)
52 set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1);
56 __attribute__((__visibility__("hidden")))
58 extern const char __cp_begin[1], __cp_end[1];
60 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
62 pthread_t self = __pthread_self();
64 const char *ip = ((char **)&uc->uc_mcontext)[CANCEL_REG_IP];
67 if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return;
69 _sigaddset(&uc->uc_sigmask, SIGCANCEL);
71 if (self->cancelasync || ip >= __cp_begin && ip < __cp_end) {
72 ((char **)&uc->uc_mcontext)[CANCEL_REG_IP] = (char *)__cp_cancel;
76 __syscall(SYS_tkill, self->tid, SIGCANCEL);
81 pthread_t self = __pthread_self();
82 if (self->cancel && !self->canceldisable)
86 static void init_cancellation()
88 struct sigaction sa = {
89 .sa_flags = SA_SIGINFO | SA_RESTART,
90 .sa_sigaction = cancel_handler
92 memset(&sa.sa_mask, -1, _NSIG/8);
93 __libc_sigaction(SIGCANCEL, &sa, 0);
96 int pthread_cancel(pthread_t t)
103 a_store(&t->cancel, 1);
104 return pthread_kill(t, SIGCANCEL);