aboutsummaryrefslogtreecommitdiff
path: root/src/libthread/pthread.c
blob: 83d8b023576d6e0f3d0b0f4de42a7c8d05a59c31 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <u.h>
#include <errno.h>
#include "threadimpl.h"

/*
 * Basic kernel thread management.
 */
static pthread_key_t key;

void
_kthreadinit(void)
{
	pthread_key_create(&key, 0);
}

void
_kthreadsetproc(Proc *p)
{
	sigset_t all;

	p->pthreadid = pthread_self();
	sigfillset(&all);
	pthread_sigmask(SIG_SETMASK, &all, nil);
	pthread_setspecific(key, p);
}

Proc*
_kthreadgetproc(void)
{
	return pthread_getspecific(key);
}

void
_kthreadstartproc(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;
}

void
_kthreadexitproc(char *exitstr)
{
	_threaddebug(DBGSCHED, "_pthreadexit");
	pthread_exit(nil);
}

void
_kthreadexitallproc(char *exitstr)
{
	_threaddebug(DBGSCHED, "_threadexitallproc");
	exits(exitstr);
}

/*
 * Exec.  Pthreads does the hard work of making it possible
 * for any thread to do the waiting, so this is pretty easy.
 * We create a separate proc whose job is to wait for children
 * and deliver wait messages.
 */
static Channel *_threadexecwaitchan;

static void
_threadwaitproc(void *v)
{
	Channel *c;
	Waitmsg *w;

	_threadinternalproc();

	USED(v);
	
	for(;;){
		w = wait();
		if(w == nil){
			if(errno == ECHILD)	/* wait for more */
				recvul(_threadexecwaitchan);
			continue;
		}
		if((c = _threadwaitchan) != nil)
			sendp(c, w);
		else
			free(w);
	}
	fprint(2, "_threadwaitproc exits\n");	/* not reached */
}


/* 
 * Call _threadexec in the right conditions.
 */
int
_kthreadexec(Channel *c, int fd[3], char *prog, char *args[], int freeargs)
{
	static Lock lk;
	int rv;

	if(!_threadexecwaitchan){
		lock(&lk);
		if(!_threadexecwaitchan){
			_threadexecwaitchan = chancreate(sizeof(ulong), 1);
			proccreate(_threadwaitproc, nil, 32*1024);
		}
		unlock(&lk);
	}
	rv = _threadexec(c, fd, prog, args, freeargs);
	nbsendul(_threadexecwaitchan, 1);
	return rv;
}

/*
 * Some threaded applications want to run in the background.
 * Calling fork() and exiting in the parent will result in a child
 * with a single pthread (if we are using pthreads), and will screw
 * up our internal process info if we are using clone/rfork.
 * Instead, apps should call threadbackground(), which takes
 * care of this.
 * 
 * _threadbackgroundinit is called from main.
 */

static int mainpid, passerpid;

static void
passer(void *x, char *msg)
{
	Waitmsg *w;

	USED(x);
	if(strcmp(msg, "sys: usr2") == 0)
		_exit(0);	/* daemonize */
	else if(strcmp(msg, "sys: child") == 0){
		/* child exited => so should we */
		w = wait();
		if(w == nil)
			_exit(1);
		_exit(atoi(w->msg));
	}else
		postnote(PNGROUP, mainpid, msg);
}

void
_threadbackgroundinit(void)
{
	int pid;
	sigset_t mask;

	sigfillset(&mask);
	pthread_sigmask(SIG_BLOCK, &mask, 0);

return;

	passerpid = getpid();
	switch(pid = fork()){
	case -1:
		sysfatal("fork: %r");

	case 0:
		rfork(RFNOTEG);
		return;

	default:
		break;
	}

	mainpid = pid;
	notify(passer);
	notifyon("sys: child");
	notifyon("sys: usr2");	/* should already be on */
	for(;;)
		pause();
	_exit(0);
}

void
threadbackground(void)
{
	if(passerpid <= 1)
		return;
	postnote(PNPROC, passerpid, "sys: usr2");
}

/*
 * Notes.
 */
Channel *_threadnotechan;
static ulong sigs;
static Lock _threadnotelk;
static void _threadnoteproc(void*);
extern int _p9strsig(char*);
extern char *_p9sigstr(int);

Channel*
threadnotechan(void)
{
	if(_threadnotechan == nil){
		lock(&_threadnotelk);
		if(_threadnotechan == nil){
			_threadnotechan = chancreate(sizeof(char*), 1);
			proccreate(_threadnoteproc, nil, 32*1024);
		}
		unlock(&_threadnotelk);
	}
	return _threadnotechan;
}

void
_threadnote(void *x, char *msg)
{
	USED(x);

	if(_threadexitsallstatus)
		_kthreadexitproc(_threadexitsallstatus);

	if(strcmp(msg, "sys: usr2") == 0)
		noted(NCONT);

	if(_threadnotechan == nil)
		noted(NDFLT);

	sigs |= 1<<_p9strsig(msg);
	noted(NCONT);
}

void
_threadnoteproc(void *x)
{
	int i;
	sigset_t none;
	Channel *c;

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

	c = _threadnotechan;
	for(;;){
		if(sigs == 0)
			pause();
		for(i=0; i<32; i++){
			if((sigs&(1<<i)) == 0)
				continue;
			sigs &= ~(1<<i);
			if(i == 0)
				continue;
			sendp(c, _p9sigstr(i));
		}
	}
}

void
_threadschednote(void)
{
}

void
_kmaininit(void)
{
	sigset_t all;

	sigfillset(&all);
	pthread_sigmask(SIG_SETMASK, &all, 0);
}