pthread_create.c 3.0 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
	__pthread_tsd_run_dtors();
26

27 28
	__lock(&self->exitlock);

29
	/* Mark this thread dead before decrementing count */
R
Rich Felker 已提交
30
	__lock(&self->killlock);
31
	self->dead = 1;
R
Rich Felker 已提交
32
	a_store(&self->killlock, 0);
33

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

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

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

static int start(void *p)
{
	struct pthread *self = p;
49
	if (self->unblock_cancel)
50
		__syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
R
Rich Felker 已提交
51 52 53 54
	pthread_exit(self->start(self->start_arg));
	return 0;
}

55
int __uniclone(void *, int (*)(), void *);
R
Rich Felker 已提交
56 57 58 59 60 61 62 63 64 65

#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;
66 67
	size_t size = DEFAULT_STACK_SIZE + DEFAULT_GUARD_SIZE;
	size_t guard = DEFAULT_GUARD_SIZE;
R
Rich Felker 已提交
68 69 70
	struct pthread *self = pthread_self(), *new;
	unsigned char *map, *stack, *tsd;

R
Rich Felker 已提交
71
	if (!self) return ENOSYS;
72
	if (!libc.threaded) {
73
		__syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, 8);
74 75
		libc.threaded = 1;
	}
R
Rich Felker 已提交
76

77 78 79 80
	if (attr) {
		guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
		size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
	}
R
Rich Felker 已提交
81 82 83
	size += __pthread_tsd_size;
	map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
	if (!map) return EAGAIN;
84
	if (guard) mprotect(map, guard, PROT_NONE);
R
Rich Felker 已提交
85 86 87 88 89 90 91 92 93 94 95

	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;
96
	if (attr) new->detached = attr->_a_detach;
97
	new->unblock_cancel = self->cancel;
R
Rich Felker 已提交
98 99 100 101
	memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
	new->tlsdesc[1] = (uintptr_t)new;
	stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);

102
	__rsyscall_lock();
R
Rich Felker 已提交
103 104

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

107
	__rsyscall_unlock();
R
Rich Felker 已提交
108 109 110 111

	if (ret < 0) {
		a_dec(&libc.threads_minus_1);
		munmap(map, size);
112
		return EAGAIN;
R
Rich Felker 已提交
113 114 115 116
	}
	*res = new;
	return 0;
}
117 118 119 120

void pthread_exit(void *result)
{
	struct pthread *self = pthread_self();
R
Rich Felker 已提交
121
	struct __ptcb cb = { .__next = self->cancelbuf };
122
	self->result = result;
R
Rich Felker 已提交
123
	__pthread_unwind_next(&cb);
124
}