• 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 hidden long __cancel(), __syscall_cp_asm(), __syscall_cp_c();
7 
__cancel()8 long __cancel()
9 {
10 	pthread_t self = __pthread_self();
11 	if (self->canceldisable == PTHREAD_CANCEL_ENABLE || self->cancelasync)
12 		pthread_exit(PTHREAD_CANCELED);
13 	self->canceldisable = PTHREAD_CANCEL_DISABLE;
14 	return -ECANCELED;
15 }
16 
17 long __syscall_cp_asm(volatile void *, syscall_arg_t,
18                       syscall_arg_t, syscall_arg_t, syscall_arg_t,
19                       syscall_arg_t, syscall_arg_t, syscall_arg_t);
20 
__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)21 long __syscall_cp_c(syscall_arg_t nr,
22                     syscall_arg_t u, syscall_arg_t v, syscall_arg_t w,
23                     syscall_arg_t x, syscall_arg_t y, syscall_arg_t z)
24 {
25 	pthread_t self;
26 	long r;
27 	int st;
28 
29 	if ((st=(self=__pthread_self())->canceldisable)
30 	    && (st==PTHREAD_CANCEL_DISABLE || nr==SYS_close))
31 		return __syscall(nr, u, v, w, x, y, z);
32 
33 	r = __syscall_cp_asm(&self->cancel, nr, u, v, w, x, y, z);
34 	if (r==-EINTR && nr!=SYS_close && self->cancel &&
35 	    self->canceldisable != PTHREAD_CANCEL_DISABLE)
36 		r = __cancel();
37 	return r;
38 }
39 
_sigaddset(sigset_t * set,int sig)40 static void _sigaddset(sigset_t *set, int sig)
41 {
42 	unsigned s = sig-1;
43 	set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1);
44 }
45 
46 extern hidden const char __cp_begin[1], __cp_end[1], __cp_cancel[1];
47 
cancel_handler(int sig,siginfo_t * si,void * ctx)48 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
49 {
50 	pthread_t self = __pthread_self();
51 	a_barrier();
52 	if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return;
53 	if(self->cancelasync){
54 		pthread_exit(PTHREAD_CANCELED);
55 	}
56 }
57 
__testcancel()58 void __testcancel()
59 {
60 	pthread_t self = __pthread_self();
61 	if (self->cancel && !self->canceldisable)
62 		__cancel();
63 }
64 
init_cancellation()65 static void init_cancellation()
66 {
67 	struct sigaction sa = {
68 		.sa_flags = SA_SIGINFO | SA_RESTART,
69 		.sa_sigaction = cancel_handler
70 	};
71 	memset(&sa.sa_mask, -1, _NSIG/8);
72 	__libc_sigaction(SIGCANCEL, &sa, 0);
73 }
74 
pthread_cancel(pthread_t t)75 int pthread_cancel(pthread_t t)
76 {
77 	static int init;
78 	if (!init) {
79 		init_cancellation();
80 		init = 1;
81 	}
82 	a_store(&t->cancel, 1);
83 	if (t == pthread_self()) {
84 		if (t->canceldisable == PTHREAD_CANCEL_ENABLE && t->cancelasync)
85 			pthread_exit(PTHREAD_CANCELED);
86 		return 0;
87 	}
88 	return pthread_kill(t, SIGCANCEL);
89 }
90