aboutsummaryrefslogtreecommitdiff
path: root/src/libthread/pthread.c
blob: b2c1c026b12af15671f78cb378d05aaa3847cef5 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <u.h>
#include <errno.h>
#include "threadimpl.h"

static int multi;
static Proc *theproc;
static pthread_key_t key;

/*
 * Called before we go multiprocess.
 */
void
_threadmultiproc(void)
{
	if(multi == 0){
		multi = 1;
		pthread_key_create(&key, 0);
		_threadsetproc(theproc);
	}
}

/*
 * Set the proc for the current pthread.
 */
void
_threadsetproc(Proc *p)
{
	if(!multi){
		theproc = p;
		return;
	}
	pthread_setspecific(key, p);
}

/* 
 * Get the proc for the current pthread.
 */
Proc*
_threadgetproc(void)
{
	if(!multi)
		return theproc;

	return pthread_getspecific(key);
}

/*
 * Called to associate p with the current pthread.
 */
void
_threadinitproc(Proc *p)
{
	p->pthreadid = pthread_self();
	_threadsetproc(p);
}

/*
 * Called to exit the current pthread.
 */
void
_threadexitproc(char *exitstr)
{
	pthread_exit(nil);
}

/*
 * Called to exit all pthreads.
 */
void
_threadexitallproc(char *exitstr)
{
	exits(0);
}

/*
 * Called to poll for any kids of this pthread.
 * Wait messages aren't restricted to a particular
 * pthread, so we have a separate proc responsible
 * for them.  So this is a no-op.
 */
void
_threadwaitkids(Proc *p)
{
}

/*
 * Separate process to wait for child messages.
 */
Channel *_threadexecchan;
void
_threadwaitproc(void *v)
{
	Channel *c;
	Waitmsg *w;
	sigset_t none;

	sigemptyset(&none);
	pthread_sigmask(SIG_SETMASK, &none, 0);

	USED(v);
	
	for(;;){
		w = wait();
		if(w == nil){
			if(errno == ECHILD)
				recvul(_threadexecchan);
			continue;
		}
		if((c = _threadwaitchan) != nil)
			sendp(c, w);
		else
			free(w);
	}
}

/* 
 * Called before the first exec.
 */
void
_threadfirstexec(void)
{
}

void
_threadmaininit(void)
{
	_threadexecchan = chancreate(sizeof(ulong), 1);
	proccreate(_threadwaitproc, nil, 32*1024);

	/*
	 * Sleazy: decrement threadnprocs so that 
	 * the existence of the _threadwaitproc proc
	 * doesn't keep us from exiting.
	 */
	lock(&_threadpq.lock);
	--_threadnprocs;
	/* print("change %d -> %d\n", _threadnprocs+1, _threadnprocs); */
	unlock(&_threadpq.lock);
}

void
_threadafterexec(void)
{
	nbsendul(_threadexecchan, 1);
}

/*
 * Called to start a new proc.
 */
void
_threadstartproc(Proc *p)
{
	Proc *np;
	pthread_t tid;
	sigset_t all;

	np = p->newproc;
	sigfillset(&all);
	pthread_sigmask(SIG_SETMASK, &all, nil);
	if(pthread_create(&tid, nil, (void*(*)(void*))_threadscheduler, 
			np) < 0)
		sysfatal("pthread_create: %r");
	np->pthreadid = tid;
}