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

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

9 10 11 12 13
static void dummy_1(pthread_t self)
{
}
weak_alias(dummy_1, __pthread_tsd_run_dtors);

14 15 16 17 18
#ifdef __pthread_unwind_next
#undef __pthread_unwind_next
#define __pthread_unwind_next __pthread_unwind_next_3
#endif

19 20 21
void __pthread_unwind_next(struct __ptcb *cb)
{
	pthread_t self;
R
Rich Felker 已提交
22
	int n;
23 24 25 26 27 28 29

	if (cb->__next) longjmp((void *)cb->__next->__jb, 1);

	self = pthread_self();

	LOCK(&self->exitlock);

30
	__pthread_tsd_run_dtors(self);
31

32 33
	/* Mark this thread dead before decrementing count */
	self->dead = 1;
34

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

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

44
	__syscall(SYS_exit, 0);
45
}
R
Rich Felker 已提交
46 47 48 49

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

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

#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;
74
	const pthread_attr_t default_attr = { 0 };
R
Rich Felker 已提交
75

R
Rich Felker 已提交
76
	if (!self) return ENOSYS;
77 78 79 80 81 82 83 84
	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 已提交
85 86

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

	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;
104
	new->detached = attr->_a_detach;
R
Rich Felker 已提交
105
	new->attr = *attr;
106
	new->unblock_cancel = self->cancel;
107
	new->result = PTHREAD_CANCELED;
R
Rich Felker 已提交
108 109 110 111
	memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
	new->tlsdesc[1] = (uintptr_t)new;
	stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);

112
	__rsyscall_lock();
R
Rich Felker 已提交
113 114

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

117
	__rsyscall_unlock();
R
Rich Felker 已提交
118 119 120 121

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

void pthread_exit(void *result)
{
	struct pthread *self = pthread_self();
R
Rich Felker 已提交
131
	struct __ptcb cb = { .__next = self->cancelbuf };
132
	self->result = result;
R
Rich Felker 已提交
133 134 135
	self->canceldisable = 1;
	self->cancelasync = 0;
	__pthread_unwind_next(&cb);
136
}