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 void addthreadinproc(Proc*, _Thread*);
16 static void delthreadinproc(Proc*, _Thread*);
17 static void contextswitch(Context *from, Context *to);
18 static void procscheduler(Proc*);
19 static int threadinfo(void*, char*);
21 static void
22 _threaddebug(char *fmt, ...)
23 {
24 va_list arg;
25 char buf[128];
26 _Thread *t;
27 char *p;
28 static int fd = -1;
30 if(_threaddebuglevel == 0)
31 return;
33 if(fd < 0){
34 p = strrchr(argv0, '/');
35 if(p)
36 p++;
37 else
38 p = argv0;
39 snprint(buf, sizeof buf, "/tmp/%s.tlog", p);
40 if((fd = create(buf, OWRITE, 0666)) < 0)
41 fd = open("/dev/null", OWRITE);
42 if(fd >= 0 && fd != 2){
43 dup(fd, 2);
44 close(fd);
45 fd = 2;
46 }
47 }
49 va_start(arg, fmt);
50 vsnprint(buf, sizeof buf, fmt, arg);
51 va_end(arg);
52 t = proc()->thread;
53 if(t)
54 fprint(fd, "%d.%d: %s\n", getpid(), t->id, buf);
55 else
56 fprint(fd, "%d._: %s\n", getpid(), buf);
57 }
59 static _Thread*
60 getthreadnow(void)
61 {
62 return proc()->thread;
63 }
64 _Thread *(*threadnow)(void) = getthreadnow;
66 static Proc*
67 procalloc(void)
68 {
69 Proc *p;
71 p = malloc(sizeof *p);
72 if(p == nil)
73 sysfatal("procalloc malloc: %r");
74 memset(p, 0, sizeof *p);
75 addproc(p);
76 lock(&threadnproclock);
77 threadnproc++;
78 unlock(&threadnproclock);
79 return p;
80 }
82 static void
83 threadstart(uint y, uint x)
84 {
85 _Thread *t;
86 ulong z;
88 z = x<<16; /* hide undefined 32-bit shift from 32-bit compilers */
89 z <<= 16;
90 z |= y;
91 t = (_Thread*)z;
93 /*print("threadstart %p\n", v); */
94 t->startfn(t->startarg);
95 /*print("threadexits %p\n", v); */
96 threadexits(nil);
97 /*print("not reacehd\n"); */
98 }
100 static _Thread*
101 threadalloc(void (*fn)(void*), void *arg, uint stack)
103 _Thread *t;
104 sigset_t zero;
105 uint x, y;
106 ulong z;
108 /* allocate the task and stack together */
109 t = malloc(sizeof *t+stack);
110 if(t == nil)
111 sysfatal("threadalloc malloc: %r");
112 memset(t, 0, sizeof *t);
113 t->stk = (uchar*)(t+1);
114 t->stksize = stack;
115 t->id = incref(&threadidref);
116 t->startfn = fn;
117 t->startarg = arg;
119 /* do a reasonable initialization */
120 memset(&t->context.uc, 0, sizeof t->context.uc);
121 sigemptyset(&zero);
122 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
124 /* must initialize with current context */
125 if(getcontext(&t->context.uc) < 0)
126 sysfatal("threadalloc getcontext: %r");
128 /* call makecontext to do the real work. */
129 /* leave a few words open on both ends */
130 t->context.uc.uc_stack.ss_sp = t->stk+8;
131 t->context.uc.uc_stack.ss_size = t->stksize-64;
132 #if defined(__sun__) && !defined(__MAKECONTEXT_V2_SOURCE) /* sigh */
133 /* can avoid this with __MAKECONTEXT_V2_SOURCE but only on SunOS 5.9 */
134 t->context.uc.uc_stack.ss_sp =
135 (char*)t->context.uc.uc_stack.ss_sp
136 +t->context.uc.uc_stack.ss_size;
137 #endif
138 /*
139 * All this magic is because you have to pass makecontext a
140 * function that takes some number of word-sized variables,
141 * and on 64-bit machines pointers are bigger than words.
142 */
143 z = (ulong)t;
144 y = z;
145 z >>= 16; /* hide undefined 32-bit shift from 32-bit compilers */
146 x = z>>16;
147 makecontext(&t->context.uc, (void(*)(void))threadstart, 2, y, x);
149 return t;
152 _Thread*
153 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
155 _Thread *t;
157 t = threadalloc(fn, arg, stack);
158 t->proc = p;
159 addthreadinproc(p, t);
160 p->nthread++;
161 _threadready(t);
162 return t;
165 int
166 threadcreate(void (*fn)(void*), void *arg, uint stack)
168 _Thread *t;
170 t = _threadcreate(proc(), fn, arg, stack);
171 return t->id;
174 int
175 proccreate(void (*fn)(void*), void *arg, uint stack)
177 int id;
178 _Thread *t;
179 Proc *p;
181 p = procalloc();
182 t = _threadcreate(p, fn, arg, stack);
183 id = t->id; /* t might be freed after _procstart */
184 _procstart(p, procscheduler);
185 return id;
188 void
189 _threadswitch(void)
191 Proc *p;
193 needstack(0);
194 p = proc();
195 /*print("threadswtch %p\n", p); */
196 contextswitch(&p->thread->context, &p->schedcontext);
199 void
200 _threadready(_Thread *t)
202 Proc *p;
204 p = t->proc;
205 lock(&p->lock);
206 p->runrend.l = &p->lock;
207 addthread(&p->runqueue, t);
208 /*print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid); */
209 if(p != proc())
210 _procwakeupandunlock(&p->runrend);
211 else
212 unlock(&p->lock);
215 int
216 threadidle(void)
218 int n;
219 Proc *p;
221 p = proc();
222 n = p->nswitch;
223 lock(&p->lock);
224 p->runrend.l = &p->lock;
225 addthread(&p->idlequeue, p->thread);
226 unlock(&p->lock);
227 _threadswitch();
228 return p->nswitch - n;
231 int
232 threadyield(void)
234 int n;
235 Proc *p;
237 p = proc();
238 n = p->nswitch;
239 _threadready(p->thread);
240 _threadswitch();
241 return p->nswitch - n;
244 void
245 threadexits(char *msg)
247 Proc *p;
249 p = proc();
250 if(msg == nil)
251 msg = "";
252 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
253 proc()->thread->exiting = 1;
254 _threadswitch();
257 static void
258 contextswitch(Context *from, Context *to)
260 if(swapcontext(&from->uc, &to->uc) < 0){
261 fprint(2, "swapcontext failed: %r\n");
262 assert(0);
266 static void
267 procscheduler(Proc *p)
269 _Thread *t;
271 setproc(p);
272 _threaddebug("scheduler enter");
273 /* print("s %p\n", p); */
274 lock(&p->lock);
275 for(;;){
276 while((t = p->runqueue.head) == nil){
277 if(p->nthread == 0)
278 goto Out;
279 if((t = p->idlequeue.head) != nil){
280 /*
281 * Run all the idling threads once.
282 */
283 while((t = p->idlequeue.head) != nil){
284 delthread(&p->idlequeue, t);
285 addthread(&p->runqueue, t);
287 continue;
289 p->runrend.l = &p->lock;
290 _threaddebug("scheduler sleep");
291 _procsleep(&p->runrend);
292 _threaddebug("scheduler wake");
294 delthread(&p->runqueue, t);
295 unlock(&p->lock);
296 p->thread = t;
297 p->nswitch++;
298 _threaddebug("run %d (%s)", t->id, t->name);
299 contextswitch(&p->schedcontext, &t->context);
300 /*print("back in scheduler\n"); */
301 p->thread = nil;
302 lock(&p->lock);
303 if(t->exiting){
304 delthreadinproc(p, t);
305 p->nthread--;
306 /*print("nthread %d\n", p->nthread); */
307 free(t);
311 Out:
312 _threaddebug("scheduler exit");
313 if(p->mainproc){
314 /*
315 * Stupid bug - on Linux 2.6 and maybe elsewhere,
316 * if the main thread exits then the others keep running
317 * but the process shows up as a zombie in ps and is not
318 * attachable with ptrace. We'll just sit around pretending
319 * to be a system proc instead of exiting.
320 */
321 _threaddaemonize();
322 lock(&threadnproclock);
323 if(++threadnsysproc == threadnproc)
324 threadexitsall(p->msg);
325 p->sysproc = 1;
326 unlock(&threadnproclock);
327 for(;;)
328 sleep(1000);
331 delproc(p);
332 lock(&threadnproclock);
333 if(p->sysproc)
334 --threadnsysproc;
335 if(--threadnproc == threadnsysproc)
336 threadexitsall(p->msg);
337 unlock(&threadnproclock);
338 unlock(&p->lock);
339 _threadsetproc(nil);
340 free(p);
343 void
344 _threadsetsysproc(void)
346 lock(&threadnproclock);
347 if(++threadnsysproc == threadnproc)
348 threadexitsall(nil);
349 unlock(&threadnproclock);
350 proc()->sysproc = 1;
353 void**
354 procdata(void)
356 return &proc()->udata;
359 void**
360 threaddata(void)
362 return &proc()->thread->udata;
365 extern Jmp *(*_notejmpbuf)(void);
366 static Jmp*
367 threadnotejmp(void)
369 return &proc()->sigjmp;
372 /*
373 * debugging
374 */
375 void
376 threadsetname(char *fmt, ...)
378 va_list arg;
379 _Thread *t;
381 t = proc()->thread;
382 va_start(arg, fmt);
383 vsnprint(t->name, sizeof t->name, fmt, arg);
384 va_end(arg);
387 char*
388 threadgetname(void)
390 return proc()->thread->name;
393 void
394 threadsetstate(char *fmt, ...)
396 va_list arg;
397 _Thread *t;
399 t = proc()->thread;
400 va_start(arg, fmt);
401 vsnprint(t->state, sizeof t->name, fmt, arg);
402 va_end(arg);
405 int
406 threadid(void)
408 _Thread *t;
410 t = proc()->thread;
411 return t->id;
414 void
415 needstack(int n)
417 _Thread *t;
419 t = proc()->thread;
421 if((char*)&t <= (char*)t->stk
422 || (char*)&t - (char*)t->stk < 256+n){
423 fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
424 abort();
428 /*
429 * locking
430 */
431 static int
432 threadqlock(QLock *l, int block, ulong pc)
434 /*print("threadqlock %p\n", l); */
435 lock(&l->l);
436 if(l->owner == nil){
437 l->owner = (*threadnow)();
438 /*print("qlock %p @%#x by %p\n", l, pc, l->owner); */
439 unlock(&l->l);
440 return 1;
442 if(!block){
443 unlock(&l->l);
444 return 0;
446 /*print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)()); */
447 addthread(&l->waiting, (*threadnow)());
448 unlock(&l->l);
450 _threadswitch();
452 if(l->owner != (*threadnow)()){
453 fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
454 argv0, pc, l->owner, (*threadnow)());
455 abort();
457 /*print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)()); */
458 return 1;
461 static void
462 threadqunlock(QLock *l, ulong pc)
464 _Thread *ready;
466 lock(&l->l);
467 /*print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner); */
468 if(l->owner == 0){
469 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
470 argv0, pc, l->owner, (*threadnow)());
471 abort();
473 if((l->owner = ready = l->waiting.head) != nil)
474 delthread(&l->waiting, l->owner);
475 /*
476 * N.B. Cannot call _threadready() before unlocking l->l,
477 * because the thread we are readying might:
478 * - be in another proc
479 * - start running immediately
480 * - and free l before we get a chance to run again
481 */
482 unlock(&l->l);
483 if(ready)
484 _threadready(l->owner);
487 static int
488 threadrlock(RWLock *l, int block, ulong pc)
490 USED(pc);
492 lock(&l->l);
493 if(l->writer == nil && l->wwaiting.head == nil){
494 l->readers++;
495 unlock(&l->l);
496 return 1;
498 if(!block){
499 unlock(&l->l);
500 return 0;
502 addthread(&l->rwaiting, (*threadnow)());
503 unlock(&l->l);
504 _threadswitch();
505 return 1;
508 static int
509 threadwlock(RWLock *l, int block, ulong pc)
511 USED(pc);
513 lock(&l->l);
514 if(l->writer == nil && l->readers == 0){
515 l->writer = (*threadnow)();
516 unlock(&l->l);
517 return 1;
519 if(!block){
520 unlock(&l->l);
521 return 0;
523 addthread(&l->wwaiting, (*threadnow)());
524 unlock(&l->l);
525 _threadswitch();
526 return 1;
529 static void
530 threadrunlock(RWLock *l, ulong pc)
532 _Thread *t;
534 USED(pc);
535 t = nil;
536 lock(&l->l);
537 --l->readers;
538 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
539 delthread(&l->wwaiting, t);
540 l->writer = t;
542 unlock(&l->l);
543 if(t)
544 _threadready(t);
548 static void
549 threadwunlock(RWLock *l, ulong pc)
551 _Thread *t;
553 USED(pc);
554 lock(&l->l);
555 l->writer = nil;
556 assert(l->readers == 0);
557 while((t = l->rwaiting.head) != nil){
558 delthread(&l->rwaiting, t);
559 l->readers++;
560 _threadready(t);
562 t = nil;
563 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
564 delthread(&l->wwaiting, t);
565 l->writer = t;
567 unlock(&l->l);
568 if(t)
569 _threadready(t);
572 /*
573 * sleep and wakeup
574 */
575 static void
576 threadrsleep(Rendez *r, ulong pc)
578 addthread(&r->waiting, proc()->thread);
579 qunlock(r->l);
580 _threadswitch();
581 qlock(r->l);
584 static int
585 threadrwakeup(Rendez *r, int all, ulong pc)
587 int i;
588 _Thread *t;
590 for(i=0;; i++){
591 if(i==1 && !all)
592 break;
593 if((t = r->waiting.head) == nil)
594 break;
595 delthread(&r->waiting, t);
596 _threadready(t);
598 return i;
601 /*
602 * startup
603 */
605 static int threadargc;
606 static char **threadargv;
607 int mainstacksize;
608 extern int _p9usepwlibrary; /* getgrgid etc. smash the stack - tell _p9dir just say no */
609 static void
610 threadmainstart(void *v)
612 USED(v);
614 /*
615 * N.B. This call to proc() is a program's first call (indirectly) to a
616 * pthreads function while executing on a non-pthreads-allocated
617 * stack. If the pthreads implementation is using the stack pointer
618 * to locate the per-thread data, then this call will blow up.
619 * This means the pthread implementation is not suitable for
620 * running under libthread. Time to write your own. Sorry.
621 */
622 _p9usepwlibrary = 0;
623 threadmainproc = proc();
624 threadmain(threadargc, threadargv);
627 int
628 main(int argc, char **argv)
630 Proc *p;
632 argv0 = argv[0];
634 write(1, "", 0);
635 if(getenv("NOLIBTHREADDAEMONIZE") == nil)
636 _threadsetupdaemonize();
638 threadargc = argc;
639 threadargv = argv;
641 /*
642 * Install locking routines into C library.
643 */
644 _lock = _threadlock;
645 _unlock = _threadunlock;
646 _qlock = threadqlock;
647 _qunlock = threadqunlock;
648 _rlock = threadrlock;
649 _runlock = threadrunlock;
650 _wlock = threadwlock;
651 _wunlock = threadwunlock;
652 _rsleep = threadrsleep;
653 _rwakeup = threadrwakeup;
654 _notejmpbuf = threadnotejmp;
656 _pthreadinit();
657 p = procalloc();
658 p->mainproc = 1;
659 _threadsetproc(p);
660 if(mainstacksize == 0)
661 mainstacksize = 256*1024;
662 atnotify(threadinfo, 1);
663 _threadcreate(p, threadmainstart, nil, mainstacksize);
664 procscheduler(p);
665 sysfatal("procscheduler returned in threadmain!");
666 /* does not return */
667 return 0;
670 /*
671 * hooray for linked lists
672 */
673 static void
674 addthread(_Threadlist *l, _Thread *t)
676 if(l->tail){
677 l->tail->next = t;
678 t->prev = l->tail;
679 }else{
680 l->head = t;
681 t->prev = nil;
683 l->tail = t;
684 t->next = nil;
687 static void
688 delthread(_Threadlist *l, _Thread *t)
690 if(t->prev)
691 t->prev->next = t->next;
692 else
693 l->head = t->next;
694 if(t->next)
695 t->next->prev = t->prev;
696 else
697 l->tail = t->prev;
700 static void
701 addthreadinproc(Proc *p, _Thread *t)
703 _Threadlist *l;
705 l = &p->allthreads;
706 if(l->tail){
707 l->tail->allnext = t;
708 t->allprev = l->tail;
709 }else{
710 l->head = t;
711 t->allprev = nil;
713 l->tail = t;
714 t->allnext = nil;
717 static void
718 delthreadinproc(Proc *p, _Thread *t)
720 _Threadlist *l;
722 l = &p->allthreads;
723 if(t->allprev)
724 t->allprev->allnext = t->allnext;
725 else
726 l->head = t->allnext;
727 if(t->allnext)
728 t->allnext->allprev = t->allprev;
729 else
730 l->tail = t->allprev;
733 Proc *_threadprocs;
734 Lock _threadprocslock;
735 static Proc *_threadprocstail;
737 static void
738 addproc(Proc *p)
740 lock(&_threadprocslock);
741 if(_threadprocstail){
742 _threadprocstail->next = p;
743 p->prev = _threadprocstail;
744 }else{
745 _threadprocs = p;
746 p->prev = nil;
748 _threadprocstail = p;
749 p->next = nil;
750 unlock(&_threadprocslock);
753 static void
754 delproc(Proc *p)
756 lock(&_threadprocslock);
757 if(p->prev)
758 p->prev->next = p->next;
759 else
760 _threadprocs = p->next;
761 if(p->next)
762 p->next->prev = p->prev;
763 else
764 _threadprocstail = p->prev;
765 unlock(&_threadprocslock);
768 /*
769 * notify - for now just use the usual mechanisms
770 */
771 void
772 threadnotify(int (*f)(void*, char*), int in)
774 atnotify(f, in);
777 static int
778 onrunqueue(Proc *p, _Thread *t)
780 _Thread *tt;
782 for(tt=p->runqueue.head; tt; tt=tt->next)
783 if(tt == t)
784 return 1;
785 return 0;
788 /*
789 * print state - called from SIGINFO
790 */
791 static int
792 threadinfo(void *v, char *s)
794 Proc *p;
795 _Thread *t;
797 if(strcmp(s, "quit") != 0 && strcmp(s, "sys: status request") != 0)
798 return 0;
800 for(p=_threadprocs; p; p=p->next){
801 fprint(2, "proc %p %s%s\n", (void*)p->osprocid, p->msg,
802 p->sysproc ? " (sysproc)": "");
803 for(t=p->allthreads.head; t; t=t->allnext){
804 fprint(2, "\tthread %d %s: %s %s\n",
805 t->id,
806 t == p->thread ? "Running" :
807 onrunqueue(p, t) ? "Ready" : "Sleeping",
808 t->state, t->name);
811 return 1;