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 = 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 t = threadalloc(fn, arg, stack);
159 t->proc = p;
160 addthreadinproc(p, t);
161 p->nthread++;
162 _threadready(t);
163 return t;
166 int
167 threadcreate(void (*fn)(void*), void *arg, uint stack)
169 _Thread *t;
171 t = _threadcreate(proc(), fn, arg, stack);
172 return t->id;
175 int
176 proccreate(void (*fn)(void*), void *arg, uint stack)
178 int id;
179 _Thread *t;
180 Proc *p;
182 p = procalloc();
183 t = _threadcreate(p, fn, arg, stack);
184 id = t->id; /* t might be freed after _procstart */
185 _procstart(p, procscheduler);
186 return id;
189 void
190 _threadswitch(void)
192 Proc *p;
194 needstack(0);
195 p = proc();
196 /*print("threadswtch %p\n", p); */
197 contextswitch(&p->thread->context, &p->schedcontext);
200 void
201 _threadready(_Thread *t)
203 Proc *p;
205 p = t->proc;
206 lock(&p->lock);
207 p->runrend.l = &p->lock;
208 addthread(&p->runqueue, t);
209 /*print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid); */
210 if(p != proc())
211 _procwakeupandunlock(&p->runrend);
212 else
213 unlock(&p->lock);
216 int
217 threadidle(void)
219 int n;
220 Proc *p;
222 p = proc();
223 n = p->nswitch;
224 lock(&p->lock);
225 p->runrend.l = &p->lock;
226 addthread(&p->idlequeue, p->thread);
227 unlock(&p->lock);
228 _threadswitch();
229 return p->nswitch - n;
232 int
233 threadyield(void)
235 int n;
236 Proc *p;
238 p = proc();
239 n = p->nswitch;
240 _threadready(p->thread);
241 _threadswitch();
242 return p->nswitch - n;
245 void
246 threadexits(char *msg)
248 Proc *p;
250 p = proc();
251 if(msg == nil)
252 msg = "";
253 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
254 proc()->thread->exiting = 1;
255 _threadswitch();
258 void
259 threadpin(void)
261 Proc *p;
263 p = proc();
264 if(p->pinthread){
265 fprint(2, "already pinning a thread - %p %p\n", p->pinthread, p->thread);
266 assert(0);
268 p->pinthread = p->thread;
271 void
272 threadunpin(void)
274 Proc *p;
276 p = proc();
277 if(p->pinthread != p->thread){
278 fprint(2, "wrong pinthread - %p %p\n", p->pinthread, p->thread);
279 assert(0);
281 p->pinthread = nil;
284 static void
285 contextswitch(Context *from, Context *to)
287 if(swapcontext(&from->uc, &to->uc) < 0){
288 fprint(2, "swapcontext failed: %r\n");
289 assert(0);
293 static void
294 procscheduler(Proc *p)
296 _Thread *t;
298 setproc(p);
299 _threaddebug("scheduler enter");
300 /* print("s %p\n", p); */
301 lock(&p->lock);
302 for(;;){
303 if((t = p->pinthread) != nil){
304 while(!onlist(&p->runqueue, t)){
305 p->runrend.l = &p->lock;
306 _threaddebug("scheduler sleep (pin)");
307 _procsleep(&p->runrend);
308 _threaddebug("scheduler wake (pin)");
310 }else
311 while((t = p->runqueue.head) == nil){
312 if(p->nthread == 0)
313 goto Out;
314 if((t = p->idlequeue.head) != nil){
315 /*
316 * Run all the idling threads once.
317 */
318 while((t = p->idlequeue.head) != nil){
319 delthread(&p->idlequeue, t);
320 addthread(&p->runqueue, t);
322 continue;
324 p->runrend.l = &p->lock;
325 _threaddebug("scheduler sleep");
326 _procsleep(&p->runrend);
327 _threaddebug("scheduler wake");
329 if(p->pinthread && p->pinthread != t)
330 fprint(2, "p->pinthread %p t %p\n", p->pinthread, t);
331 assert(p->pinthread == nil || p->pinthread == t);
332 delthread(&p->runqueue, t);
333 unlock(&p->lock);
334 p->thread = t;
335 p->nswitch++;
336 _threaddebug("run %d (%s)", t->id, t->name);
337 contextswitch(&p->schedcontext, &t->context);
338 /*print("back in scheduler\n"); */
339 p->thread = nil;
340 lock(&p->lock);
341 if(t->exiting){
342 delthreadinproc(p, t);
343 p->nthread--;
344 /*print("nthread %d\n", p->nthread); */
345 free(t);
349 Out:
350 _threaddebug("scheduler exit");
351 if(p->mainproc){
352 /*
353 * Stupid bug - on Linux 2.6 and maybe elsewhere,
354 * if the main thread exits then the others keep running
355 * but the process shows up as a zombie in ps and is not
356 * attachable with ptrace. We'll just sit around pretending
357 * to be a system proc instead of exiting.
358 */
359 _threaddaemonize();
360 lock(&threadnproclock);
361 if(++threadnsysproc == threadnproc)
362 threadexitsall(p->msg);
363 p->sysproc = 1;
364 unlock(&threadnproclock);
365 for(;;)
366 sleep(1000);
369 delproc(p);
370 lock(&threadnproclock);
371 if(p->sysproc)
372 --threadnsysproc;
373 if(--threadnproc == threadnsysproc)
374 threadexitsall(p->msg);
375 unlock(&threadnproclock);
376 unlock(&p->lock);
377 _threadsetproc(nil);
378 free(p);
381 void
382 _threadsetsysproc(void)
384 lock(&threadnproclock);
385 if(++threadnsysproc == threadnproc)
386 threadexitsall(nil);
387 unlock(&threadnproclock);
388 proc()->sysproc = 1;
391 void**
392 procdata(void)
394 return &proc()->udata;
397 void**
398 threaddata(void)
400 return &proc()->thread->udata;
403 extern Jmp *(*_notejmpbuf)(void);
404 static Jmp*
405 threadnotejmp(void)
407 return &proc()->sigjmp;
410 /*
411 * debugging
412 */
413 void
414 threadsetname(char *fmt, ...)
416 va_list arg;
417 _Thread *t;
419 t = proc()->thread;
420 va_start(arg, fmt);
421 vsnprint(t->name, sizeof t->name, fmt, arg);
422 va_end(arg);
425 char*
426 threadgetname(void)
428 return proc()->thread->name;
431 void
432 threadsetstate(char *fmt, ...)
434 va_list arg;
435 _Thread *t;
437 t = proc()->thread;
438 va_start(arg, fmt);
439 vsnprint(t->state, sizeof t->name, fmt, arg);
440 va_end(arg);
443 int
444 threadid(void)
446 _Thread *t;
448 t = proc()->thread;
449 return t->id;
452 void
453 needstack(int n)
455 _Thread *t;
457 t = proc()->thread;
459 if((char*)&t <= (char*)t->stk
460 || (char*)&t - (char*)t->stk < 256+n){
461 fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
462 abort();
466 static int
467 singlethreaded(void)
469 return threadnproc == 1 && _threadprocs->nthread == 1;
472 /*
473 * locking
474 */
475 static int
476 threadqlock(QLock *l, int block, ulong pc)
478 /*print("threadqlock %p\n", l); */
479 lock(&l->l);
480 if(l->owner == nil){
481 l->owner = (*threadnow)();
482 /*print("qlock %p @%#x by %p\n", l, pc, l->owner); */
483 unlock(&l->l);
484 return 1;
486 if(!block){
487 unlock(&l->l);
488 return 0;
491 if(singlethreaded()){
492 fprint(2, "qlock deadlock\n");
493 abort();
496 /*print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)()); */
497 addthread(&l->waiting, (*threadnow)());
498 unlock(&l->l);
500 _threadswitch();
502 if(l->owner != (*threadnow)()){
503 fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
504 argv0, pc, l->owner, (*threadnow)());
505 abort();
507 /*print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)()); */
508 return 1;
511 static void
512 threadqunlock(QLock *l, ulong pc)
514 _Thread *ready;
516 lock(&l->l);
517 /*print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner); */
518 if(l->owner == 0){
519 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
520 argv0, pc, l->owner, (*threadnow)());
521 abort();
523 if((l->owner = ready = l->waiting.head) != nil)
524 delthread(&l->waiting, l->owner);
525 /*
526 * N.B. Cannot call _threadready() before unlocking l->l,
527 * because the thread we are readying might:
528 * - be in another proc
529 * - start running immediately
530 * - and free l before we get a chance to run again
531 */
532 unlock(&l->l);
533 if(ready)
534 _threadready(l->owner);
537 static int
538 threadrlock(RWLock *l, int block, ulong pc)
540 USED(pc);
542 lock(&l->l);
543 if(l->writer == nil && l->wwaiting.head == nil){
544 l->readers++;
545 unlock(&l->l);
546 return 1;
548 if(!block){
549 unlock(&l->l);
550 return 0;
552 if(singlethreaded()){
553 fprint(2, "rlock deadlock\n");
554 abort();
556 addthread(&l->rwaiting, (*threadnow)());
557 unlock(&l->l);
558 _threadswitch();
559 return 1;
562 static int
563 threadwlock(RWLock *l, int block, ulong pc)
565 USED(pc);
567 lock(&l->l);
568 if(l->writer == nil && l->readers == 0){
569 l->writer = (*threadnow)();
570 unlock(&l->l);
571 return 1;
573 if(!block){
574 unlock(&l->l);
575 return 0;
577 if(singlethreaded()){
578 fprint(2, "wlock deadlock\n");
579 abort();
581 addthread(&l->wwaiting, (*threadnow)());
582 unlock(&l->l);
583 _threadswitch();
584 return 1;
587 static void
588 threadrunlock(RWLock *l, ulong pc)
590 _Thread *t;
592 USED(pc);
593 t = nil;
594 lock(&l->l);
595 --l->readers;
596 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
597 delthread(&l->wwaiting, t);
598 l->writer = t;
600 unlock(&l->l);
601 if(t)
602 _threadready(t);
606 static void
607 threadwunlock(RWLock *l, ulong pc)
609 _Thread *t;
611 USED(pc);
612 lock(&l->l);
613 l->writer = nil;
614 assert(l->readers == 0);
615 while((t = l->rwaiting.head) != nil){
616 delthread(&l->rwaiting, t);
617 l->readers++;
618 _threadready(t);
620 t = nil;
621 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
622 delthread(&l->wwaiting, t);
623 l->writer = t;
625 unlock(&l->l);
626 if(t)
627 _threadready(t);
630 /*
631 * sleep and wakeup
632 */
633 static void
634 threadrsleep(Rendez *r, ulong pc)
636 if(singlethreaded()){
637 fprint(2, "rsleep deadlock\n");
638 abort();
640 addthread(&r->waiting, proc()->thread);
641 qunlock(r->l);
642 _threadswitch();
643 qlock(r->l);
646 static int
647 threadrwakeup(Rendez *r, int all, ulong pc)
649 int i;
650 _Thread *t;
652 for(i=0;; i++){
653 if(i==1 && !all)
654 break;
655 if((t = r->waiting.head) == nil)
656 break;
657 delthread(&r->waiting, t);
658 _threadready(t);
660 return i;
663 /*
664 * startup
665 */
667 static int threadargc;
668 static char **threadargv;
669 int mainstacksize;
670 extern int _p9usepwlibrary; /* getgrgid etc. smash the stack - tell _p9dir just say no */
671 static void
672 threadmainstart(void *v)
674 USED(v);
676 /*
677 * N.B. This call to proc() is a program's first call (indirectly) to a
678 * pthreads function while executing on a non-pthreads-allocated
679 * stack. If the pthreads implementation is using the stack pointer
680 * to locate the per-thread data, then this call will blow up.
681 * This means the pthread implementation is not suitable for
682 * running under libthread. Time to write your own. Sorry.
683 */
684 _p9usepwlibrary = 0;
685 threadmainproc = proc();
686 threadmain(threadargc, threadargv);
689 int
690 main(int argc, char **argv)
692 Proc *p;
694 argv0 = argv[0];
696 if(getenv("NOLIBTHREADDAEMONIZE") == nil)
697 _threadsetupdaemonize();
699 threadargc = argc;
700 threadargv = argv;
702 /*
703 * Install locking routines into C library.
704 */
705 _lock = _threadlock;
706 _unlock = _threadunlock;
707 _qlock = threadqlock;
708 _qunlock = threadqunlock;
709 _rlock = threadrlock;
710 _runlock = threadrunlock;
711 _wlock = threadwlock;
712 _wunlock = threadwunlock;
713 _rsleep = threadrsleep;
714 _rwakeup = threadrwakeup;
715 _notejmpbuf = threadnotejmp;
716 _pin = threadpin;
717 _unpin = threadunpin;
719 _pthreadinit();
720 p = procalloc();
721 p->mainproc = 1;
722 _threadsetproc(p);
723 if(mainstacksize == 0)
724 mainstacksize = 256*1024;
725 atnotify(threadinfo, 1);
726 _threadcreate(p, threadmainstart, nil, mainstacksize);
727 procscheduler(p);
728 sysfatal("procscheduler returned in threadmain!");
729 /* does not return */
730 return 0;
733 /*
734 * hooray for linked lists
735 */
736 static void
737 addthread(_Threadlist *l, _Thread *t)
739 if(l->tail){
740 l->tail->next = t;
741 t->prev = l->tail;
742 }else{
743 l->head = t;
744 t->prev = nil;
746 l->tail = t;
747 t->next = nil;
750 static void
751 delthread(_Threadlist *l, _Thread *t)
753 if(t->prev)
754 t->prev->next = t->next;
755 else
756 l->head = t->next;
757 if(t->next)
758 t->next->prev = t->prev;
759 else
760 l->tail = t->prev;
763 /* inefficient but rarely used */
764 static int
765 onlist(_Threadlist *l, _Thread *t)
767 _Thread *tt;
769 for(tt = l->head; tt; tt=tt->next)
770 if(tt == t)
771 return 1;
772 return 0;
775 static void
776 addthreadinproc(Proc *p, _Thread *t)
778 _Threadlist *l;
780 l = &p->allthreads;
781 if(l->tail){
782 l->tail->allnext = t;
783 t->allprev = l->tail;
784 }else{
785 l->head = t;
786 t->allprev = nil;
788 l->tail = t;
789 t->allnext = nil;
792 static void
793 delthreadinproc(Proc *p, _Thread *t)
795 _Threadlist *l;
797 l = &p->allthreads;
798 if(t->allprev)
799 t->allprev->allnext = t->allnext;
800 else
801 l->head = t->allnext;
802 if(t->allnext)
803 t->allnext->allprev = t->allprev;
804 else
805 l->tail = t->allprev;
808 Proc *_threadprocs;
809 Lock _threadprocslock;
810 static Proc *_threadprocstail;
812 static void
813 addproc(Proc *p)
815 lock(&_threadprocslock);
816 if(_threadprocstail){
817 _threadprocstail->next = p;
818 p->prev = _threadprocstail;
819 }else{
820 _threadprocs = p;
821 p->prev = nil;
823 _threadprocstail = p;
824 p->next = nil;
825 unlock(&_threadprocslock);
828 static void
829 delproc(Proc *p)
831 lock(&_threadprocslock);
832 if(p->prev)
833 p->prev->next = p->next;
834 else
835 _threadprocs = p->next;
836 if(p->next)
837 p->next->prev = p->prev;
838 else
839 _threadprocstail = p->prev;
840 unlock(&_threadprocslock);
843 /*
844 * notify - for now just use the usual mechanisms
845 */
846 void
847 threadnotify(int (*f)(void*, char*), int in)
849 atnotify(f, in);
852 static int
853 onrunqueue(Proc *p, _Thread *t)
855 _Thread *tt;
857 for(tt=p->runqueue.head; tt; tt=tt->next)
858 if(tt == t)
859 return 1;
860 return 0;
863 /*
864 * print state - called from SIGINFO
865 */
866 static int
867 threadinfo(void *v, char *s)
869 Proc *p;
870 _Thread *t;
872 if(strcmp(s, "quit") != 0 && strcmp(s, "sys: status request") != 0)
873 return 0;
875 for(p=_threadprocs; p; p=p->next){
876 fprint(2, "proc %p %s%s\n", (void*)p->osprocid, p->msg,
877 p->sysproc ? " (sysproc)": "");
878 for(t=p->allthreads.head; t; t=t->allnext){
879 fprint(2, "\tthread %d %s: %s %s\n",
880 t->id,
881 t == p->thread ? "Running" :
882 onrunqueue(p, t) ? "Ready" : "Sleeping",
883 t->state, t->name);
886 return 1;