pthread_cancel.c 2.7 KB
Newer Older
1
#include <string.h>
R
Rich Felker 已提交
2
#include "pthread_impl.h"
3
#include "syscall.h"
4
#include "libc.h"
R
Rich Felker 已提交
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
{
R
Rich Felker 已提交
13 14 15 16 17
	pthread_t self = __pthread_self();
	if (self->canceldisable == PTHREAD_CANCEL_ENABLE || self->cancelasync)
		pthread_exit(PTHREAD_CANCELED);
	self->canceldisable = PTHREAD_CANCEL_DISABLE;
	return -ECANCELED;
18 19
}

20 21 22 23
/* If __syscall_cp_asm has adjusted the stack pointer, it must provide a
 * definition of __cp_cancel to undo those adjustments and call __cancel.
 * Otherwise, __cancel provides a definition for __cp_cancel. */

24
weak_alias(__cancel, __cp_cancel);
25

26 27 28
long __syscall_cp_asm(volatile void *, syscall_arg_t,
                      syscall_arg_t, syscall_arg_t, syscall_arg_t,
                      syscall_arg_t, syscall_arg_t, syscall_arg_t);
R
Rich Felker 已提交
29

30
long __syscall_cp_c(syscall_arg_t nr,
31 32
                    syscall_arg_t u, syscall_arg_t v, syscall_arg_t w,
                    syscall_arg_t x, syscall_arg_t y, syscall_arg_t z)
R
Rich Felker 已提交
33 34 35
{
	pthread_t self;
	long r;
R
Rich Felker 已提交
36
	int st;
R
Rich Felker 已提交
37

38
	if ((st=(self=__pthread_self())->canceldisable)
R
Rich Felker 已提交
39
	    && (st==PTHREAD_CANCEL_DISABLE || nr==SYS_close))
R
Rich Felker 已提交
40 41
		return __syscall(nr, u, v, w, x, y, z);

42
	r = __syscall_cp_asm(&self->cancel, nr, u, v, w, x, y, z);
R
Rich Felker 已提交
43 44 45
	if (r==-EINTR && nr!=SYS_close && self->cancel &&
	    self->canceldisable != PTHREAD_CANCEL_DISABLE)
		r = __cancel();
R
Rich Felker 已提交
46 47 48
	return r;
}

49 50 51 52 53 54
static void _sigaddset(sigset_t *set, int sig)
{
	unsigned s = sig-1;
	set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1);
}

55 56 57 58 59
#ifdef SHARED
__attribute__((__visibility__("hidden")))
#endif
extern const char __cp_begin[1], __cp_end[1];

R
Rich Felker 已提交
60 61 62 63
static void cancel_handler(int sig, siginfo_t *si, void *ctx)
{
	pthread_t self = __pthread_self();
	ucontext_t *uc = ctx;
64
	const char *ip = ((char **)&uc->uc_mcontext)[CANCEL_REG_IP];
R
Rich Felker 已提交
65

66
	a_barrier();
R
Rich Felker 已提交
67
	if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return;
R
Rich Felker 已提交
68

69
	_sigaddset(&uc->uc_sigmask, SIGCANCEL);
R
Rich Felker 已提交
70

71
	if (self->cancelasync || ip >= __cp_begin && ip < __cp_end) {
R
Rich Felker 已提交
72 73
		((char **)&uc->uc_mcontext)[CANCEL_REG_IP] = (char *)__cp_cancel;
		return;
R
Rich Felker 已提交
74 75
	}

76
	__syscall(SYS_tkill, self->tid, SIGCANCEL);
R
Rich Felker 已提交
77 78
}

79
void __testcancel()
R
Rich Felker 已提交
80
{
81
	pthread_t self = __pthread_self();
R
Rich Felker 已提交
82
	if (self->cancel && !self->canceldisable)
83
		__cancel();
R
Rich Felker 已提交
84 85 86 87 88 89 90 91
}

static void init_cancellation()
{
	struct sigaction sa = {
		.sa_flags = SA_SIGINFO | SA_RESTART,
		.sa_sigaction = cancel_handler
	};
92
	memset(&sa.sa_mask, -1, _NSIG/8);
R
Rich Felker 已提交
93 94 95 96 97
	__libc_sigaction(SIGCANCEL, &sa, 0);
}

int pthread_cancel(pthread_t t)
{
98 99 100 101 102
	static int init;
	if (!init) {
		init_cancellation();
		init = 1;
	}
R
Rich Felker 已提交
103 104 105
	a_store(&t->cancel, 1);
	return pthread_kill(t, SIGCANCEL);
}