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 //print("threadstart\n");
90 z = (ulong)x << 16; /* hide undefined 32-bit shift from 32-bit compilers */
91 z <<= 16;
92 z |= y;
93 t = (_Thread*)z;
95 //print("threadstart sp=%p arg=%p startfn=%p t=%p\n", &t, t, t->startfn, t->startarg);
96 t->startfn(t->startarg);
97 /*print("threadexits %p\n", v); */
98 threadexits(nil);
99 /*print("not reacehd\n"); */
102 static _Thread*
103 threadalloc(void (*fn)(void*), void *arg, uint stack)
105 _Thread *t;
106 sigset_t zero;
107 uint x, y;
108 ulong z;
110 /* allocate the task and stack together */
111 t = malloc(sizeof *t+stack);
112 if(t == nil)
113 sysfatal("threadalloc malloc: %r");
114 memset(t, 0, sizeof *t);
115 t->stk = (uchar*)(t+1);
116 t->stksize = stack;
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 memset(&t->context.uc, 0, sizeof t->context.uc);
125 sigemptyset(&zero);
126 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
127 //print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn);
129 /* must initialize with current context */
130 if(getcontext(&t->context.uc) < 0)
131 sysfatal("threadalloc getcontext: %r");
132 //print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn);
134 /* call makecontext to do the real work. */
135 /* leave a few words open on both ends */
136 t->context.uc.uc_stack.ss_sp = (void*)(t->stk+8);
137 t->context.uc.uc_stack.ss_size = t->stksize-64;
138 #if defined(__sun__) && !defined(__MAKECONTEXT_V2_SOURCE) /* sigh */
139 /* can avoid this with __MAKECONTEXT_V2_SOURCE but only on SunOS 5.9 */
140 t->context.uc.uc_stack.ss_sp =
141 (char*)t->context.uc.uc_stack.ss_sp
142 +t->context.uc.uc_stack.ss_size;
143 #endif
144 /*
145 * All this magic is because you have to pass makecontext a
146 * function that takes some number of word-sized variables,
147 * and on 64-bit machines pointers are bigger than words.
148 */
149 //print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn);
150 z = (ulong)t;
151 y = z;
152 z >>= 16; /* hide undefined 32-bit shift from 32-bit compilers */
153 x = z>>16;
154 makecontext(&t->context.uc, (void(*)(void))threadstart, 2, y, x);
156 return t;
159 _Thread*
160 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
162 _Thread *t;
164 /* defend against bad C libraries */
165 if(stack < (256<<10))
166 stack = 256<<10;
168 t = threadalloc(fn, arg, stack);
169 t->proc = p;
170 addthreadinproc(p, t);
171 p->nthread++;
172 _threadready(t);
173 return t;
176 int
177 threadcreate(void (*fn)(void*), void *arg, uint stack)
179 _Thread *t;
181 t = _threadcreate(proc(), fn, arg, stack);
182 return t->id;
185 int
186 proccreate(void (*fn)(void*), void *arg, uint stack)
188 int id;
189 _Thread *t;
190 Proc *p;
192 p = procalloc();
193 t = _threadcreate(p, fn, arg, stack);
194 id = t->id; /* t might be freed after _procstart */
195 _procstart(p, procscheduler);
196 return id;
199 void
200 _threadswitch(void)
202 Proc *p;
204 needstack(0);
205 p = proc();
206 /*print("threadswtch %p\n", p); */
207 contextswitch(&p->thread->context, &p->schedcontext);
210 void
211 _threadready(_Thread *t)
213 Proc *p;
215 p = t->proc;
216 lock(&p->lock);
217 p->runrend.l = &p->lock;
218 addthread(&p->runqueue, t);
219 /*print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid); */
220 if(p != proc())
221 _procwakeupandunlock(&p->runrend);
222 else
223 unlock(&p->lock);
226 int
227 threadidle(void)
229 int n;
230 Proc *p;
232 p = proc();
233 n = p->nswitch;
234 lock(&p->lock);
235 p->runrend.l = &p->lock;
236 addthread(&p->idlequeue, p->thread);
237 unlock(&p->lock);
238 _threadswitch();
239 return p->nswitch - n;
242 int
243 threadyield(void)
245 int n;
246 Proc *p;
248 p = proc();
249 n = p->nswitch;
250 _threadready(p->thread);
251 _threadswitch();
252 return p->nswitch - n;
255 void
256 threadexits(char *msg)
258 Proc *p;
260 p = proc();
261 if(msg == nil)
262 msg = "";
263 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
264 proc()->thread->exiting = 1;
265 _threadswitch();
268 void
269 threadpin(void)
271 Proc *p;
273 p = proc();
274 if(p->pinthread){
275 fprint(2, "already pinning a thread - %p %p\n", p->pinthread, p->thread);
276 assert(0);
278 p->pinthread = p->thread;
281 void
282 threadunpin(void)
284 Proc *p;
286 p = proc();
287 if(p->pinthread != p->thread){
288 fprint(2, "wrong pinthread - %p %p\n", p->pinthread, p->thread);
289 assert(0);
291 p->pinthread = nil;
294 void
295 threadsysfatal(char *fmt, va_list arg)
297 char buf[256];
299 vseprint(buf, buf+sizeof(buf), fmt, arg);
300 __fixargv0();
301 fprint(2, "%s: %s\n", argv0 ? argv0 : "<prog>", buf);
302 threadexitsall(buf);
305 static void
306 contextswitch(Context *from, Context *to)
308 if(swapcontext(&from->uc, &to->uc) < 0){
309 fprint(2, "swapcontext failed: %r\n");
310 assert(0);
314 static void
315 procscheduler(Proc *p)
317 _Thread *t;
319 setproc(p);
320 _threaddebug("scheduler enter");
321 //print("s %p\n", p);
322 lock(&p->lock);
323 for(;;){
324 if((t = p->pinthread) != nil){
325 while(!onlist(&p->runqueue, t)){
326 p->runrend.l = &p->lock;
327 _threaddebug("scheduler sleep (pin)");
328 _procsleep(&p->runrend);
329 _threaddebug("scheduler wake (pin)");
331 }else
332 while((t = p->runqueue.head) == nil){
333 if(p->nthread == 0)
334 goto Out;
335 if((t = p->idlequeue.head) != nil){
336 /*
337 * Run all the idling threads once.
338 */
339 while((t = p->idlequeue.head) != nil){
340 delthread(&p->idlequeue, t);
341 addthread(&p->runqueue, t);
343 continue;
345 p->runrend.l = &p->lock;
346 _threaddebug("scheduler sleep");
347 _procsleep(&p->runrend);
348 _threaddebug("scheduler wake");
350 if(p->pinthread && p->pinthread != t)
351 fprint(2, "p->pinthread %p t %p\n", p->pinthread, t);
352 assert(p->pinthread == nil || p->pinthread == t);
353 delthread(&p->runqueue, t);
354 unlock(&p->lock);
355 p->thread = t;
356 p->nswitch++;
357 _threaddebug("run %d (%s)", t->id, t->name);
358 //print("run %p %p %p %p\n", t, *(uintptr*)(t->context.uc.mc.sp), t->context.uc.mc.di, t->context.uc.mc.si);
359 contextswitch(&p->schedcontext, &t->context);
360 /*print("back in scheduler\n"); */
361 p->thread = nil;
362 lock(&p->lock);
363 if(t->exiting){
364 delthreadinproc(p, t);
365 p->nthread--;
366 /*print("nthread %d\n", p->nthread); */
367 free(t);
371 Out:
372 _threaddebug("scheduler exit");
373 if(p->mainproc){
374 /*
375 * Stupid bug - on Linux 2.6 and maybe elsewhere,
376 * if the main thread exits then the others keep running
377 * but the process shows up as a zombie in ps and is not
378 * attachable with ptrace. We'll just sit around pretending
379 * to be a system proc instead of exiting.
380 */
381 _threaddaemonize();
382 lock(&threadnproclock);
383 if(++threadnsysproc == threadnproc)
384 threadexitsall(p->msg);
385 p->sysproc = 1;
386 unlock(&threadnproclock);
387 for(;;)
388 sleep(1000);
391 delproc(p);
392 lock(&threadnproclock);
393 if(p->sysproc)
394 --threadnsysproc;
395 if(--threadnproc == threadnsysproc)
396 threadexitsall(p->msg);
397 unlock(&threadnproclock);
398 unlock(&p->lock);
399 _threadsetproc(nil);
400 free(p);
403 void
404 _threadsetsysproc(void)
406 lock(&threadnproclock);
407 if(++threadnsysproc == threadnproc)
408 threadexitsall(nil);
409 unlock(&threadnproclock);
410 proc()->sysproc = 1;
413 void**
414 procdata(void)
416 return &proc()->udata;
419 void**
420 threaddata(void)
422 return &proc()->thread->udata;
425 extern Jmp *(*_notejmpbuf)(void);
426 static Jmp*
427 threadnotejmp(void)
429 return &proc()->sigjmp;
432 /*
433 * debugging
434 */
435 void
436 threadsetname(char *fmt, ...)
438 va_list arg;
439 _Thread *t;
441 t = proc()->thread;
442 va_start(arg, fmt);
443 vsnprint(t->name, sizeof t->name, fmt, arg);
444 va_end(arg);
447 char*
448 threadgetname(void)
450 return proc()->thread->name;
453 void
454 threadsetstate(char *fmt, ...)
456 va_list arg;
457 _Thread *t;
459 t = proc()->thread;
460 va_start(arg, fmt);
461 vsnprint(t->state, sizeof t->name, fmt, arg);
462 va_end(arg);
465 int
466 threadid(void)
468 _Thread *t;
470 t = proc()->thread;
471 return t->id;
474 void
475 needstack(int n)
477 _Thread *t;
479 t = proc()->thread;
481 if((char*)&t <= (char*)t->stk
482 || (char*)&t - (char*)t->stk < 256+n){
483 fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
484 abort();
488 static int
489 singlethreaded(void)
491 return threadnproc == 1 && _threadprocs->nthread == 1;
494 /*
495 * locking
496 */
497 static int
498 threadqlock(QLock *l, int block, ulong pc)
500 /*print("threadqlock %p\n", l); */
501 lock(&l->l);
502 if(l->owner == nil){
503 l->owner = (*threadnow)();
504 /*print("qlock %p @%#x by %p\n", l, pc, l->owner); */
505 unlock(&l->l);
506 return 1;
508 if(!block){
509 unlock(&l->l);
510 return 0;
513 if(singlethreaded()){
514 fprint(2, "qlock deadlock\n");
515 abort();
518 /*print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)()); */
519 addthread(&l->waiting, (*threadnow)());
520 unlock(&l->l);
522 _threadswitch();
524 if(l->owner != (*threadnow)()){
525 fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
526 argv0, pc, l->owner, (*threadnow)());
527 abort();
529 /*print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)()); */
530 return 1;
533 static void
534 threadqunlock(QLock *l, ulong pc)
536 _Thread *ready;
538 lock(&l->l);
539 /*print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner); */
540 if(l->owner == 0){
541 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
542 argv0, pc, l->owner, (*threadnow)());
543 abort();
545 if((l->owner = ready = l->waiting.head) != nil)
546 delthread(&l->waiting, l->owner);
547 /*
548 * N.B. Cannot call _threadready() before unlocking l->l,
549 * because the thread we are readying might:
550 * - be in another proc
551 * - start running immediately
552 * - and free l before we get a chance to run again
553 */
554 unlock(&l->l);
555 if(ready)
556 _threadready(l->owner);
559 static int
560 threadrlock(RWLock *l, int block, ulong pc)
562 USED(pc);
564 lock(&l->l);
565 if(l->writer == nil && l->wwaiting.head == nil){
566 l->readers++;
567 unlock(&l->l);
568 return 1;
570 if(!block){
571 unlock(&l->l);
572 return 0;
574 if(singlethreaded()){
575 fprint(2, "rlock deadlock\n");
576 abort();
578 addthread(&l->rwaiting, (*threadnow)());
579 unlock(&l->l);
580 _threadswitch();
581 return 1;
584 static int
585 threadwlock(RWLock *l, int block, ulong pc)
587 USED(pc);
589 lock(&l->l);
590 if(l->writer == nil && l->readers == 0){
591 l->writer = (*threadnow)();
592 unlock(&l->l);
593 return 1;
595 if(!block){
596 unlock(&l->l);
597 return 0;
599 if(singlethreaded()){
600 fprint(2, "wlock deadlock\n");
601 abort();
603 addthread(&l->wwaiting, (*threadnow)());
604 unlock(&l->l);
605 _threadswitch();
606 return 1;
609 static void
610 threadrunlock(RWLock *l, ulong pc)
612 _Thread *t;
614 USED(pc);
615 t = nil;
616 lock(&l->l);
617 --l->readers;
618 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
619 delthread(&l->wwaiting, t);
620 l->writer = t;
622 unlock(&l->l);
623 if(t)
624 _threadready(t);
628 static void
629 threadwunlock(RWLock *l, ulong pc)
631 _Thread *t;
633 USED(pc);
634 lock(&l->l);
635 l->writer = nil;
636 assert(l->readers == 0);
637 while((t = l->rwaiting.head) != nil){
638 delthread(&l->rwaiting, t);
639 l->readers++;
640 _threadready(t);
642 t = nil;
643 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
644 delthread(&l->wwaiting, t);
645 l->writer = t;
647 unlock(&l->l);
648 if(t)
649 _threadready(t);
652 /*
653 * sleep and wakeup
654 */
655 static void
656 threadrsleep(Rendez *r, ulong pc)
658 if(singlethreaded()){
659 fprint(2, "rsleep deadlock\n");
660 abort();
662 addthread(&r->waiting, proc()->thread);
663 qunlock(r->l);
664 _threadswitch();
665 qlock(r->l);
668 static int
669 threadrwakeup(Rendez *r, int all, ulong pc)
671 int i;
672 _Thread *t;
674 for(i=0;; i++){
675 if(i==1 && !all)
676 break;
677 if((t = r->waiting.head) == nil)
678 break;
679 delthread(&r->waiting, t);
680 _threadready(t);
682 return i;
685 /*
686 * startup
687 */
689 static int threadargc;
690 static char **threadargv;
691 int mainstacksize;
692 extern int _p9usepwlibrary; /* getgrgid etc. smash the stack - tell _p9dir just say no */
693 static void
694 threadmainstart(void *v)
696 USED(v);
698 /*
699 * N.B. This call to proc() is a program's first call (indirectly) to a
700 * pthreads function while executing on a non-pthreads-allocated
701 * stack. If the pthreads implementation is using the stack pointer
702 * to locate the per-thread data, then this call will blow up.
703 * This means the pthread implementation is not suitable for
704 * running under libthread. Time to write your own. Sorry.
705 */
706 _p9usepwlibrary = 0;
707 threadmainproc = proc();
708 threadmain(threadargc, threadargv);
711 extern void (*_sysfatal)(char*, va_list);
713 int
714 main(int argc, char **argv)
716 Proc *p;
718 argv0 = argv[0];
720 if(getenv("NOLIBTHREADDAEMONIZE") == nil)
721 _threadsetupdaemonize();
723 threadargc = argc;
724 threadargv = argv;
726 /*
727 * Install locking routines into C library.
728 */
729 _lock = _threadlock;
730 _unlock = _threadunlock;
731 _qlock = threadqlock;
732 _qunlock = threadqunlock;
733 _rlock = threadrlock;
734 _runlock = threadrunlock;
735 _wlock = threadwlock;
736 _wunlock = threadwunlock;
737 _rsleep = threadrsleep;
738 _rwakeup = threadrwakeup;
739 _notejmpbuf = threadnotejmp;
740 _pin = threadpin;
741 _unpin = threadunpin;
742 _sysfatal = threadsysfatal;
744 _pthreadinit();
745 p = procalloc();
746 p->mainproc = 1;
747 _threadsetproc(p);
748 if(mainstacksize == 0)
749 mainstacksize = 256*1024;
750 atnotify(threadinfo, 1);
751 _threadcreate(p, threadmainstart, nil, mainstacksize);
752 procscheduler(p);
753 sysfatal("procscheduler returned in threadmain!");
754 /* does not return */
755 return 0;
758 /*
759 * hooray for linked lists
760 */
761 static void
762 addthread(_Threadlist *l, _Thread *t)
764 if(l->tail){
765 l->tail->next = t;
766 t->prev = l->tail;
767 }else{
768 l->head = t;
769 t->prev = nil;
771 l->tail = t;
772 t->next = nil;
775 static void
776 delthread(_Threadlist *l, _Thread *t)
778 if(t->prev)
779 t->prev->next = t->next;
780 else
781 l->head = t->next;
782 if(t->next)
783 t->next->prev = t->prev;
784 else
785 l->tail = t->prev;
788 /* inefficient but rarely used */
789 static int
790 onlist(_Threadlist *l, _Thread *t)
792 _Thread *tt;
794 for(tt = l->head; tt; tt=tt->next)
795 if(tt == t)
796 return 1;
797 return 0;
800 static void
801 addthreadinproc(Proc *p, _Thread *t)
803 _Threadlist *l;
805 l = &p->allthreads;
806 if(l->tail){
807 l->tail->allnext = t;
808 t->allprev = l->tail;
809 }else{
810 l->head = t;
811 t->allprev = nil;
813 l->tail = t;
814 t->allnext = nil;
817 static void
818 delthreadinproc(Proc *p, _Thread *t)
820 _Threadlist *l;
822 l = &p->allthreads;
823 if(t->allprev)
824 t->allprev->allnext = t->allnext;
825 else
826 l->head = t->allnext;
827 if(t->allnext)
828 t->allnext->allprev = t->allprev;
829 else
830 l->tail = t->allprev;
833 Proc *_threadprocs;
834 Lock _threadprocslock;
835 static Proc *_threadprocstail;
837 static void
838 addproc(Proc *p)
840 lock(&_threadprocslock);
841 if(_threadprocstail){
842 _threadprocstail->next = p;
843 p->prev = _threadprocstail;
844 }else{
845 _threadprocs = p;
846 p->prev = nil;
848 _threadprocstail = p;
849 p->next = nil;
850 unlock(&_threadprocslock);
853 static void
854 delproc(Proc *p)
856 lock(&_threadprocslock);
857 if(p->prev)
858 p->prev->next = p->next;
859 else
860 _threadprocs = p->next;
861 if(p->next)
862 p->next->prev = p->prev;
863 else
864 _threadprocstail = p->prev;
865 unlock(&_threadprocslock);
868 /*
869 * notify - for now just use the usual mechanisms
870 */
871 void
872 threadnotify(int (*f)(void*, char*), int in)
874 atnotify(f, in);
877 static int
878 onrunqueue(Proc *p, _Thread *t)
880 _Thread *tt;
882 for(tt=p->runqueue.head; tt; tt=tt->next)
883 if(tt == t)
884 return 1;
885 return 0;
888 /*
889 * print state - called from SIGINFO
890 */
891 static int
892 threadinfo(void *v, char *s)
894 Proc *p;
895 _Thread *t;
897 if(strcmp(s, "quit") != 0 && strcmp(s, "sys: status request") != 0)
898 return 0;
900 for(p=_threadprocs; p; p=p->next){
901 fprint(2, "proc %p %s%s\n", (void*)p->osprocid, p->msg,
902 p->sysproc ? " (sysproc)": "");
903 for(t=p->allthreads.head; t; t=t->allnext){
904 fprint(2, "\tthread %d %s: %s %s\n",
905 t->id,
906 t == p->thread ? "Running" :
907 onrunqueue(p, t) ? "Ready" : "Sleeping",
908 t->state, t->name);
911 return 1;