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;
11 static void addproc(Proc*);
12 static void delproc(Proc*);
13 static void addthread(_Threadlist*, _Thread*);
14 static void delthread(_Threadlist*, _Thread*);
15 static int onlist(_Threadlist*, _Thread*);
16 static void addthreadinproc(Proc*, _Thread*);
17 static void delthreadinproc(Proc*, _Thread*);
18 static void contextswitch(Context *from, Context *to);
19 static void procmain(Proc*);
20 static void procscheduler(Proc*);
21 static int threadinfo(void*, char*);
23 static void
24 _threaddebug(char *fmt, ...)
25 {
26 va_list arg;
27 char buf[128];
28 _Thread *t;
29 char *p;
30 static int fd = -1;
32 if(_threaddebuglevel == 0)
33 return;
35 if(fd < 0){
36 p = strrchr(argv0, '/');
37 if(p)
38 p++;
39 else
40 p = argv0;
41 snprint(buf, sizeof buf, "/tmp/%s.tlog", p);
42 if((fd = create(buf, OWRITE, 0666)) < 0)
43 fd = open("/dev/null", OWRITE);
44 if(fd >= 0 && fd != 2){
45 dup(fd, 2);
46 close(fd);
47 fd = 2;
48 }
49 }
51 va_start(arg, fmt);
52 vsnprint(buf, sizeof buf, fmt, arg);
53 va_end(arg);
54 t = proc()->thread;
55 if(t)
56 fprint(fd, "%d.%d: %s\n", getpid(), t->id, buf);
57 else
58 fprint(fd, "%d._: %s\n", getpid(), buf);
59 }
61 static _Thread*
62 getthreadnow(void)
63 {
64 return proc()->thread;
65 }
66 _Thread *(*threadnow)(void) = getthreadnow;
68 static Proc*
69 procalloc(void)
70 {
71 Proc *p;
73 p = malloc(sizeof *p);
74 if(p == nil)
75 sysfatal("procalloc malloc: %r");
76 memset(p, 0, sizeof *p);
77 addproc(p);
78 lock(&threadnproclock);
79 threadnproc++;
80 unlock(&threadnproclock);
81 return p;
82 }
84 static void
85 threadstart(uint y, uint x)
86 {
87 _Thread *t;
88 ulong z;
90 //print("threadstart\n");
91 z = (ulong)x << 16; /* hide undefined 32-bit shift from 32-bit compilers */
92 z <<= 16;
93 z |= y;
94 t = (_Thread*)z;
96 //print("threadstart sp=%p arg=%p startfn=%p t=%p\n", &t, t, t->startfn, t->startarg);
97 t->startfn(t->startarg);
98 /*print("threadexits %p\n", v); */
99 threadexits(nil);
100 /*print("not reacehd\n"); */
103 static _Thread*
104 threadalloc(void (*fn)(void*), void *arg, uint stack)
106 _Thread *t;
107 sigset_t zero;
108 uint x, y;
109 ulong z;
111 /* allocate the task and stack together */
112 t = malloc(sizeof *t);
113 if(t == nil)
114 sysfatal("threadalloc malloc: %r");
115 memset(t, 0, sizeof *t);
116 t->id = incref(&threadidref);
117 //print("fn=%p arg=%p\n", fn, arg);
118 t->startfn = fn;
119 t->startarg = arg;
120 //print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn);
122 /* do a reasonable initialization */
123 if(stack == 0)
124 return t;
125 t->stk = _threadstkalloc(stack);
126 if(t->stk == nil)
127 sysfatal("threadalloc malloc stack: %r");
128 t->stksize = stack;
129 memset(&t->context.uc, 0, sizeof t->context.uc);
130 sigemptyset(&zero);
131 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
132 //print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn);
134 /* must initialize with current context */
135 if(getcontext(&t->context.uc) < 0)
136 sysfatal("threadalloc getcontext: %r");
137 //print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn);
139 /* call makecontext to do the real work. */
140 /* leave a few words open on both ends */
141 t->context.uc.uc_stack.ss_sp = (void*)(t->stk+8);
142 t->context.uc.uc_stack.ss_size = t->stksize-64;
143 #if defined(__sun__) && !defined(__MAKECONTEXT_V2_SOURCE) /* sigh */
144 /* can avoid this with __MAKECONTEXT_V2_SOURCE but only on SunOS 5.9 */
145 t->context.uc.uc_stack.ss_sp =
146 (char*)t->context.uc.uc_stack.ss_sp
147 +t->context.uc.uc_stack.ss_size;
148 #endif
149 /*
150 * All this magic is because you have to pass makecontext a
151 * function that takes some number of word-sized variables,
152 * and on 64-bit machines pointers are bigger than words.
153 */
154 //print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn);
155 z = (ulong)t;
156 y = z;
157 z >>= 16; /* hide undefined 32-bit shift from 32-bit compilers */
158 x = z>>16;
159 makecontext(&t->context.uc, (void(*)(void))threadstart, 2, y, x);
161 return t;
164 _Thread*
165 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
167 _Thread *t;
169 /* defend against bad C libraries */
170 if(stack < (256<<10))
171 stack = 256<<10;
173 if(p->nthread == 0)
174 stack = 0; // not using it
175 t = threadalloc(fn, arg, stack);
176 t->proc = p;
177 addthreadinproc(p, t);
178 p->nthread++;
179 _threadready(t);
180 return t;
183 int
184 threadcreate(void (*fn)(void*), void *arg, uint stack)
186 _Thread *t;
188 t = _threadcreate(proc(), fn, arg, stack);
189 return t->id;
192 int
193 proccreate(void (*fn)(void*), void *arg, uint stack)
195 int id;
196 _Thread *t;
197 Proc *p;
199 p = procalloc();
200 t = _threadcreate(p, fn, arg, stack);
201 id = t->id; /* t might be freed after _procstart */
202 _procstart(p, procmain);
203 return id;
206 void
207 _threadswitch(void)
209 Proc *p;
211 needstack(0);
212 p = proc();
213 /*print("threadswtch %p\n", p); */
214 if(p->thread->stk == nil)
215 procscheduler(p);
216 else
217 contextswitch(&p->thread->context, &p->schedcontext);
220 void
221 _threadready(_Thread *t)
223 Proc *p;
225 p = t->proc;
226 lock(&p->lock);
227 p->runrend.l = &p->lock;
228 addthread(&p->runqueue, t);
229 /*print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid); */
230 if(p != proc())
231 _procwakeupandunlock(&p->runrend);
232 else
233 unlock(&p->lock);
236 int
237 threadidle(void)
239 int n;
240 Proc *p;
242 p = proc();
243 n = p->nswitch;
244 lock(&p->lock);
245 p->runrend.l = &p->lock;
246 addthread(&p->idlequeue, p->thread);
247 unlock(&p->lock);
248 _threadswitch();
249 return p->nswitch - n;
252 int
253 threadyield(void)
255 int n;
256 Proc *p;
258 p = proc();
259 n = p->nswitch;
260 _threadready(p->thread);
261 _threadswitch();
262 return p->nswitch - n;
265 void
266 threadexits(char *msg)
268 Proc *p;
270 p = proc();
271 if(msg == nil)
272 msg = "";
273 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
274 proc()->thread->exiting = 1;
275 _threadswitch();
278 void
279 threadpin(void)
281 Proc *p;
283 p = proc();
284 if(p->pinthread){
285 fprint(2, "already pinning a thread - %p %p\n", p->pinthread, p->thread);
286 assert(0);
288 p->pinthread = p->thread;
291 void
292 threadunpin(void)
294 Proc *p;
296 p = proc();
297 if(p->pinthread != p->thread){
298 fprint(2, "wrong pinthread - %p %p\n", p->pinthread, p->thread);
299 assert(0);
301 p->pinthread = nil;
304 void
305 threadsysfatal(char *fmt, va_list arg)
307 char buf[256];
309 vseprint(buf, buf+sizeof(buf), fmt, arg);
310 __fixargv0();
311 fprint(2, "%s: %s\n", argv0 ? argv0 : "<prog>", buf);
312 threadexitsall(buf);
315 static void
316 contextswitch(Context *from, Context *to)
318 if(swapcontext(&from->uc, &to->uc) < 0){
319 fprint(2, "swapcontext failed: %r\n");
320 assert(0);
324 static void
325 procmain(Proc *p)
327 _Thread *t;
329 _threadsetproc(p);
331 /* take out first thread to run on system stack */
332 t = p->runqueue.head;
333 delthread(&p->runqueue, t);
334 memset(&t->context.uc, 0, sizeof t->context.uc);
336 /* run it */
337 p->thread = t;
338 t->startfn(t->startarg);
339 if(p->nthread != 0)
340 threadexits(nil);
343 static void
344 procscheduler(Proc *p)
346 _Thread *t;
348 _threaddebug("scheduler enter");
349 //print("s %p\n", p);
350 Top:
351 lock(&p->lock);
352 t = p->thread;
353 p->thread = nil;
354 if(t->exiting){
355 delthreadinproc(p, t);
356 p->nthread--;
357 /*print("nthread %d\n", p->nthread); */
358 _threadstkfree(t->stk, t->stksize);
359 free(t);
362 for(;;){
363 if((t = p->pinthread) != nil){
364 while(!onlist(&p->runqueue, t)){
365 p->runrend.l = &p->lock;
366 _threaddebug("scheduler sleep (pin)");
367 _procsleep(&p->runrend);
368 _threaddebug("scheduler wake (pin)");
370 }else
371 while((t = p->runqueue.head) == nil){
372 if(p->nthread == 0)
373 goto Out;
374 if((t = p->idlequeue.head) != nil){
375 /*
376 * Run all the idling threads once.
377 */
378 while((t = p->idlequeue.head) != nil){
379 delthread(&p->idlequeue, t);
380 addthread(&p->runqueue, t);
382 continue;
384 p->runrend.l = &p->lock;
385 _threaddebug("scheduler sleep");
386 _procsleep(&p->runrend);
387 _threaddebug("scheduler wake");
389 if(p->pinthread && p->pinthread != t)
390 fprint(2, "p->pinthread %p t %p\n", p->pinthread, t);
391 assert(p->pinthread == nil || p->pinthread == t);
392 delthread(&p->runqueue, t);
393 unlock(&p->lock);
394 p->thread = t;
395 p->nswitch++;
396 _threaddebug("run %d (%s)", t->id, t->name);
397 //print("run %p %p %p %p\n", t, *(uintptr*)(t->context.uc.mc.sp), t->context.uc.mc.di, t->context.uc.mc.si);
398 if(t->stk == nil)
399 return;
400 contextswitch(&p->schedcontext, &t->context);
401 /*print("back in scheduler\n"); */
402 goto Top;
405 Out:
406 _threaddebug("scheduler exit");
407 if(p->mainproc){
408 /*
409 * Stupid bug - on Linux 2.6 and maybe elsewhere,
410 * if the main thread exits then the others keep running
411 * but the process shows up as a zombie in ps and is not
412 * attachable with ptrace. We'll just sit around pretending
413 * to be a system proc instead of exiting.
414 */
415 _threaddaemonize();
416 lock(&threadnproclock);
417 if(++threadnsysproc == threadnproc)
418 threadexitsall(p->msg);
419 p->sysproc = 1;
420 unlock(&threadnproclock);
421 for(;;)
422 sleep(1000);
425 delproc(p);
426 lock(&threadnproclock);
427 if(p->sysproc)
428 --threadnsysproc;
429 if(--threadnproc == threadnsysproc)
430 threadexitsall(p->msg);
431 unlock(&threadnproclock);
432 unlock(&p->lock);
433 _threadsetproc(nil);
434 free(p);
435 _threadpexit();
438 void
439 _threadsetsysproc(void)
441 lock(&threadnproclock);
442 if(++threadnsysproc == threadnproc)
443 threadexitsall(nil);
444 unlock(&threadnproclock);
445 proc()->sysproc = 1;
448 void**
449 procdata(void)
451 return &proc()->udata;
454 void**
455 threaddata(void)
457 return &proc()->thread->udata;
460 extern Jmp *(*_notejmpbuf)(void);
461 static Jmp*
462 threadnotejmp(void)
464 return &proc()->sigjmp;
467 /*
468 * debugging
469 */
470 void
471 threadsetname(char *fmt, ...)
473 va_list arg;
474 _Thread *t;
476 t = proc()->thread;
477 va_start(arg, fmt);
478 vsnprint(t->name, sizeof t->name, fmt, arg);
479 va_end(arg);
482 char*
483 threadgetname(void)
485 return proc()->thread->name;
488 void
489 threadsetstate(char *fmt, ...)
491 va_list arg;
492 _Thread *t;
494 t = proc()->thread;
495 va_start(arg, fmt);
496 vsnprint(t->state, sizeof t->name, fmt, arg);
497 va_end(arg);
500 int
501 threadid(void)
503 _Thread *t;
505 t = proc()->thread;
506 return t->id;
509 void
510 needstack(int n)
512 _Thread *t;
514 t = proc()->thread;
515 if(t->stk == nil)
516 return;
518 if((char*)&t <= (char*)t->stk
519 || (char*)&t - (char*)t->stk < 256+n){
520 fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
521 abort();
525 static int
526 singlethreaded(void)
528 return threadnproc == 1 && _threadprocs->nthread == 1;
531 /*
532 * locking
533 */
534 static int
535 threadqlock(QLock *l, int block, ulong pc)
537 /*print("threadqlock %p\n", l); */
538 lock(&l->l);
539 if(l->owner == nil){
540 l->owner = (*threadnow)();
541 /*print("qlock %p @%#x by %p\n", l, pc, l->owner); */
542 unlock(&l->l);
543 return 1;
545 if(!block){
546 unlock(&l->l);
547 return 0;
550 if(singlethreaded()){
551 fprint(2, "qlock deadlock\n");
552 abort();
555 /*print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)()); */
556 addthread(&l->waiting, (*threadnow)());
557 unlock(&l->l);
559 _threadswitch();
561 if(l->owner != (*threadnow)()){
562 fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
563 argv0, pc, l->owner, (*threadnow)());
564 abort();
566 /*print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)()); */
567 return 1;
570 static void
571 threadqunlock(QLock *l, ulong pc)
573 _Thread *ready;
575 lock(&l->l);
576 /*print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner); */
577 if(l->owner == 0){
578 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
579 argv0, pc, l->owner, (*threadnow)());
580 abort();
582 if((l->owner = ready = l->waiting.head) != nil)
583 delthread(&l->waiting, l->owner);
584 /*
585 * N.B. Cannot call _threadready() before unlocking l->l,
586 * because the thread we are readying might:
587 * - be in another proc
588 * - start running immediately
589 * - and free l before we get a chance to run again
590 */
591 unlock(&l->l);
592 if(ready)
593 _threadready(l->owner);
596 static int
597 threadrlock(RWLock *l, int block, ulong pc)
599 USED(pc);
601 lock(&l->l);
602 if(l->writer == nil && l->wwaiting.head == nil){
603 l->readers++;
604 unlock(&l->l);
605 return 1;
607 if(!block){
608 unlock(&l->l);
609 return 0;
611 if(singlethreaded()){
612 fprint(2, "rlock deadlock\n");
613 abort();
615 addthread(&l->rwaiting, (*threadnow)());
616 unlock(&l->l);
617 _threadswitch();
618 return 1;
621 static int
622 threadwlock(RWLock *l, int block, ulong pc)
624 USED(pc);
626 lock(&l->l);
627 if(l->writer == nil && l->readers == 0){
628 l->writer = (*threadnow)();
629 unlock(&l->l);
630 return 1;
632 if(!block){
633 unlock(&l->l);
634 return 0;
636 if(singlethreaded()){
637 fprint(2, "wlock deadlock\n");
638 abort();
640 addthread(&l->wwaiting, (*threadnow)());
641 unlock(&l->l);
642 _threadswitch();
643 return 1;
646 static void
647 threadrunlock(RWLock *l, ulong pc)
649 _Thread *t;
651 USED(pc);
652 t = nil;
653 lock(&l->l);
654 --l->readers;
655 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
656 delthread(&l->wwaiting, t);
657 l->writer = t;
659 unlock(&l->l);
660 if(t)
661 _threadready(t);
665 static void
666 threadwunlock(RWLock *l, ulong pc)
668 _Thread *t;
670 USED(pc);
671 lock(&l->l);
672 l->writer = nil;
673 assert(l->readers == 0);
674 while((t = l->rwaiting.head) != nil){
675 delthread(&l->rwaiting, t);
676 l->readers++;
677 _threadready(t);
679 t = nil;
680 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
681 delthread(&l->wwaiting, t);
682 l->writer = t;
684 unlock(&l->l);
685 if(t)
686 _threadready(t);
689 /*
690 * sleep and wakeup
691 */
692 static void
693 threadrsleep(Rendez *r, ulong pc)
695 if(singlethreaded()){
696 fprint(2, "rsleep deadlock\n");
697 abort();
699 addthread(&r->waiting, proc()->thread);
700 qunlock(r->l);
701 _threadswitch();
702 qlock(r->l);
705 static int
706 threadrwakeup(Rendez *r, int all, ulong pc)
708 int i;
709 _Thread *t;
711 for(i=0;; i++){
712 if(i==1 && !all)
713 break;
714 if((t = r->waiting.head) == nil)
715 break;
716 delthread(&r->waiting, t);
717 _threadready(t);
719 return i;
722 /*
723 * startup
724 */
726 static int threadargc;
727 static char **threadargv;
728 int mainstacksize;
729 extern int _p9usepwlibrary; /* getgrgid etc. smash the stack - tell _p9dir just say no */
730 static void
731 threadmainstart(void *v)
733 USED(v);
735 /*
736 * N.B. This call to proc() is a program's first call (indirectly) to a
737 * pthreads function while executing on a non-pthreads-allocated
738 * stack. If the pthreads implementation is using the stack pointer
739 * to locate the per-thread data, then this call will blow up.
740 * This means the pthread implementation is not suitable for
741 * running under libthread. Time to write your own. Sorry.
742 */
743 _p9usepwlibrary = 0;
744 threadmainproc = proc();
745 threadmain(threadargc, threadargv);
748 extern void (*_sysfatal)(char*, va_list);
750 int
751 main(int argc, char **argv)
753 Proc *p;
755 argv0 = argv[0];
757 if(getenv("NOLIBTHREADDAEMONIZE") == nil)
758 _threadsetupdaemonize();
760 threadargc = argc;
761 threadargv = argv;
763 /*
764 * Install locking routines into C library.
765 */
766 _lock = _threadlock;
767 _unlock = _threadunlock;
768 _qlock = threadqlock;
769 _qunlock = threadqunlock;
770 _rlock = threadrlock;
771 _runlock = threadrunlock;
772 _wlock = threadwlock;
773 _wunlock = threadwunlock;
774 _rsleep = threadrsleep;
775 _rwakeup = threadrwakeup;
776 _notejmpbuf = threadnotejmp;
777 _pin = threadpin;
778 _unpin = threadunpin;
779 _sysfatal = threadsysfatal;
781 _pthreadinit();
782 p = procalloc();
783 p->mainproc = 1;
784 _threadsetproc(p);
785 if(mainstacksize == 0)
786 mainstacksize = 256*1024;
787 atnotify(threadinfo, 1);
788 _threadcreate(p, threadmainstart, nil, mainstacksize);
789 procmain(p);
790 sysfatal("procscheduler returned in threadmain!");
791 /* does not return */
792 return 0;
795 /*
796 * hooray for linked lists
797 */
798 static void
799 addthread(_Threadlist *l, _Thread *t)
801 if(l->tail){
802 l->tail->next = t;
803 t->prev = l->tail;
804 }else{
805 l->head = t;
806 t->prev = nil;
808 l->tail = t;
809 t->next = nil;
812 static void
813 delthread(_Threadlist *l, _Thread *t)
815 if(t->prev)
816 t->prev->next = t->next;
817 else
818 l->head = t->next;
819 if(t->next)
820 t->next->prev = t->prev;
821 else
822 l->tail = t->prev;
825 /* inefficient but rarely used */
826 static int
827 onlist(_Threadlist *l, _Thread *t)
829 _Thread *tt;
831 for(tt = l->head; tt; tt=tt->next)
832 if(tt == t)
833 return 1;
834 return 0;
837 static void
838 addthreadinproc(Proc *p, _Thread *t)
840 _Threadlist *l;
842 l = &p->allthreads;
843 if(l->tail){
844 l->tail->allnext = t;
845 t->allprev = l->tail;
846 }else{
847 l->head = t;
848 t->allprev = nil;
850 l->tail = t;
851 t->allnext = nil;
854 static void
855 delthreadinproc(Proc *p, _Thread *t)
857 _Threadlist *l;
859 l = &p->allthreads;
860 if(t->allprev)
861 t->allprev->allnext = t->allnext;
862 else
863 l->head = t->allnext;
864 if(t->allnext)
865 t->allnext->allprev = t->allprev;
866 else
867 l->tail = t->allprev;
870 Proc *_threadprocs;
871 Lock _threadprocslock;
872 static Proc *_threadprocstail;
874 static void
875 addproc(Proc *p)
877 lock(&_threadprocslock);
878 if(_threadprocstail){
879 _threadprocstail->next = p;
880 p->prev = _threadprocstail;
881 }else{
882 _threadprocs = p;
883 p->prev = nil;
885 _threadprocstail = p;
886 p->next = nil;
887 unlock(&_threadprocslock);
890 static void
891 delproc(Proc *p)
893 lock(&_threadprocslock);
894 if(p->prev)
895 p->prev->next = p->next;
896 else
897 _threadprocs = p->next;
898 if(p->next)
899 p->next->prev = p->prev;
900 else
901 _threadprocstail = p->prev;
902 unlock(&_threadprocslock);
905 /*
906 * notify - for now just use the usual mechanisms
907 */
908 void
909 threadnotify(int (*f)(void*, char*), int in)
911 atnotify(f, in);
914 static int
915 onrunqueue(Proc *p, _Thread *t)
917 _Thread *tt;
919 for(tt=p->runqueue.head; tt; tt=tt->next)
920 if(tt == t)
921 return 1;
922 return 0;
925 /*
926 * print state - called from SIGINFO
927 */
928 static int
929 threadinfo(void *v, char *s)
931 Proc *p;
932 _Thread *t;
934 if(strcmp(s, "quit") != 0 && strcmp(s, "sys: status request") != 0)
935 return 0;
937 for(p=_threadprocs; p; p=p->next){
938 fprint(2, "proc %p %s%s\n", (void*)p->osprocid, p->msg,
939 p->sysproc ? " (sysproc)": "");
940 for(t=p->allthreads.head; t; t=t->allnext){
941 fprint(2, "\tthread %d %s: %s %s\n",
942 t->id,
943 t == p->thread ? "Running" :
944 onrunqueue(p, t) ? "Ready" : "Sleeping",
945 t->state, t->name);
948 return 1;