Blob


1 #include "threadimpl.h"
3 int _threaddebuglevel;
5 static uint threadnproc;
6 static uint threadnsysproc;
7 static Lock threadnproclock;
8 static Ref threadidref;
9 static Proc *threadmainproc;
10 static int pthreadperthread;
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*);
24 static void
25 _threaddebug(char *fmt, ...)
26 {
27 va_list arg;
28 char buf[128];
29 _Thread *t;
30 char *p;
31 static int fd = -1;
33 if(_threaddebuglevel == 0)
34 return;
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 }
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 }
62 static _Thread*
63 getthreadnow(void)
64 {
65 return proc()->thread;
66 }
67 _Thread *(*threadnow)(void) = getthreadnow;
69 static Proc*
70 procalloc(void)
71 {
72 Proc *p;
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 }
85 static void
86 threadstart(uint y, uint x)
87 {
88 _Thread *t;
89 ulong z;
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;
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"); */
104 static _Thread*
105 threadalloc(void (*fn)(void*), void *arg, uint stack)
107 _Thread *t;
108 sigset_t zero;
109 uint x, y;
110 ulong z;
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);
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);
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);
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);
168 return t;
171 _Thread*
172 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
174 _Thread *t;
176 /* defend against bad C libraries */
177 if(stack < (256<<10))
178 stack = 256<<10;
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;
194 int
195 threadcreate(void (*fn)(void*), void *arg, uint stack)
197 _Thread *t;
199 t = _threadcreate(proc(), fn, arg, stack);
200 return t->id;
203 int
204 proccreate(void (*fn)(void*), void *arg, uint stack)
206 int id;
207 _Thread *t;
208 Proc *p;
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;
217 // For pthreadperthread mode, procswitch flips
218 // between the threads.
219 static void
220 procswitch(Proc *p, _Thread *from, _Thread *to)
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);
231 if(p->schedthread != from) {
232 if(from->exiting) {
233 unlock(&p->schedlock);
234 _threadpexit();
235 _threaddebug("procswitch exit wakeup!!!\n");
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);
242 if(p->schedthread != from)
243 sysfatal("_procswitch %p %p oops", p->schedthread, from);
245 unlock(&p->schedlock);
248 void
249 _threadswitch(void)
251 Proc *p;
253 needstack(0);
254 p = proc();
256 /*print("threadswtch %p\n", p); */
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);
266 void
267 _threadready(_Thread *t)
269 Proc *p;
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);
282 int
283 threadidle(void)
285 int n;
286 Proc *p;
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;
298 int
299 threadyield(void)
301 int n;
302 Proc *p;
304 p = proc();
305 n = p->nswitch;
306 _threadready(p->thread);
307 _threadswitch();
308 return p->nswitch - n;
311 void
312 threadexits(char *msg)
314 Proc *p;
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();
324 void
325 threadpin(void)
327 Proc *p;
329 p = proc();
330 if(p->pinthread){
331 fprint(2, "already pinning a thread - %p %p\n", p->pinthread, p->thread);
332 assert(0);
334 p->pinthread = p->thread;
337 void
338 threadunpin(void)
340 Proc *p;
342 p = proc();
343 if(p->pinthread != p->thread){
344 fprint(2, "wrong pinthread - %p %p\n", p->pinthread, p->thread);
345 assert(0);
347 p->pinthread = nil;
350 void
351 threadsysfatal(char *fmt, va_list arg)
353 char buf[256];
355 vseprint(buf, buf+sizeof(buf), fmt, arg);
356 __fixargv0();
357 fprint(2, "%s: %s\n", argv0 ? argv0 : "<prog>", buf);
358 threadexitsall(buf);
361 static void
362 contextswitch(Context *from, Context *to)
364 if(swapcontext(&from->uc, &to->uc) < 0){
365 fprint(2, "swapcontext failed: %r\n");
366 assert(0);
370 static void
371 procmain(Proc *p)
373 _Thread *t;
375 _threadsetproc(p);
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);
382 /* run it */
383 p->thread = t;
384 t->startfn(t->startarg);
385 if(p->nthread != 0)
386 threadexits(nil);
389 void
390 _threadpthreadmain(Proc *p, _Thread *t)
392 _threadsetproc(p);
393 procswitch(p, t, nil);
394 t->startfn(t->startarg);
395 threadexits(nil);
398 static void
399 procscheduler(Proc *p)
401 _Thread *t;
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);
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)");
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);
437 continue;
439 p->runrend.l = &p->lock;
440 _threaddebug("scheduler sleep");
441 _procsleep(&p->runrend);
442 _threaddebug("scheduler wake");
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;
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);
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();
497 void
498 _threadsetsysproc(void)
500 lock(&threadnproclock);
501 if(++threadnsysproc == threadnproc)
502 threadexitsall(nil);
503 unlock(&threadnproclock);
504 proc()->sysproc = 1;
507 void**
508 procdata(void)
510 return &proc()->udata;
513 void**
514 threaddata(void)
516 return &proc()->thread->udata;
519 extern Jmp *(*_notejmpbuf)(void);
520 static Jmp*
521 threadnotejmp(void)
523 return &proc()->sigjmp;
526 /*
527 * debugging
528 */
529 void
530 threadsetname(char *fmt, ...)
532 va_list arg;
533 _Thread *t;
535 t = proc()->thread;
536 va_start(arg, fmt);
537 vsnprint(t->name, sizeof t->name, fmt, arg);
538 va_end(arg);
541 char*
542 threadgetname(void)
544 return proc()->thread->name;
547 void
548 threadsetstate(char *fmt, ...)
550 va_list arg;
551 _Thread *t;
553 t = proc()->thread;
554 va_start(arg, fmt);
555 vsnprint(t->state, sizeof t->name, fmt, arg);
556 va_end(arg);
559 int
560 threadid(void)
562 _Thread *t;
564 t = proc()->thread;
565 return t->id;
568 void
569 needstack(int n)
571 _Thread *t;
573 t = proc()->thread;
574 if(t->stk == nil)
575 return;
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();
584 static int
585 singlethreaded(void)
587 return threadnproc == 1 && _threadprocs->nthread == 1;
590 /*
591 * locking
592 */
593 static int
594 threadqlock(QLock *l, int block, ulong pc)
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();
605 unlock(&l->l);
606 return 1;
608 if(!block){
609 unlock(&l->l);
610 return 0;
613 if(singlethreaded()){
614 fprint(2, "qlock deadlock\n");
615 abort();
618 /*print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)()); */
619 addthread(&l->waiting, (*threadnow)());
620 unlock(&l->l);
622 _threadswitch();
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();
629 if(l->owner == nil) {
630 fprint(2, "%s: qlock threadswitch owner=nil oops\n", argv0);
631 abort();
634 /*print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)()); */
635 return 1;
638 static void
639 threadqunlock(QLock *l, ulong pc)
641 _Thread *ready;
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();
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);
664 static int
665 threadrlock(RWLock *l, int block, ulong pc)
667 USED(pc);
669 lock(&l->l);
670 if(l->writer == nil && l->wwaiting.head == nil){
671 l->readers++;
672 unlock(&l->l);
673 return 1;
675 if(!block){
676 unlock(&l->l);
677 return 0;
679 if(singlethreaded()){
680 fprint(2, "rlock deadlock\n");
681 abort();
683 addthread(&l->rwaiting, (*threadnow)());
684 unlock(&l->l);
685 _threadswitch();
686 return 1;
689 static int
690 threadwlock(RWLock *l, int block, ulong pc)
692 USED(pc);
694 lock(&l->l);
695 if(l->writer == nil && l->readers == 0){
696 l->writer = (*threadnow)();
697 unlock(&l->l);
698 return 1;
700 if(!block){
701 unlock(&l->l);
702 return 0;
704 if(singlethreaded()){
705 fprint(2, "wlock deadlock\n");
706 abort();
708 addthread(&l->wwaiting, (*threadnow)());
709 unlock(&l->l);
710 _threadswitch();
711 return 1;
714 static void
715 threadrunlock(RWLock *l, ulong pc)
717 _Thread *t;
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;
727 unlock(&l->l);
728 if(t)
729 _threadready(t);
733 static void
734 threadwunlock(RWLock *l, ulong pc)
736 _Thread *t;
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);
747 t = nil;
748 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
749 delthread(&l->wwaiting, t);
750 l->writer = t;
752 unlock(&l->l);
753 if(t)
754 _threadready(t);
757 /*
758 * sleep and wakeup
759 */
760 static void
761 threadrsleep(Rendez *r, ulong pc)
763 if(singlethreaded()){
764 fprint(2, "rsleep deadlock\n");
765 abort();
767 addthread(&r->waiting, proc()->thread);
768 qunlock(r->l);
769 _threadswitch();
770 qlock(r->l);
773 static int
774 threadrwakeup(Rendez *r, int all, ulong pc)
776 int i;
777 _Thread *t;
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);
787 return i;
790 /*
791 * startup
792 */
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
799 threadmainstart(void *v)
801 USED(v);
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);
816 extern void (*_sysfatal)(char*, va_list);
818 int
819 main(int argc, char **argv)
821 Proc *p;
822 char *opts;
824 argv0 = argv[0];
826 opts = getenv("LIBTHREAD");
827 if(opts == nil)
828 opts = "";
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();
842 threadargc = argc;
843 threadargv = argv;
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;
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;
877 /*
878 * hooray for linked lists
879 */
880 static void
881 addthread(_Threadlist *l, _Thread *t)
883 if(l->tail){
884 l->tail->next = t;
885 t->prev = l->tail;
886 }else{
887 l->head = t;
888 t->prev = nil;
890 l->tail = t;
891 t->next = nil;
894 static void
895 delthread(_Threadlist *l, _Thread *t)
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;
907 /* inefficient but rarely used */
908 static int
909 onlist(_Threadlist *l, _Thread *t)
911 _Thread *tt;
913 for(tt = l->head; tt; tt=tt->next)
914 if(tt == t)
915 return 1;
916 return 0;
919 static void
920 addthreadinproc(Proc *p, _Thread *t)
922 _Threadlist *l;
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;
932 l->tail = t;
933 t->allnext = nil;
936 static void
937 delthreadinproc(Proc *p, _Thread *t)
939 _Threadlist *l;
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;
952 Proc *_threadprocs;
953 Lock _threadprocslock;
954 static Proc *_threadprocstail;
956 static void
957 addproc(Proc *p)
959 lock(&_threadprocslock);
960 if(_threadprocstail){
961 _threadprocstail->next = p;
962 p->prev = _threadprocstail;
963 }else{
964 _threadprocs = p;
965 p->prev = nil;
967 _threadprocstail = p;
968 p->next = nil;
969 unlock(&_threadprocslock);
972 static void
973 delproc(Proc *p)
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);
987 /*
988 * notify - for now just use the usual mechanisms
989 */
990 void
991 threadnotify(int (*f)(void*, char*), int in)
993 atnotify(f, in);
996 static int
997 onrunqueue(Proc *p, _Thread *t)
999 _Thread *tt;
1001 for(tt=p->runqueue.head; tt; tt=tt->next)
1002 if(tt == t)
1003 return 1;
1004 return 0;
1008 * print state - called from SIGINFO
1010 static int
1011 threadinfo(void *v, char *s)
1013 Proc *p;
1014 _Thread *t;
1016 if(strcmp(s, "quit") != 0 && strcmp(s, "sys: status request") != 0)
1017 return 0;
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);
1030 return 1;