summaryrefslogtreecommitdiff
path: root/src/thread/forkall.c
blob: 403818ec956edab5c5195a185c70ca77a73bb4ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "pthread_impl.h"
#include <setjmp.h>

struct thread {
	struct thread *next;
	pthread_t td;
	jmp_buf jb;
	void *tmp, *stack;
};

struct ctx {
	struct thread *list;
	pthread_t caller;
	pid_t pid;
	size_t cnt;
	pthread_barrier_t barrier;
};

static void restart_thread(pthread_t self)
{
	struct thread *t = self->start_arg;
	self->start_arg = t->tmp;
	self->pid = getpid();
	longjmp(t->jb, 1);
}

static void do_forkall(void *p)
{
	struct ctx *c = p, *volatile cv = c;
	char tmpstack[2048];
	struct thread *tp, t = {
		.td = __pthread_self(),
		.next = c->list,
		.stack = tmpstack+1024
	};

	if (t.td != c->caller) {
		c->cnt++;
		t.tmp = t.td->start_arg;
		t.td->start_arg = &t;
		if (setjmp(t.jb)) {
			c = cv;
			if (c->pid) return;
			pthread_barrier_wait(&c->barrier);
			return;
		}
		c->list = &t;
		__synccall_wait();
		return;
	}
	c->pid = syscall(SYS_fork);
	if (c->pid) return;

	pthread_barrier_init(&c->barrier, 0, c->cnt);
	for (tp=c->list; tp; tp=tp->next)
		if (__uniclone(tp->stack, restart_thread, tp->td) < 0)
			_exit(127);
	pthread_barrier_wait(&c->barrier);
}

pid_t forkall()
{
	struct ctx c = { .caller = pthread_self() };
	__synccall(do_forkall, &c);
	return c.pid;
}