mark mips cancellable syscall code as code
[musl] / src / thread / pthread_cancel.c
1 #include <string.h>
2 #include "pthread_impl.h"
3 #include "syscall.h"
4 #include "libc.h"
5
6 #ifdef SHARED
7 __attribute__((__visibility__("hidden")))
8 #endif
9 long __cancel(), __cp_cancel(), __syscall_cp_asm(), __syscall_cp_c();
10
11 long __cancel()
12 {
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;
17         return -ECANCELED;
18 }
19
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. */
23
24 weak_alias(__cancel, __cp_cancel);
25
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);
29
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)
33 {
34         pthread_t self;
35         long r;
36         int st;
37
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);
41
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)
45                 r = __cancel();
46         return r;
47 }
48
49 static void _sigaddset(sigset_t *set, int sig)
50 {
51         unsigned s = sig-1;
52         set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1);
53 }
54
55 #ifdef SHARED
56 __attribute__((__visibility__("hidden")))
57 #endif
58 extern const char __cp_begin[1], __cp_end[1];
59
60 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
61 {
62         pthread_t self = __pthread_self();
63         ucontext_t *uc = ctx;
64         const char *ip = ((char **)&uc->uc_mcontext)[CANCEL_REG_IP];
65
66         a_barrier();
67         if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return;
68
69         _sigaddset(&uc->uc_sigmask, SIGCANCEL);
70
71         if (self->cancelasync || ip >= __cp_begin && ip < __cp_end) {
72                 ((char **)&uc->uc_mcontext)[CANCEL_REG_IP] = (char *)__cp_cancel;
73                 return;
74         }
75
76         __syscall(SYS_tkill, self->tid, SIGCANCEL);
77 }
78
79 void __testcancel()
80 {
81         pthread_t self = __pthread_self();
82         if (self->cancel && !self->canceldisable)
83                 __cancel();
84 }
85
86 static void init_cancellation()
87 {
88         struct sigaction sa = {
89                 .sa_flags = SA_SIGINFO | SA_RESTART,
90                 .sa_sigaction = cancel_handler
91         };
92         memset(&sa.sa_mask, -1, _NSIG/8);
93         __libc_sigaction(SIGCANCEL, &sa, 0);
94 }
95
96 int pthread_cancel(pthread_t t)
97 {
98         static int init;
99         if (!init) {
100                 init_cancellation();
101                 init = 1;
102         }
103         a_store(&t->cancel, 1);
104         return pthread_kill(t, SIGCANCEL);
105 }