1 #include "threadimpl.h"
2 
3 int	_threaddebuglevel;
4 
5 static	uint		threadnproc;
6 static	uint		threadnsysproc;
7 static	Lock		threadnproclock;
8 static	Ref		threadidref;
9 static	Proc		*threadmainproc;
10 static	int		pthreadperthread;
11 
12 static	void		addproc(Proc*);
13 static	void		delproc(Proc*);
14 static	void		addthread(_Threadlist*, _Thread*);
15 static	void		delthread(_Threadlist*, _Thread*);
16 static	int		onlist(_Threadlist*, _Thread*);
17 static	void		addthreadinproc(Proc*, _Thread*);
18 static	void		delthreadinproc(Proc*, _Thread*);
19 static	void		contextswitch(Context *from, Context *to);
20 static	void		procmain(Proc*);
21 static	void		procscheduler(Proc*);
22 static	int		threadinfo(void*, char*);
23 
24 static void
_threaddebug(char * fmt,...)25 _threaddebug(char *fmt, ...)
26 {
27 	va_list arg;
28 	char buf[128];
29 	_Thread *t;
30 	char *p;
31 	static int fd = -1;
32 
33 	if(_threaddebuglevel == 0)
34 		return;
35 
36 	if(fd < 0){
37 		p = strrchr(argv0, '/');
38 		if(p)
39 			p++;
40 		else
41 			p = argv0;
42 		snprint(buf, sizeof buf, "/tmp/%s.tlog", p);
43 		if((fd = create(buf, OWRITE, 0666)) < 0)
44 			fd = open("/dev/null", OWRITE);
45 		if(fd >= 0 && fd != 2){
46 			dup(fd, 2);
47 			close(fd);
48 			fd = 2;
49 		}
50 	}
51 
52 	va_start(arg, fmt);
53 	vsnprint(buf, sizeof buf, fmt, arg);
54 	va_end(arg);
55 	t = proc()->thread;
56 	if(t)
57 		fprint(fd, "%p %d.%d: %s\n", proc(), getpid(), t->id, buf);
58 	else
59 		fprint(fd, "%p %d._: %s\n", proc(), getpid(), buf);
60 }
61 
62 static _Thread*
getthreadnow(void)63 getthreadnow(void)
64 {
65 	return proc()->thread;
66 }
67 _Thread	*(*threadnow)(void) = getthreadnow;
68 
69 static Proc*
procalloc(void)70 procalloc(void)
71 {
72 	Proc *p;
73 
74 	p = malloc(sizeof *p);
75 	if(p == nil)
76 		sysfatal("procalloc malloc: %r");
77 	memset(p, 0, sizeof *p);
78 	addproc(p);
79 	lock(&threadnproclock);
80 	threadnproc++;
81 	unlock(&threadnproclock);
82 	return p;
83 }
84 
85 static void
threadstart(uint y,uint x)86 threadstart(uint y, uint x)
87 {
88 	_Thread *t;
89 	ulong z;
90 
91 //print("threadstart\n");
92 	z = (ulong)x << 16;	/* hide undefined 32-bit shift from 32-bit compilers */
93 	z <<= 16;
94 	z |= y;
95 	t = (_Thread*)z;
96 
97 //print("threadstart sp=%p arg=%p startfn=%p t=%p\n", &t, t, t->startfn, t->startarg);
98 	t->startfn(t->startarg);
99 /*print("threadexits %p\n", v); */
100 	threadexits(nil);
101 /*print("not reacehd\n"); */
102 }
103 
104 static _Thread*
threadalloc(void (* fn)(void *),void * arg,uint stack)105 threadalloc(void (*fn)(void*), void *arg, uint stack)
106 {
107 	_Thread *t;
108 	sigset_t zero;
109 	uint x, y;
110 	ulong z;
111 
112 	/* allocate the task and stack together */
113 	t = malloc(sizeof *t);
114 	if(t == nil)
115 		sysfatal("threadalloc malloc: %r");
116 	memset(t, 0, sizeof *t);
117 	t->id = incref(&threadidref);
118 //print("fn=%p arg=%p\n", fn, arg);
119 	t->startfn = fn;
120 	t->startarg = arg;
121 //print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn);
122 
123 	/* do a reasonable initialization */
124 	if(stack == 0)
125 		return t;
126 	t->stk = _threadstkalloc(stack);
127 	if(t->stk == nil)
128 		sysfatal("threadalloc malloc stack: %r");
129 	t->stksize = stack;
130 	memset(&t->context.uc, 0, sizeof t->context.uc);
131 	sigemptyset(&zero);
132 	sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
133 //print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn);
134 
135 	/* must initialize with current context */
136 	if(getcontext(&t->context.uc) < 0)
137 		sysfatal("threadalloc getcontext: %r");
138 //print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn);
139 
140 	/*
141 	 * Call makecontext to do the real work.
142 	 * To avoid various mistakes on other system software,
143 	 * debuggers, and so on, don't get too close to both
144 	 * ends of the stack. Just staying away is much easier
145 	 * than debugging everything (outside our control)
146 	 * that has off-by-one errors.
147 	 */
148 	t->context.uc.uc_stack.ss_sp = (void*)(t->stk+64);
149 	t->context.uc.uc_stack.ss_size = t->stksize-2*64;
150 #if defined(__sun__) && !defined(__MAKECONTEXT_V2_SOURCE)		/* sigh */
151 	/* can avoid this with __MAKECONTEXT_V2_SOURCE but only on SunOS 5.9 */
152 	t->context.uc.uc_stack.ss_sp =
153 		(char*)t->context.uc.uc_stack.ss_sp
154 		+t->context.uc.uc_stack.ss_size;
155 #endif
156 	/*
157 	 * All this magic is because you have to pass makecontext a
158 	 * function that takes some number of word-sized variables,
159 	 * and on 64-bit machines pointers are bigger than words.
160 	 */
161 //print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn);
162 	z = (ulong)t;
163 	y = z;
164 	z >>= 16;	/* hide undefined 32-bit shift from 32-bit compilers */
165 	x = z>>16;
166 	makecontext(&t->context.uc, (void(*)(void))threadstart, 2, y, x);
167 
168 	return t;
169 }
170 
171 _Thread*
_threadcreate(Proc * p,void (* fn)(void *),void * arg,uint stack)172 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
173 {
174 	_Thread *t;
175 
176 	/* defend against bad C libraries */
177 	if(stack < (256<<10))
178 		stack = 256<<10;
179 
180 	if(p->nthread == 0 || pthreadperthread)
181 		stack = 0; // not using it
182 	t = threadalloc(fn, arg, stack);
183 	t->proc = p;
184 	if(p->nthread == 0)
185 		p->thread0 = t;
186 	else if(pthreadperthread)
187 		_threadpthreadstart(p, t);
188 	p->nthread++;
189 	addthreadinproc(p, t);
190 	_threadready(t);
191 	return t;
192 }
193 
194 int
threadcreate(void (* fn)(void *),void * arg,uint stack)195 threadcreate(void (*fn)(void*), void *arg, uint stack)
196 {
197 	_Thread *t;
198 
199 	t = _threadcreate(proc(), fn, arg, stack);
200 	return t->id;
201 }
202 
203 int
proccreate(void (* fn)(void *),void * arg,uint stack)204 proccreate(void (*fn)(void*), void *arg, uint stack)
205 {
206 	int id;
207 	_Thread *t;
208 	Proc *p;
209 
210 	p = procalloc();
211 	t = _threadcreate(p, fn, arg, stack);
212 	id = t->id;	/* t might be freed after _procstart */
213 	_procstart(p, procmain);
214 	return id;
215 }
216 
217 // For pthreadperthread mode, procswitch flips
218 // between the threads.
219 static void
procswitch(Proc * p,_Thread * from,_Thread * to)220 procswitch(Proc *p, _Thread *from, _Thread *to)
221 {
222 	_threaddebug("procswitch %p %d %d", p, from?from->id:-1, to?to->id:-1);
223 	lock(&p->schedlock);
224 	from->schedrend.l = &p->schedlock;
225 	if(to) {
226 		p->schedthread = to;
227 		to->schedrend.l = &p->schedlock;
228 		_threaddebug("procswitch wakeup %p %d", p, to->id);
229 		_procwakeup(&to->schedrend);
230 	}
231 	if(p->schedthread != from) {
232 		if(from->exiting) {
233 			unlock(&p->schedlock);
234 			_threadpexit();
235 			_threaddebug("procswitch exit wakeup!!!\n");
236 		}
237 		while(p->schedthread != from) {
238 			_threaddebug("procswitch sleep %p %d", p, from->id);
239 			_procsleep(&from->schedrend);
240 			_threaddebug("procswitch awake %p %d", p, from->id);
241 		}
242 		if(p->schedthread != from)
243 			sysfatal("_procswitch %p %p oops", p->schedthread, from);
244 	}
245 	unlock(&p->schedlock);
246 }
247 
248 void
_threadswitch(void)249 _threadswitch(void)
250 {
251 	Proc *p;
252 
253 	needstack(0);
254 	p = proc();
255 
256 /*print("threadswtch %p\n", p); */
257 
258 	if(p->thread == p->thread0)
259 		procscheduler(p);
260 	else if(pthreadperthread)
261 		procswitch(p, p->thread, p->thread0);
262 	else
263 		contextswitch(&p->thread->context, &p->schedcontext);
264 }
265 
266 void
_threadready(_Thread * t)267 _threadready(_Thread *t)
268 {
269 	Proc *p;
270 
271 	p = t->proc;
272 	lock(&p->lock);
273 	p->runrend.l = &p->lock;
274 	addthread(&p->runqueue, t);
275 /*print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid); */
276 	if(p != proc())
277 		_procwakeupandunlock(&p->runrend);
278 	else
279 		unlock(&p->lock);
280 }
281 
282 int
threadidle(void)283 threadidle(void)
284 {
285 	int n;
286 	Proc *p;
287 
288 	p = proc();
289 	n = p->nswitch;
290 	lock(&p->lock);
291 	p->runrend.l = &p->lock;
292 	addthread(&p->idlequeue, p->thread);
293 	unlock(&p->lock);
294 	_threadswitch();
295 	return p->nswitch - n;
296 }
297 
298 int
threadyield(void)299 threadyield(void)
300 {
301 	int n;
302 	Proc *p;
303 
304 	p = proc();
305 	n = p->nswitch;
306 	_threadready(p->thread);
307 	_threadswitch();
308 	return p->nswitch - n;
309 }
310 
311 void
threadexits(char * msg)312 threadexits(char *msg)
313 {
314 	Proc *p;
315 
316 	p = proc();
317 	if(msg == nil)
318 		msg = "";
319 	utfecpy(p->msg, p->msg+sizeof p->msg, msg);
320 	proc()->thread->exiting = 1;
321 	_threadswitch();
322 }
323 
324 void
threadpin(void)325 threadpin(void)
326 {
327 	Proc *p;
328 
329 	p = proc();
330 	if(p->pinthread){
331 		fprint(2, "already pinning a thread - %p %p\n", p->pinthread, p->thread);
332 		assert(0);
333 	}
334 	p->pinthread = p->thread;
335 }
336 
337 void
threadunpin(void)338 threadunpin(void)
339 {
340 	Proc *p;
341 
342 	p = proc();
343 	if(p->pinthread != p->thread){
344 		fprint(2, "wrong pinthread - %p %p\n", p->pinthread, p->thread);
345 		assert(0);
346 	}
347 	p->pinthread = nil;
348 }
349 
350 void
threadsysfatal(char * fmt,va_list arg)351 threadsysfatal(char *fmt, va_list arg)
352 {
353 	char buf[256];
354 
355 	vseprint(buf, buf+sizeof(buf), fmt, arg);
356 	__fixargv0();
357 	fprint(2, "%s: %s\n", argv0 ? argv0 : "<prog>", buf);
358 	threadexitsall(buf);
359 }
360 
361 static void
contextswitch(Context * from,Context * to)362 contextswitch(Context *from, Context *to)
363 {
364 	if(swapcontext(&from->uc, &to->uc) < 0){
365 		fprint(2, "swapcontext failed: %r\n");
366 		assert(0);
367 	}
368 }
369 
370 static void
procmain(Proc * p)371 procmain(Proc *p)
372 {
373 	_Thread *t;
374 
375 	_threadsetproc(p);
376 
377 	/* take out first thread to run on system stack */
378 	t = p->runqueue.head;
379 	delthread(&p->runqueue, t);
380 	memset(&t->context.uc, 0, sizeof t->context.uc);
381 
382 	/* run it */
383 	p->thread = t;
384 	t->startfn(t->startarg);
385 	if(p->nthread != 0)
386 		threadexits(nil);
387 }
388 
389 void
_threadpthreadmain(Proc * p,_Thread * t)390 _threadpthreadmain(Proc *p, _Thread *t)
391 {
392 	_threadsetproc(p);
393 	procswitch(p, t, nil);
394 	t->startfn(t->startarg);
395 	threadexits(nil);
396 }
397 
398 static void
procscheduler(Proc * p)399 procscheduler(Proc *p)
400 {
401 	_Thread *t;
402 
403 	_threaddebug("scheduler enter");
404 //print("s %p\n", p);
405 Top:
406 	lock(&p->lock);
407 	t = p->thread;
408 	p->thread = nil;
409 	if(t->exiting){
410 		delthreadinproc(p, t);
411 		p->nthread--;
412 /*print("nthread %d\n", p->nthread); */
413 		_threadstkfree(t->stk, t->stksize);
414 		free(t);
415 	}
416 
417 	for(;;){
418 		if((t = p->pinthread) != nil){
419 			while(!onlist(&p->runqueue, t)){
420 				p->runrend.l = &p->lock;
421 				_threaddebug("scheduler sleep (pin)");
422 				_procsleep(&p->runrend);
423 				_threaddebug("scheduler wake (pin)");
424 			}
425 		}else
426 		while((t = p->runqueue.head) == nil){
427 			if(p->nthread == 0)
428 				goto Out;
429 			if((t = p->idlequeue.head) != nil){
430 				/*
431 				 * Run all the idling threads once.
432 				 */
433 				while((t = p->idlequeue.head) != nil){
434 					delthread(&p->idlequeue, t);
435 					addthread(&p->runqueue, t);
436 				}
437 				continue;
438 			}
439 			p->runrend.l = &p->lock;
440 			_threaddebug("scheduler sleep");
441 			_procsleep(&p->runrend);
442 			_threaddebug("scheduler wake");
443 		}
444 		if(p->pinthread && p->pinthread != t)
445 			fprint(2, "p->pinthread %p t %p\n", p->pinthread, t);
446 		assert(p->pinthread == nil || p->pinthread == t);
447 		delthread(&p->runqueue, t);
448 		unlock(&p->lock);
449 		p->thread = t;
450 		p->nswitch++;
451 		_threaddebug("run %d (%s)", t->id, t->name);
452 //print("run %p %p %p %p\n", t, *(uintptr*)(t->context.uc.mc.sp), t->context.uc.mc.di, t->context.uc.mc.si);
453 		if(t == p->thread0)
454 			return;
455 		if(pthreadperthread)
456 			procswitch(p, p->thread0, t);
457 		else
458 			contextswitch(&p->schedcontext, &t->context);
459 		_threaddebug("back in scheduler");
460 /*print("back in scheduler\n"); */
461 		goto Top;
462 	}
463 
464 Out:
465 	_threaddebug("scheduler exit");
466 	if(p->mainproc){
467 		/*
468 		 * Stupid bug - on Linux 2.6 and maybe elsewhere,
469 		 * if the main thread exits then the others keep running
470 		 * but the process shows up as a zombie in ps and is not
471 		 * attachable with ptrace.  We'll just sit around pretending
472 		 * to be a system proc instead of exiting.
473 		 */
474 		_threaddaemonize();
475 		lock(&threadnproclock);
476 		if(++threadnsysproc == threadnproc)
477 			threadexitsall(p->msg);
478 		p->sysproc = 1;
479 		unlock(&threadnproclock);
480 		for(;;)
481 		 	sleep(1000);
482 	}
483 
484 	delproc(p);
485 	lock(&threadnproclock);
486 	if(p->sysproc)
487 		--threadnsysproc;
488 	if(--threadnproc == threadnsysproc)
489 		threadexitsall(p->msg);
490 	unlock(&threadnproclock);
491 	unlock(&p->lock);
492 	_threadsetproc(nil);
493 	free(p);
494 	_threadpexit();
495 }
496 
497 void
_threadsetsysproc(void)498 _threadsetsysproc(void)
499 {
500 	lock(&threadnproclock);
501 	if(++threadnsysproc == threadnproc)
502 		threadexitsall(nil);
503 	unlock(&threadnproclock);
504 	proc()->sysproc = 1;
505 }
506 
507 void**
procdata(void)508 procdata(void)
509 {
510 	return &proc()->udata;
511 }
512 
513 void**
threaddata(void)514 threaddata(void)
515 {
516 	return &proc()->thread->udata;
517 }
518 
519 extern Jmp *(*_notejmpbuf)(void);
520 static Jmp*
threadnotejmp(void)521 threadnotejmp(void)
522 {
523 	return &proc()->sigjmp;
524 }
525 
526 /*
527  * debugging
528  */
529 void
threadsetname(char * fmt,...)530 threadsetname(char *fmt, ...)
531 {
532 	va_list arg;
533 	_Thread *t;
534 
535 	t = proc()->thread;
536 	va_start(arg, fmt);
537 	vsnprint(t->name, sizeof t->name, fmt, arg);
538 	va_end(arg);
539 }
540 
541 char*
threadgetname(void)542 threadgetname(void)
543 {
544 	return proc()->thread->name;
545 }
546 
547 void
threadsetstate(char * fmt,...)548 threadsetstate(char *fmt, ...)
549 {
550 	va_list arg;
551 	_Thread *t;
552 
553 	t = proc()->thread;
554 	va_start(arg, fmt);
555 	vsnprint(t->state, sizeof t->name, fmt, arg);
556 	va_end(arg);
557 }
558 
559 int
threadid(void)560 threadid(void)
561 {
562 	_Thread *t;
563 
564 	t = proc()->thread;
565 	return t->id;
566 }
567 
568 void
needstack(int n)569 needstack(int n)
570 {
571 	_Thread *t;
572 
573 	t = proc()->thread;
574 	if(t->stk == nil)
575 		return;
576 
577 	if((char*)&t <= (char*)t->stk
578 	|| (char*)&t - (char*)t->stk < 256+n){
579 		fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
580 		abort();
581 	}
582 }
583 
584 static int
singlethreaded(void)585 singlethreaded(void)
586 {
587 	return threadnproc == 1 && _threadprocs->nthread == 1;
588 }
589 
590 /*
591  * locking
592  */
593 static int
threadqlock(QLock * l,int block,ulong pc)594 threadqlock(QLock *l, int block, ulong pc)
595 {
596 /*print("threadqlock %p\n", l); */
597 	lock(&l->l);
598 	if(l->owner == nil){
599 		l->owner = (*threadnow)();
600 /*print("qlock %p @%#x by %p\n", l, pc, l->owner); */
601 		if(l->owner == nil) {
602 			fprint(2, "%s: qlock uncontended owner=nil oops\n", argv0);
603 			abort();
604 		}
605 		unlock(&l->l);
606 		return 1;
607 	}
608 	if(!block){
609 		unlock(&l->l);
610 		return 0;
611 	}
612 
613 	if(singlethreaded()){
614 		fprint(2, "qlock deadlock\n");
615 		abort();
616 	}
617 
618 /*print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)()); */
619 	addthread(&l->waiting, (*threadnow)());
620 	unlock(&l->l);
621 
622 	_threadswitch();
623 
624 	if(l->owner != (*threadnow)()){
625 		fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
626 			argv0, pc, l->owner, (*threadnow)());
627 		abort();
628 	}
629 	if(l->owner == nil) {
630 		fprint(2, "%s: qlock threadswitch owner=nil oops\n", argv0);
631 		abort();
632 	}
633 
634 /*print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)()); */
635 	return 1;
636 }
637 
638 static void
threadqunlock(QLock * l,ulong pc)639 threadqunlock(QLock *l, ulong pc)
640 {
641 	_Thread *ready;
642 
643 	lock(&l->l);
644 /*print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner); */
645 	if(l->owner == 0){
646 		fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
647 			argv0, pc, l->owner, (*threadnow)());
648 		abort();
649 	}
650 	if((l->owner = ready = l->waiting.head) != nil)
651 		delthread(&l->waiting, l->owner);
652 	/*
653 	 * N.B. Cannot call _threadready() before unlocking l->l,
654 	 * because the thread we are readying might:
655 	 *	- be in another proc
656 	 *	- start running immediately
657 	 *	- and free l before we get a chance to run again
658 	 */
659 	unlock(&l->l);
660 	if(ready)
661 		_threadready(l->owner);
662 }
663 
664 static int
threadrlock(RWLock * l,int block,ulong pc)665 threadrlock(RWLock *l, int block, ulong pc)
666 {
667 	USED(pc);
668 
669 	lock(&l->l);
670 	if(l->writer == nil && l->wwaiting.head == nil){
671 		l->readers++;
672 		unlock(&l->l);
673 		return 1;
674 	}
675 	if(!block){
676 		unlock(&l->l);
677 		return 0;
678 	}
679 	if(singlethreaded()){
680 		fprint(2, "rlock deadlock\n");
681 		abort();
682 	}
683 	addthread(&l->rwaiting, (*threadnow)());
684 	unlock(&l->l);
685 	_threadswitch();
686 	return 1;
687 }
688 
689 static int
threadwlock(RWLock * l,int block,ulong pc)690 threadwlock(RWLock *l, int block, ulong pc)
691 {
692 	USED(pc);
693 
694 	lock(&l->l);
695 	if(l->writer == nil && l->readers == 0){
696 		l->writer = (*threadnow)();
697 		unlock(&l->l);
698 		return 1;
699 	}
700 	if(!block){
701 		unlock(&l->l);
702 		return 0;
703 	}
704 	if(singlethreaded()){
705 		fprint(2, "wlock deadlock\n");
706 		abort();
707 	}
708 	addthread(&l->wwaiting, (*threadnow)());
709 	unlock(&l->l);
710 	_threadswitch();
711 	return 1;
712 }
713 
714 static void
threadrunlock(RWLock * l,ulong pc)715 threadrunlock(RWLock *l, ulong pc)
716 {
717 	_Thread *t;
718 
719 	USED(pc);
720 	t = nil;
721 	lock(&l->l);
722 	--l->readers;
723 	if(l->readers == 0 && (t = l->wwaiting.head) != nil){
724 		delthread(&l->wwaiting, t);
725 		l->writer = t;
726 	}
727 	unlock(&l->l);
728 	if(t)
729 		_threadready(t);
730 
731 }
732 
733 static void
threadwunlock(RWLock * l,ulong pc)734 threadwunlock(RWLock *l, ulong pc)
735 {
736 	_Thread *t;
737 
738 	USED(pc);
739 	lock(&l->l);
740 	l->writer = nil;
741 	assert(l->readers == 0);
742 	while((t = l->rwaiting.head) != nil){
743 		delthread(&l->rwaiting, t);
744 		l->readers++;
745 		_threadready(t);
746 	}
747 	t = nil;
748 	if(l->readers == 0 && (t = l->wwaiting.head) != nil){
749 		delthread(&l->wwaiting, t);
750 		l->writer = t;
751 	}
752 	unlock(&l->l);
753 	if(t)
754 		_threadready(t);
755 }
756 
757 /*
758  * sleep and wakeup
759  */
760 static void
threadrsleep(Rendez * r,ulong pc)761 threadrsleep(Rendez *r, ulong pc)
762 {
763 	if(singlethreaded()){
764 		fprint(2, "rsleep deadlock\n");
765 		abort();
766 	}
767 	addthread(&r->waiting, proc()->thread);
768 	qunlock(r->l);
769 	_threadswitch();
770 	qlock(r->l);
771 }
772 
773 static int
threadrwakeup(Rendez * r,int all,ulong pc)774 threadrwakeup(Rendez *r, int all, ulong pc)
775 {
776 	int i;
777 	_Thread *t;
778 
779 	for(i=0;; i++){
780 		if(i==1 && !all)
781 			break;
782 		if((t = r->waiting.head) == nil)
783 			break;
784 		delthread(&r->waiting, t);
785 		_threadready(t);
786 	}
787 	return i;
788 }
789 
790 /*
791  * startup
792  */
793 
794 static int threadargc;
795 static char **threadargv;
796 int mainstacksize;
797 extern int _p9usepwlibrary;	/* getgrgid etc. smash the stack - tell _p9dir just say no */
798 static void
threadmainstart(void * v)799 threadmainstart(void *v)
800 {
801 	USED(v);
802 
803 	/*
804 	 * N.B. This call to proc() is a program's first call (indirectly) to a
805 	 * pthreads function while executing on a non-pthreads-allocated
806 	 * stack.  If the pthreads implementation is using the stack pointer
807 	 * to locate the per-thread data, then this call will blow up.
808 	 * This means the pthread implementation is not suitable for
809 	 * running under libthread.  Time to write your own.  Sorry.
810 	 */
811 	_p9usepwlibrary = 0;
812 	threadmainproc = proc();
813 	threadmain(threadargc, threadargv);
814 }
815 
816 extern void (*_sysfatal)(char*, va_list);
817 
818 int
main(int argc,char ** argv)819 main(int argc, char **argv)
820 {
821 	Proc *p;
822 	char *opts;
823 
824 	argv0 = argv[0];
825 
826 	opts = getenv("LIBTHREAD");
827 	if(opts == nil)
828 		opts = "";
829 
830 	pthreadperthread = (strstr(opts, "pthreadperthread") != nil);
831 #ifdef PLAN9PORT_ASAN
832 	// ASAN can't deal with the coroutine stack switches.
833 	// In theory it has support for informing it about stack switches,
834 	// but even with those calls added it can't deal with things
835 	// like fork or exit from a coroutine stack.
836 	// Easier to just run in pthread-per-thread mode.
837 	pthreadperthread = 1;
838 #endif
839 	if(strstr(opts, "nodaemon") == nil && getenv("NOLIBTHREADDAEMONIZE") == nil)
840 		_threadsetupdaemonize();
841 
842 	threadargc = argc;
843 	threadargv = argv;
844 
845 	/*
846 	 * Install locking routines into C library.
847 	 */
848 	_lock = _threadlock;
849 	_unlock = _threadunlock;
850 	_qlock = threadqlock;
851 	_qunlock = threadqunlock;
852 	_rlock = threadrlock;
853 	_runlock = threadrunlock;
854 	_wlock = threadwlock;
855 	_wunlock = threadwunlock;
856 	_rsleep = threadrsleep;
857 	_rwakeup = threadrwakeup;
858 	_notejmpbuf = threadnotejmp;
859 	_pin = threadpin;
860 	_unpin = threadunpin;
861 	_sysfatal = threadsysfatal;
862 
863 	_pthreadinit();
864 	p = procalloc();
865 	p->mainproc = 1;
866 	_threadsetproc(p);
867 	if(mainstacksize == 0)
868 		mainstacksize = 256*1024;
869 	atnotify(threadinfo, 1);
870 	_threadcreate(p, threadmainstart, nil, mainstacksize);
871 	procmain(p);
872 	sysfatal("procscheduler returned in threadmain!");
873 	/* does not return */
874 	return 0;
875 }
876 
877 /*
878  * hooray for linked lists
879  */
880 static void
addthread(_Threadlist * l,_Thread * t)881 addthread(_Threadlist *l, _Thread *t)
882 {
883 	if(l->tail){
884 		l->tail->next = t;
885 		t->prev = l->tail;
886 	}else{
887 		l->head = t;
888 		t->prev = nil;
889 	}
890 	l->tail = t;
891 	t->next = nil;
892 }
893 
894 static void
delthread(_Threadlist * l,_Thread * t)895 delthread(_Threadlist *l, _Thread *t)
896 {
897 	if(t->prev)
898 		t->prev->next = t->next;
899 	else
900 		l->head = t->next;
901 	if(t->next)
902 		t->next->prev = t->prev;
903 	else
904 		l->tail = t->prev;
905 }
906 
907 /* inefficient but rarely used */
908 static int
onlist(_Threadlist * l,_Thread * t)909 onlist(_Threadlist *l, _Thread *t)
910 {
911 	_Thread *tt;
912 
913 	for(tt = l->head; tt; tt=tt->next)
914 		if(tt == t)
915 			return 1;
916 	return 0;
917 }
918 
919 static void
addthreadinproc(Proc * p,_Thread * t)920 addthreadinproc(Proc *p, _Thread *t)
921 {
922 	_Threadlist *l;
923 
924 	l = &p->allthreads;
925 	if(l->tail){
926 		l->tail->allnext = t;
927 		t->allprev = l->tail;
928 	}else{
929 		l->head = t;
930 		t->allprev = nil;
931 	}
932 	l->tail = t;
933 	t->allnext = nil;
934 }
935 
936 static void
delthreadinproc(Proc * p,_Thread * t)937 delthreadinproc(Proc *p, _Thread *t)
938 {
939 	_Threadlist *l;
940 
941 	l = &p->allthreads;
942 	if(t->allprev)
943 		t->allprev->allnext = t->allnext;
944 	else
945 		l->head = t->allnext;
946 	if(t->allnext)
947 		t->allnext->allprev = t->allprev;
948 	else
949 		l->tail = t->allprev;
950 }
951 
952 Proc *_threadprocs;
953 Lock _threadprocslock;
954 static Proc *_threadprocstail;
955 
956 static void
addproc(Proc * p)957 addproc(Proc *p)
958 {
959 	lock(&_threadprocslock);
960 	if(_threadprocstail){
961 		_threadprocstail->next = p;
962 		p->prev = _threadprocstail;
963 	}else{
964 		_threadprocs = p;
965 		p->prev = nil;
966 	}
967 	_threadprocstail = p;
968 	p->next = nil;
969 	unlock(&_threadprocslock);
970 }
971 
972 static void
delproc(Proc * p)973 delproc(Proc *p)
974 {
975 	lock(&_threadprocslock);
976 	if(p->prev)
977 		p->prev->next = p->next;
978 	else
979 		_threadprocs = p->next;
980 	if(p->next)
981 		p->next->prev = p->prev;
982 	else
983 		_threadprocstail = p->prev;
984 	unlock(&_threadprocslock);
985 }
986 
987 /*
988  * notify - for now just use the usual mechanisms
989  */
990 void
threadnotify(int (* f)(void *,char *),int in)991 threadnotify(int (*f)(void*, char*), int in)
992 {
993 	atnotify(f, in);
994 }
995 
996 static int
onrunqueue(Proc * p,_Thread * t)997 onrunqueue(Proc *p, _Thread *t)
998 {
999 	_Thread *tt;
1000 
1001 	for(tt=p->runqueue.head; tt; tt=tt->next)
1002 		if(tt == t)
1003 			return 1;
1004 	return 0;
1005 }
1006 
1007 /*
1008  * print state - called from SIGINFO
1009  */
1010 static int
threadinfo(void * v,char * s)1011 threadinfo(void *v, char *s)
1012 {
1013 	Proc *p;
1014 	_Thread *t;
1015 
1016 	if(strcmp(s, "quit") != 0 && strcmp(s, "sys: status request") != 0)
1017 		return 0;
1018 
1019 	for(p=_threadprocs; p; p=p->next){
1020 		fprint(2, "proc %p %s%s\n", (void*)p->osprocid, p->msg,
1021 			p->sysproc ? " (sysproc)": "");
1022 		for(t=p->allthreads.head; t; t=t->allnext){
1023 			fprint(2, "\tthread %d %s: %s %s\n",
1024 				t->id,
1025 				t == p->thread ? "Running" :
1026 				onrunqueue(p, t) ? "Ready" : "Sleeping",
1027 				t->state, t->name);
1028 		}
1029 	}
1030 	return 1;
1031 }
1032