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 procscheduler(Proc*);
20 static int threadinfo(void*, char*);
22 static void
23 _threaddebug(char *fmt, ...)
24 {
25 va_list arg;
26 char buf[128];
27 _Thread *t;
28 char *p;
29 static int fd = -1;
31 if(_threaddebuglevel == 0)
32 return;
34 if(fd < 0){
35 p = strrchr(argv0, '/');
36 if(p)
37 p++;
38 else
39 p = argv0;
40 snprint(buf, sizeof buf, "/tmp/%s.tlog", p);
41 if((fd = create(buf, OWRITE, 0666)) < 0)
42 fd = open("/dev/null", OWRITE);
43 if(fd >= 0 && fd != 2){
44 dup(fd, 2);
45 close(fd);
46 fd = 2;
47 }
48 }
50 va_start(arg, fmt);
51 vsnprint(buf, sizeof buf, fmt, arg);
52 va_end(arg);
53 t = proc()->thread;
54 if(t)
55 fprint(fd, "%d.%d: %s\n", getpid(), t->id, buf);
56 else
57 fprint(fd, "%d._: %s\n", getpid(), buf);
58 }
60 static _Thread*
61 getthreadnow(void)
62 {
63 return proc()->thread;
64 }
65 _Thread *(*threadnow)(void) = getthreadnow;
67 static Proc*
68 procalloc(void)
69 {
70 Proc *p;
72 p = malloc(sizeof *p);
73 if(p == nil)
74 sysfatal("procalloc malloc: %r");
75 memset(p, 0, sizeof *p);
76 addproc(p);
77 lock(&threadnproclock);
78 threadnproc++;
79 unlock(&threadnproclock);
80 return p;
81 }
83 static void
84 threadstart(uint y, uint x)
85 {
86 _Thread *t;
87 ulong z;
89 z = (ulong)x << 16; /* hide undefined 32-bit shift from 32-bit compilers */
90 z <<= 16;
91 z |= y;
92 t = (_Thread*)z;
94 /*print("threadstart %p\n", v); */
95 t->startfn(t->startarg);
96 /*print("threadexits %p\n", v); */
97 threadexits(nil);
98 /*print("not reacehd\n"); */
99 }
101 static _Thread*
102 threadalloc(void (*fn)(void*), void *arg, uint stack)
104 _Thread *t;
105 sigset_t zero;
106 uint x, y;
107 ulong z;
109 /* allocate the task and stack together */
110 t = malloc(sizeof *t+stack);
111 if(t == nil)
112 sysfatal("threadalloc malloc: %r");
113 memset(t, 0, sizeof *t);
114 t->stk = (uchar*)(t+1);
115 t->stksize = stack;
116 t->id = incref(&threadidref);
117 t->startfn = fn;
118 t->startarg = arg;
120 /* do a reasonable initialization */
121 memset(&t->context.uc, 0, sizeof t->context.uc);
122 sigemptyset(&zero);
123 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
125 /* must initialize with current context */
126 if(getcontext(&t->context.uc) < 0)
127 sysfatal("threadalloc getcontext: %r");
129 /* call makecontext to do the real work. */
130 /* leave a few words open on both ends */
131 t->context.uc.uc_stack.ss_sp = (void*)(t->stk+8);
132 t->context.uc.uc_stack.ss_size = t->stksize-64;
133 #if defined(__sun__) && !defined(__MAKECONTEXT_V2_SOURCE) /* sigh */
134 /* can avoid this with __MAKECONTEXT_V2_SOURCE but only on SunOS 5.9 */
135 t->context.uc.uc_stack.ss_sp =
136 (char*)t->context.uc.uc_stack.ss_sp
137 +t->context.uc.uc_stack.ss_size;
138 #endif
139 /*
140 * All this magic is because you have to pass makecontext a
141 * function that takes some number of word-sized variables,
142 * and on 64-bit machines pointers are bigger than words.
143 */
144 z = (ulong)t;
145 y = z;
146 z >>= 16; /* hide undefined 32-bit shift from 32-bit compilers */
147 x = z>>16;
148 makecontext(&t->context.uc, (void(*)(void))threadstart, 2, y, x);
150 return t;
153 _Thread*
154 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
156 _Thread *t;
158 /* defend against bad C libraries */
159 if(stack < (256<<10))
160 stack = 256<<10;
162 t = threadalloc(fn, arg, stack);
163 t->proc = p;
164 addthreadinproc(p, t);
165 p->nthread++;
166 _threadready(t);
167 return t;
170 int
171 threadcreate(void (*fn)(void*), void *arg, uint stack)
173 _Thread *t;
175 t = _threadcreate(proc(), fn, arg, stack);
176 return t->id;
179 int
180 proccreate(void (*fn)(void*), void *arg, uint stack)
182 int id;
183 _Thread *t;
184 Proc *p;
186 p = procalloc();
187 t = _threadcreate(p, fn, arg, stack);
188 id = t->id; /* t might be freed after _procstart */
189 _procstart(p, procscheduler);
190 return id;
193 void
194 _threadswitch(void)
196 Proc *p;
198 needstack(0);
199 p = proc();
200 /*print("threadswtch %p\n", p); */
201 contextswitch(&p->thread->context, &p->schedcontext);
204 void
205 _threadready(_Thread *t)
207 Proc *p;
209 p = t->proc;
210 lock(&p->lock);
211 p->runrend.l = &p->lock;
212 addthread(&p->runqueue, t);
213 /*print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid); */
214 if(p != proc())
215 _procwakeupandunlock(&p->runrend);
216 else
217 unlock(&p->lock);
220 int
221 threadidle(void)
223 int n;
224 Proc *p;
226 p = proc();
227 n = p->nswitch;
228 lock(&p->lock);
229 p->runrend.l = &p->lock;
230 addthread(&p->idlequeue, p->thread);
231 unlock(&p->lock);
232 _threadswitch();
233 return p->nswitch - n;
236 int
237 threadyield(void)
239 int n;
240 Proc *p;
242 p = proc();
243 n = p->nswitch;
244 _threadready(p->thread);
245 _threadswitch();
246 return p->nswitch - n;
249 void
250 threadexits(char *msg)
252 Proc *p;
254 p = proc();
255 if(msg == nil)
256 msg = "";
257 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
258 proc()->thread->exiting = 1;
259 _threadswitch();
262 void
263 threadpin(void)
265 Proc *p;
267 p = proc();
268 if(p->pinthread){
269 fprint(2, "already pinning a thread - %p %p\n", p->pinthread, p->thread);
270 assert(0);
272 p->pinthread = p->thread;
275 void
276 threadunpin(void)
278 Proc *p;
280 p = proc();
281 if(p->pinthread != p->thread){
282 fprint(2, "wrong pinthread - %p %p\n", p->pinthread, p->thread);
283 assert(0);
285 p->pinthread = nil;
288 void
289 threadsysfatal(char *fmt, va_list arg)
291 char buf[256];
293 vseprint(buf, buf+sizeof(buf), fmt, arg);
294 __fixargv0();
295 fprint(2, "%s: %s\n", argv0 ? argv0 : "<prog>", buf);
296 threadexitsall(buf);
299 static void
300 contextswitch(Context *from, Context *to)
302 if(swapcontext(&from->uc, &to->uc) < 0){
303 fprint(2, "swapcontext failed: %r\n");
304 assert(0);
308 static void
309 procscheduler(Proc *p)
311 _Thread *t;
313 setproc(p);
314 _threaddebug("scheduler enter");
315 /* print("s %p\n", p); */
316 lock(&p->lock);
317 for(;;){
318 if((t = p->pinthread) != nil){
319 while(!onlist(&p->runqueue, t)){
320 p->runrend.l = &p->lock;
321 _threaddebug("scheduler sleep (pin)");
322 _procsleep(&p->runrend);
323 _threaddebug("scheduler wake (pin)");
325 }else
326 while((t = p->runqueue.head) == nil){
327 if(p->nthread == 0)
328 goto Out;
329 if((t = p->idlequeue.head) != nil){
330 /*
331 * Run all the idling threads once.
332 */
333 while((t = p->idlequeue.head) != nil){
334 delthread(&p->idlequeue, t);
335 addthread(&p->runqueue, t);
337 continue;
339 p->runrend.l = &p->lock;
340 _threaddebug("scheduler sleep");
341 _procsleep(&p->runrend);
342 _threaddebug("scheduler wake");
344 if(p->pinthread && p->pinthread != t)
345 fprint(2, "p->pinthread %p t %p\n", p->pinthread, t);
346 assert(p->pinthread == nil || p->pinthread == t);
347 delthread(&p->runqueue, t);
348 unlock(&p->lock);
349 p->thread = t;
350 p->nswitch++;
351 _threaddebug("run %d (%s)", t->id, t->name);
352 contextswitch(&p->schedcontext, &t->context);
353 /*print("back in scheduler\n"); */
354 p->thread = nil;
355 lock(&p->lock);
356 if(t->exiting){
357 delthreadinproc(p, t);
358 p->nthread--;
359 /*print("nthread %d\n", p->nthread); */
360 free(t);
364 Out:
365 _threaddebug("scheduler exit");
366 if(p->mainproc){
367 /*
368 * Stupid bug - on Linux 2.6 and maybe elsewhere,
369 * if the main thread exits then the others keep running
370 * but the process shows up as a zombie in ps and is not
371 * attachable with ptrace. We'll just sit around pretending
372 * to be a system proc instead of exiting.
373 */
374 _threaddaemonize();
375 lock(&threadnproclock);
376 if(++threadnsysproc == threadnproc)
377 threadexitsall(p->msg);
378 p->sysproc = 1;
379 unlock(&threadnproclock);
380 for(;;)
381 sleep(1000);
384 delproc(p);
385 lock(&threadnproclock);
386 if(p->sysproc)
387 --threadnsysproc;
388 if(--threadnproc == threadnsysproc)
389 threadexitsall(p->msg);
390 unlock(&threadnproclock);
391 unlock(&p->lock);
392 _threadsetproc(nil);
393 free(p);
396 void
397 _threadsetsysproc(void)
399 lock(&threadnproclock);
400 if(++threadnsysproc == threadnproc)
401 threadexitsall(nil);
402 unlock(&threadnproclock);
403 proc()->sysproc = 1;
406 void**
407 procdata(void)
409 return &proc()->udata;
412 void**
413 threaddata(void)
415 return &proc()->thread->udata;
418 extern Jmp *(*_notejmpbuf)(void);
419 static Jmp*
420 threadnotejmp(void)
422 return &proc()->sigjmp;
425 /*
426 * debugging
427 */
428 void
429 threadsetname(char *fmt, ...)
431 va_list arg;
432 _Thread *t;
434 t = proc()->thread;
435 va_start(arg, fmt);
436 vsnprint(t->name, sizeof t->name, fmt, arg);
437 va_end(arg);
440 char*
441 threadgetname(void)
443 return proc()->thread->name;
446 void
447 threadsetstate(char *fmt, ...)
449 va_list arg;
450 _Thread *t;
452 t = proc()->thread;
453 va_start(arg, fmt);
454 vsnprint(t->state, sizeof t->name, fmt, arg);
455 va_end(arg);
458 int
459 threadid(void)
461 _Thread *t;
463 t = proc()->thread;
464 return t->id;
467 void
468 needstack(int n)
470 _Thread *t;
472 t = proc()->thread;
474 if((char*)&t <= (char*)t->stk
475 || (char*)&t - (char*)t->stk < 256+n){
476 fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
477 abort();
481 static int
482 singlethreaded(void)
484 return threadnproc == 1 && _threadprocs->nthread == 1;
487 /*
488 * locking
489 */
490 static int
491 threadqlock(QLock *l, int block, ulong pc)
493 /*print("threadqlock %p\n", l); */
494 lock(&l->l);
495 if(l->owner == nil){
496 l->owner = (*threadnow)();
497 /*print("qlock %p @%#x by %p\n", l, pc, l->owner); */
498 unlock(&l->l);
499 return 1;
501 if(!block){
502 unlock(&l->l);
503 return 0;
506 if(singlethreaded()){
507 fprint(2, "qlock deadlock\n");
508 abort();
511 /*print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)()); */
512 addthread(&l->waiting, (*threadnow)());
513 unlock(&l->l);
515 _threadswitch();
517 if(l->owner != (*threadnow)()){
518 fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
519 argv0, pc, l->owner, (*threadnow)());
520 abort();
522 /*print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)()); */
523 return 1;
526 static void
527 threadqunlock(QLock *l, ulong pc)
529 _Thread *ready;
531 lock(&l->l);
532 /*print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner); */
533 if(l->owner == 0){
534 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
535 argv0, pc, l->owner, (*threadnow)());
536 abort();
538 if((l->owner = ready = l->waiting.head) != nil)
539 delthread(&l->waiting, l->owner);
540 /*
541 * N.B. Cannot call _threadready() before unlocking l->l,
542 * because the thread we are readying might:
543 * - be in another proc
544 * - start running immediately
545 * - and free l before we get a chance to run again
546 */
547 unlock(&l->l);
548 if(ready)
549 _threadready(l->owner);
552 static int
553 threadrlock(RWLock *l, int block, ulong pc)
555 USED(pc);
557 lock(&l->l);
558 if(l->writer == nil && l->wwaiting.head == nil){
559 l->readers++;
560 unlock(&l->l);
561 return 1;
563 if(!block){
564 unlock(&l->l);
565 return 0;
567 if(singlethreaded()){
568 fprint(2, "rlock deadlock\n");
569 abort();
571 addthread(&l->rwaiting, (*threadnow)());
572 unlock(&l->l);
573 _threadswitch();
574 return 1;
577 static int
578 threadwlock(RWLock *l, int block, ulong pc)
580 USED(pc);
582 lock(&l->l);
583 if(l->writer == nil && l->readers == 0){
584 l->writer = (*threadnow)();
585 unlock(&l->l);
586 return 1;
588 if(!block){
589 unlock(&l->l);
590 return 0;
592 if(singlethreaded()){
593 fprint(2, "wlock deadlock\n");
594 abort();
596 addthread(&l->wwaiting, (*threadnow)());
597 unlock(&l->l);
598 _threadswitch();
599 return 1;
602 static void
603 threadrunlock(RWLock *l, ulong pc)
605 _Thread *t;
607 USED(pc);
608 t = nil;
609 lock(&l->l);
610 --l->readers;
611 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
612 delthread(&l->wwaiting, t);
613 l->writer = t;
615 unlock(&l->l);
616 if(t)
617 _threadready(t);
621 static void
622 threadwunlock(RWLock *l, ulong pc)
624 _Thread *t;
626 USED(pc);
627 lock(&l->l);
628 l->writer = nil;
629 assert(l->readers == 0);
630 while((t = l->rwaiting.head) != nil){
631 delthread(&l->rwaiting, t);
632 l->readers++;
633 _threadready(t);
635 t = nil;
636 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
637 delthread(&l->wwaiting, t);
638 l->writer = t;
640 unlock(&l->l);
641 if(t)
642 _threadready(t);
645 /*
646 * sleep and wakeup
647 */
648 static void
649 threadrsleep(Rendez *r, ulong pc)
651 if(singlethreaded()){
652 fprint(2, "rsleep deadlock\n");
653 abort();
655 addthread(&r->waiting, proc()->thread);
656 qunlock(r->l);
657 _threadswitch();
658 qlock(r->l);
661 static int
662 threadrwakeup(Rendez *r, int all, ulong pc)
664 int i;
665 _Thread *t;
667 for(i=0;; i++){
668 if(i==1 && !all)
669 break;
670 if((t = r->waiting.head) == nil)
671 break;
672 delthread(&r->waiting, t);
673 _threadready(t);
675 return i;
678 /*
679 * startup
680 */
682 static int threadargc;
683 static char **threadargv;
684 int mainstacksize;
685 extern int _p9usepwlibrary; /* getgrgid etc. smash the stack - tell _p9dir just say no */
686 static void
687 threadmainstart(void *v)
689 USED(v);
691 /*
692 * N.B. This call to proc() is a program's first call (indirectly) to a
693 * pthreads function while executing on a non-pthreads-allocated
694 * stack. If the pthreads implementation is using the stack pointer
695 * to locate the per-thread data, then this call will blow up.
696 * This means the pthread implementation is not suitable for
697 * running under libthread. Time to write your own. Sorry.
698 */
699 _p9usepwlibrary = 0;
700 threadmainproc = proc();
701 threadmain(threadargc, threadargv);
704 extern void (*_sysfatal)(char*, va_list);
706 int
707 main(int argc, char **argv)
709 Proc *p;
711 argv0 = argv[0];
713 if(getenv("NOLIBTHREADDAEMONIZE") == nil)
714 _threadsetupdaemonize();
716 threadargc = argc;
717 threadargv = argv;
719 /*
720 * Install locking routines into C library.
721 */
722 _lock = _threadlock;
723 _unlock = _threadunlock;
724 _qlock = threadqlock;
725 _qunlock = threadqunlock;
726 _rlock = threadrlock;
727 _runlock = threadrunlock;
728 _wlock = threadwlock;
729 _wunlock = threadwunlock;
730 _rsleep = threadrsleep;
731 _rwakeup = threadrwakeup;
732 _notejmpbuf = threadnotejmp;
733 _pin = threadpin;
734 _unpin = threadunpin;
735 _sysfatal = threadsysfatal;
737 _pthreadinit();
738 p = procalloc();
739 p->mainproc = 1;
740 _threadsetproc(p);
741 if(mainstacksize == 0)
742 mainstacksize = 256*1024;
743 atnotify(threadinfo, 1);
744 _threadcreate(p, threadmainstart, nil, mainstacksize);
745 procscheduler(p);
746 sysfatal("procscheduler returned in threadmain!");
747 /* does not return */
748 return 0;
751 /*
752 * hooray for linked lists
753 */
754 static void
755 addthread(_Threadlist *l, _Thread *t)
757 if(l->tail){
758 l->tail->next = t;
759 t->prev = l->tail;
760 }else{
761 l->head = t;
762 t->prev = nil;
764 l->tail = t;
765 t->next = nil;
768 static void
769 delthread(_Threadlist *l, _Thread *t)
771 if(t->prev)
772 t->prev->next = t->next;
773 else
774 l->head = t->next;
775 if(t->next)
776 t->next->prev = t->prev;
777 else
778 l->tail = t->prev;
781 /* inefficient but rarely used */
782 static int
783 onlist(_Threadlist *l, _Thread *t)
785 _Thread *tt;
787 for(tt = l->head; tt; tt=tt->next)
788 if(tt == t)
789 return 1;
790 return 0;
793 static void
794 addthreadinproc(Proc *p, _Thread *t)
796 _Threadlist *l;
798 l = &p->allthreads;
799 if(l->tail){
800 l->tail->allnext = t;
801 t->allprev = l->tail;
802 }else{
803 l->head = t;
804 t->allprev = nil;
806 l->tail = t;
807 t->allnext = nil;
810 static void
811 delthreadinproc(Proc *p, _Thread *t)
813 _Threadlist *l;
815 l = &p->allthreads;
816 if(t->allprev)
817 t->allprev->allnext = t->allnext;
818 else
819 l->head = t->allnext;
820 if(t->allnext)
821 t->allnext->allprev = t->allprev;
822 else
823 l->tail = t->allprev;
826 Proc *_threadprocs;
827 Lock _threadprocslock;
828 static Proc *_threadprocstail;
830 static void
831 addproc(Proc *p)
833 lock(&_threadprocslock);
834 if(_threadprocstail){
835 _threadprocstail->next = p;
836 p->prev = _threadprocstail;
837 }else{
838 _threadprocs = p;
839 p->prev = nil;
841 _threadprocstail = p;
842 p->next = nil;
843 unlock(&_threadprocslock);
846 static void
847 delproc(Proc *p)
849 lock(&_threadprocslock);
850 if(p->prev)
851 p->prev->next = p->next;
852 else
853 _threadprocs = p->next;
854 if(p->next)
855 p->next->prev = p->prev;
856 else
857 _threadprocstail = p->prev;
858 unlock(&_threadprocslock);
861 /*
862 * notify - for now just use the usual mechanisms
863 */
864 void
865 threadnotify(int (*f)(void*, char*), int in)
867 atnotify(f, in);
870 static int
871 onrunqueue(Proc *p, _Thread *t)
873 _Thread *tt;
875 for(tt=p->runqueue.head; tt; tt=tt->next)
876 if(tt == t)
877 return 1;
878 return 0;
881 /*
882 * print state - called from SIGINFO
883 */
884 static int
885 threadinfo(void *v, char *s)
887 Proc *p;
888 _Thread *t;
890 if(strcmp(s, "quit") != 0 && strcmp(s, "sys: status request") != 0)
891 return 0;
893 for(p=_threadprocs; p; p=p->next){
894 fprint(2, "proc %p %s%s\n", (void*)p->osprocid, p->msg,
895 p->sysproc ? " (sysproc)": "");
896 for(t=p->allthreads.head; t; t=t->allnext){
897 fprint(2, "\tthread %d %s: %s %s\n",
898 t->id,
899 t == p->thread ? "Running" :
900 onrunqueue(p, t) ? "Ready" : "Sleeping",
901 t->state, t->name);
904 return 1;