• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define _GNU_SOURCE
2 #include <string.h>
3 #include "pthread_impl.h"
4 #include "syscall.h"
5 
6 #ifdef FEATURE_PTHREAD_CANCEL
7 hidden long __cancel(), __syscall_cp_asm(), __syscall_cp_c();
8 
__cancel()9 long __cancel()
10 {
11 	pthread_t self = __pthread_self();
12 	if (self->canceldisable == PTHREAD_CANCEL_ENABLE || self->cancelasync)
13 		pthread_exit(PTHREAD_CANCELED);
14 	self->canceldisable = PTHREAD_CANCEL_DISABLE;
15 	return -ECANCELED;
16 }
17 
18 long __syscall_cp_asm(volatile void *, syscall_arg_t,
19                       syscall_arg_t, syscall_arg_t, syscall_arg_t,
20                       syscall_arg_t, syscall_arg_t, syscall_arg_t);
21 
__syscall_cp_c(syscall_arg_t nr,syscall_arg_t u,syscall_arg_t v,syscall_arg_t w,syscall_arg_t x,syscall_arg_t y,syscall_arg_t z)22 long __syscall_cp_c(syscall_arg_t nr,
23                     syscall_arg_t u, syscall_arg_t v, syscall_arg_t w,
24                     syscall_arg_t x, syscall_arg_t y, syscall_arg_t z)
25 {
26 	pthread_t self;
27 	long r;
28 	int st;
29 
30 	if ((st=(self=__pthread_self())->canceldisable)
31 	    && (st==PTHREAD_CANCEL_DISABLE || nr==SYS_close))
32 		return __syscall(nr, u, v, w, x, y, z);
33 
34 	r = __syscall_cp_asm(&self->cancel, nr, u, v, w, x, y, z);
35 	if (r==-EINTR && nr!=SYS_close && self->cancel &&
36 	    self->canceldisable != PTHREAD_CANCEL_DISABLE)
37 		r = __cancel();
38 	return r;
39 }
40 
_sigaddset(sigset_t * set,int sig)41 static void _sigaddset(sigset_t *set, int sig)
42 {
43 	unsigned s = sig-1;
44 	set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1);
45 }
46 
47 extern hidden const char __cp_begin[1], __cp_end[1], __cp_cancel[1];
48 
cancel_handler(int sig,siginfo_t * si,void * ctx)49 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
50 {
51 	pthread_t self = __pthread_self();
52 #ifndef __LITEOS_A__
53 	ucontext_t *uc = ctx;
54 	uintptr_t pc = uc->uc_mcontext.MC_PC;
55 #endif
56 
57 	a_barrier();
58 	if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return;
59 
60 #ifdef __LITEOS_A__
61 	if (self->cancelasync) {
62 		pthread_exit(PTHREAD_CANCELED);
63 	}
64 #else
65 	_sigaddset(&uc->uc_sigmask, SIGCANCEL);
66 
67 	if (self->cancelasync) {
68 		pthread_sigmask(SIG_SETMASK, &uc->uc_sigmask, 0);
69 		__cancel();
70 	}
71 
72 	if (pc >= (uintptr_t)__cp_begin && pc < (uintptr_t)__cp_end) {
73 		uc->uc_mcontext.MC_PC = (uintptr_t)__cp_cancel;
74 #ifdef CANCEL_GOT
75 		uc->uc_mcontext.MC_GOT = CANCEL_GOT;
76 #endif
77 		return;
78 	}
79 	__syscall(SYS_tkill, self->tid, SIGCANCEL);
80 #endif
81 }
82 
__testcancel()83 void __testcancel()
84 {
85 	pthread_t self = __pthread_self();
86 	if (self->cancel && !self->canceldisable)
87 		__cancel();
88 }
89 
init_cancellation()90 static void init_cancellation()
91 {
92 	struct sigaction sa = {
93 		.sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK,
94 		.sa_sigaction = cancel_handler
95 	};
96 	memset(&sa.sa_mask, -1, _NSIG/8);
97 	__libc_sigaction(SIGCANCEL, &sa, 0);
98 }
99 
pthread_cancel(pthread_t t)100 int pthread_cancel(pthread_t t)
101 {
102 	static int init;
103 	if (!init) {
104 		init_cancellation();
105 		init = 1;
106 	}
107 	a_store(&t->cancel, 1);
108 	if (t == pthread_self()) {
109 		if (t->canceldisable == PTHREAD_CANCEL_ENABLE && t->cancelasync)
110 			pthread_exit(PTHREAD_CANCELED);
111 		return 0;
112 	}
113 	return pthread_kill(t, SIGCANCEL);
114 }
115 #else
__cancel()116 hidden long __cancel()
117 {
118 }
119 
pthread_cancel(pthread_t t)120 int pthread_cancel(pthread_t t)
121 {
122 }
123 #endif
124