pthread_create.c 3.2 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

static void init_threads()
{
R
Rich Felker 已提交
49
	sigset_t set;
R
Rich Felker 已提交
50
	libc.lock = __lock;
51
	libc.lockfile = __lockfile;
R
Rich Felker 已提交
52

R
Rich Felker 已提交
53 54 55 56
	sigemptyset(&set);
	sigaddset(&set, SIGSYSCALL);
	sigaddset(&set, SIGCANCEL);
	__libc_sigprocmask(SIG_UNBLOCK, &set, 0);
R
Rich Felker 已提交
57 58 59 60 61
}

static int start(void *p)
{
	struct pthread *self = p;
62 63 64 65 66 67
	if (self->unblock_cancel) {
		sigset_t set;
		sigemptyset(&set);
		sigaddset(&set, SIGCANCEL);
		__libc_sigprocmask(SIG_UNBLOCK, &set, 0);
	}
R
Rich Felker 已提交
68 69 70 71
	pthread_exit(self->start(self->start_arg));
	return 0;
}

72
int __uniclone(void *, int (*)(), void *);
R
Rich Felker 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86

#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)
{
	static int init;
	int ret;
	size_t size, guard;
	struct pthread *self = pthread_self(), *new;
	unsigned char *map, *stack, *tsd;
87
	const pthread_attr_t default_attr = { 0 };
R
Rich Felker 已提交
88

R
Rich Felker 已提交
89
	if (!self) return ENOSYS;
R
Rich Felker 已提交
90 91 92
	if (!init && ++init) init_threads();

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

	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;
110
	new->detached = attr->_a_detach;
R
Rich Felker 已提交
111
	new->attr = *attr;
112
	new->unblock_cancel = self->cancel;
113
	new->result = PTHREAD_CANCELED;
R
Rich Felker 已提交
114 115 116 117
	memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
	new->tlsdesc[1] = (uintptr_t)new;
	stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);

118
	__rsyscall_lock();
R
Rich Felker 已提交
119 120

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

123
	__rsyscall_unlock();
R
Rich Felker 已提交
124 125 126 127

	if (ret < 0) {
		a_dec(&libc.threads_minus_1);
		munmap(map, size);
128
		return EAGAIN;
R
Rich Felker 已提交
129 130 131 132
	}
	*res = new;
	return 0;
}
133 134 135 136

void pthread_exit(void *result)
{
	struct pthread *self = pthread_self();
R
Rich Felker 已提交
137
	struct __ptcb cb = { .__next = self->cancelbuf };
138
	self->result = result;
R
Rich Felker 已提交
139 140 141
	self->canceldisable = 1;
	self->cancelasync = 0;
	__pthread_unwind_next(&cb);
142
}