1b71aa423399220f70a0a35066af8b04d8d0339a
[musl] / src / thread / pthread_cancel.c
1 #include "pthread_impl.h"
2 #include "syscall.h"
3 #include "libc.h"
4
5 long __cancel()
6 {
7         pthread_t self = __pthread_self();
8         if (self->canceldisable == PTHREAD_CANCEL_ENABLE || self->cancelasync)
9                 pthread_exit(PTHREAD_CANCELED);
10         self->canceldisable = PTHREAD_CANCEL_DISABLE;
11         return -ECANCELED;
12 }
13
14 /* If __syscall_cp_asm has adjusted the stack pointer, it must provide a
15  * definition of __cp_cancel to undo those adjustments and call __cancel.
16  * Otherwise, __cancel provides a definition for __cp_cancel. */
17
18 weak_alias(__cancel, __cp_cancel);
19
20 long __syscall_cp_asm(volatile void *, syscall_arg_t,
21                       syscall_arg_t, syscall_arg_t, syscall_arg_t,
22                       syscall_arg_t, syscall_arg_t, syscall_arg_t);
23
24 long __syscall_cp_c(syscall_arg_t nr,
25                     syscall_arg_t u, syscall_arg_t v, syscall_arg_t w,
26                     syscall_arg_t x, syscall_arg_t y, syscall_arg_t z)
27 {
28         pthread_t self;
29         long r;
30         int st;
31
32         if (!libc.has_thread_pointer || (st=(self=__pthread_self())->canceldisable)
33             && (st==PTHREAD_CANCEL_DISABLE || nr==SYS_close))
34                 return __syscall(nr, u, v, w, x, y, z);
35
36         r = __syscall_cp_asm(&self->cancel, nr, u, v, w, x, y, z);
37         if (r==-EINTR && nr!=SYS_close && self->cancel &&
38             self->canceldisable != PTHREAD_CANCEL_DISABLE)
39                 r = __cancel();
40         return r;
41 }
42
43 static void _sigaddset(sigset_t *set, int sig)
44 {
45         unsigned s = sig-1;
46         set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1);
47 }
48
49 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
50 {
51         pthread_t self = __pthread_self();
52         ucontext_t *uc = ctx;
53         const char *ip = ((char **)&uc->uc_mcontext)[CANCEL_REG_IP];
54         extern const char __cp_begin[1], __cp_end[1];
55
56         a_barrier();
57         if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return;
58
59         _sigaddset(&uc->uc_sigmask, SIGCANCEL);
60
61         if (self->cancelasync || ip >= __cp_begin && ip < __cp_end) {
62                 ((char **)&uc->uc_mcontext)[CANCEL_REG_IP] = (char *)__cp_cancel;
63                 return;
64         }
65
66         __syscall(SYS_tkill, self->tid, SIGCANCEL);
67 }
68
69 void __testcancel()
70 {
71         if (!libc.has_thread_pointer) return;
72         pthread_t self = __pthread_self();
73         if (self->cancel && !self->canceldisable)
74                 __cancel();
75 }
76
77 static void init_cancellation()
78 {
79         struct sigaction sa = {
80                 .sa_flags = SA_SIGINFO | SA_RESTART,
81                 .sa_sigaction = cancel_handler
82         };
83         sigfillset(&sa.sa_mask);
84         __libc_sigaction(SIGCANCEL, &sa, 0);
85 }
86
87 int pthread_cancel(pthread_t t)
88 {
89         static int init;
90         if (!init) {
91                 init_cancellation();
92                 init = 1;
93         }
94         a_store(&t->cancel, 1);
95         return pthread_kill(t, SIGCANCEL);
96 }