pthread_create.c 3.1 KB
Newer Older
R
Rich Felker 已提交
1 2
#include "pthread_impl.h"

3 4 5 6 7
static void dummy_0()
{
}
weak_alias(dummy_0, __rsyscall_lock);
weak_alias(dummy_0, __rsyscall_unlock);
8
weak_alias(dummy_0, __pthread_tsd_run_dtors);
9

10 11 12 13 14
#ifdef __pthread_unwind_next
#undef __pthread_unwind_next
#define __pthread_unwind_next __pthread_unwind_next_3
#endif

15 16
void __pthread_unwind_next(struct __ptcb *cb)
{
17
	pthread_t self = pthread_self();
R
Rich Felker 已提交
18
	int n;
19

20 21 22 23
	if (cb->__next) {
		self->cancelbuf = cb->__next->__next;
		longjmp((void *)cb->__next->__jb, 1);
	}
24 25 26

	LOCK(&self->exitlock);

27
	__pthread_tsd_run_dtors();
28

29 30
	/* Mark this thread dead before decrementing count */
	self->dead = 1;
31

R
Rich Felker 已提交
32 33 34
	do n = libc.threads_minus_1;
	while (n && a_cas(&libc.threads_minus_1, n, n-1)!=n);
	if (!n) exit(0);
35

36
	if (self->detached && self->map_base) {
37
		__syscall(SYS_rt_sigprocmask, SIG_BLOCK, (long)(uint64_t[1]){-1},0,8);
38
		__unmapself(self->map_base, self->map_size);
39
	}
40

41
	__syscall(SYS_exit, 0);
42
}
R
Rich Felker 已提交
43 44 45 46

static int start(void *p)
{
	struct pthread *self = p;
47 48 49 50 51 52
	if (self->unblock_cancel) {
		sigset_t set;
		sigemptyset(&set);
		sigaddset(&set, SIGCANCEL);
		__libc_sigprocmask(SIG_UNBLOCK, &set, 0);
	}
R
Rich Felker 已提交
53 54 55 56
	pthread_exit(self->start(self->start_arg));
	return 0;
}

57
int __uniclone(void *, int (*)(), void *);
R
Rich Felker 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70

#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)

/* pthread_key_create.c overrides this */
static const size_t dummy = 0;
weak_alias(dummy, __pthread_tsd_size);

int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
{
	int ret;
	size_t size, guard;
	struct pthread *self = pthread_self(), *new;
	unsigned char *map, *stack, *tsd;
71
	const pthread_attr_t default_attr = { 0 };
R
Rich Felker 已提交
72

R
Rich Felker 已提交
73
	if (!self) return ENOSYS;
74 75 76 77 78 79 80 81
	if (!libc.threaded) {
		sigset_t set;
		sigemptyset(&set);
		sigaddset(&set, SIGSYSCALL);
		sigaddset(&set, SIGCANCEL);
		__libc_sigprocmask(SIG_UNBLOCK, &set, 0);
		libc.threaded = 1;
	}
R
Rich Felker 已提交
82 83

	if (!attr) attr = &default_attr;
84 85
	guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
	size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
R
Rich Felker 已提交
86 87 88
	size += __pthread_tsd_size;
	map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
	if (!map) return EAGAIN;
89
	if (guard) mprotect(map, guard, PROT_NONE);
R
Rich Felker 已提交
90 91 92 93 94 95 96 97 98 99 100

	tsd = map + size - __pthread_tsd_size;
	new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
	new->map_base = map;
	new->map_size = size;
	new->pid = self->pid;
	new->errno_ptr = &new->errno_val;
	new->start = entry;
	new->start_arg = arg;
	new->self = new;
	new->tsd = (void *)tsd;
101
	new->detached = attr->_a_detach;
R
Rich Felker 已提交
102
	new->attr = *attr;
103
	new->unblock_cancel = self->cancel;
R
Rich Felker 已提交
104 105 106 107
	memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
	new->tlsdesc[1] = (uintptr_t)new;
	stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);

108
	__rsyscall_lock();
R
Rich Felker 已提交
109 110

	a_inc(&libc.threads_minus_1);
111
	ret = __uniclone(stack, start, new);
R
Rich Felker 已提交
112

113
	__rsyscall_unlock();
R
Rich Felker 已提交
114 115 116 117

	if (ret < 0) {
		a_dec(&libc.threads_minus_1);
		munmap(map, size);
118
		return EAGAIN;
R
Rich Felker 已提交
119 120 121 122
	}
	*res = new;
	return 0;
}
123 124 125 126

void pthread_exit(void *result)
{
	struct pthread *self = pthread_self();
R
Rich Felker 已提交
127
	struct __ptcb cb = { .__next = self->cancelbuf };
128
	self->result = result;
R
Rich Felker 已提交
129
	__pthread_unwind_next(&cb);
130
}