1 #include "threadimpl.h"
3 int _threaddebuglevel = 0;
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 procmain(Proc*);
19 static int threadinfo(void*, char*);
20 static void pthreadscheduler(Proc *p);
21 static void pthreadsleepschedlocked(Proc *p, _Thread *t);
22 static void pthreadwakeupschedlocked(Proc *p, _Thread *self, _Thread *t);
23 static _Thread* procnext(Proc*, _Thread*);
26 _threaddebug(_Thread *t, char *fmt, ...)
33 if(_threaddebuglevel == 0)
37 p = strrchr(argv0, '/');
42 snprint(buf, sizeof buf, "/tmp/%s.tlog", p);
43 if((fd = create(buf, OWRITE, 0666)) < 0)
44 fd = open("/dev/null", OWRITE);
45 if(fd >= 0 && fd != 2){
53 vsnprint(buf, sizeof buf, fmt, arg);
58 fprint(fd, "%p %d.%d: %s\n", proc(), getpid(), t->id, buf);
60 fprint(fd, "%p %d._: %s\n", proc(), getpid(), buf);
66 return proc()->thread;
68 _Thread *(*threadnow)(void) = getthreadnow;
75 p = malloc(sizeof *p);
77 sysfatal("procalloc malloc: %r");
78 memset(p, 0, sizeof *p);
80 lock(&threadnproclock);
82 unlock(&threadnproclock);
87 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
92 t = malloc(sizeof *t);
94 sysfatal("threadcreate malloc: %r");
95 memset(t, 0, sizeof *t);
96 t->id = incref(&threadidref);
101 _threadpthreadstart(p, t);
105 addthreadinproc(p, t);
111 threadcreate(void (*fn)(void*), void *arg, uint stack)
115 t = _threadcreate(proc(), fn, arg, stack);
116 _threaddebug(nil, "threadcreate %d", t->id);
121 proccreate(void (*fn)(void*), void *arg, uint stack)
128 t = _threadcreate(p, fn, arg, stack);
129 id = t->id; /* t might be freed after _procstart */
130 _threaddebug(t, "proccreate %p", p);
131 _procstart(p, procmain);
142 /*print("threadswtch %p\n", p); */
147 _threadready(_Thread *t)
153 p->runrend.l = &p->lock;
154 addthread(&p->runqueue, t);
155 /*print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid); */
157 _procwakeupandunlock(&p->runrend);
171 p->runrend.l = &p->lock;
172 addthread(&p->idlequeue, p->thread);
175 return p->nswitch - n;
186 _threadready(p->thread);
188 return p->nswitch - n;
192 threadexits(char *msg)
199 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
200 proc()->thread->exiting = 1;
211 fprint(2, "already pinning a thread - %p %p\n", p->pinthread, p->thread);
214 p->pinthread = p->thread;
223 if(p->pinthread != p->thread){
224 fprint(2, "wrong pinthread - %p %p\n", p->pinthread, p->thread);
231 threadsysfatal(char *fmt, va_list arg)
235 vseprint(buf, buf+sizeof(buf), fmt, arg);
237 fprint(2, "%s: %s\n", argv0 ? argv0 : "<prog>", buf);
248 /* take out first thread to run on system stack */
249 t = p->runqueue.head;
250 delthread(&p->runqueue, t);
254 t->startfn(t->startarg);
260 _threadpthreadmain(Proc *p, _Thread *t)
264 pthreadsleepschedlocked(p, t);
266 _threaddebug(nil, "startfn");
267 t->startfn(t->startarg);
272 pthreadsleepschedlocked(Proc *p, _Thread *t)
274 _threaddebug(t, "pthreadsleepsched %p %d", p, t->id);;
275 t->schedrend.l = &p->lock;
276 while(p->schedthread != t)
277 _procsleep(&t->schedrend);
281 pthreadwakeupschedlocked(Proc *p, _Thread *self, _Thread *t)
283 _threaddebug(self, "pthreadwakeupschedlocked %p %d", p, t->id);;
284 t->schedrend.l = &p->lock;
286 _procwakeup(&t->schedrend);
290 pthreadscheduler(Proc *p)
294 _threaddebug(nil, "scheduler");
298 _threaddebug(self, "pausing");
301 _threaddebug(self, "exiting");
302 delthreadinproc(p, self);
306 t = procnext(p, self);
308 pthreadwakeupschedlocked(p, self, t);
310 pthreadsleepschedlocked(p, self);
311 _threaddebug(nil, "resume %d", self->id);
318 /* Tear down proc bookkeeping. Wait to free p. */
320 lock(&threadnproclock);
323 if(--threadnproc == threadnsysproc)
324 threadexitsall(p->msg);
325 unlock(&threadnproclock);
328 /* Tear down pthread. */
329 if(self->mainthread && p->mainproc) {
331 _threaddebug(self, "sleeper");
334 * Avoid bugs with main pthread exiting.
335 * When all procs are gone, threadexitsall above will happen.
349 procnext(Proc *p, _Thread *self)
353 if((t = p->pinthread) != nil){
354 while(!onlist(&p->runqueue, t)){
355 p->runrend.l = &p->lock;
356 _threaddebug(self, "scheduler sleep (pin)");
357 _procsleep(&p->runrend);
358 _threaddebug(self, "scheduler wake (pin)");
361 while((t = p->runqueue.head) == nil){
364 if((t = p->idlequeue.head) != nil){
366 * Run all the idling threads once.
368 while((t = p->idlequeue.head) != nil){
369 delthread(&p->idlequeue, t);
370 addthread(&p->runqueue, t);
374 p->runrend.l = &p->lock;
375 _threaddebug(self, "scheduler sleep");
376 _procsleep(&p->runrend);
377 _threaddebug(self, "scheduler wake");
380 if(p->pinthread && p->pinthread != t)
381 fprint(2, "p->pinthread %p t %p\n", p->pinthread, t);
382 assert(p->pinthread == nil || p->pinthread == t);
383 delthread(&p->runqueue, t);
391 _threadsetsysproc(void)
393 lock(&threadnproclock);
394 if(++threadnsysproc == threadnproc)
396 unlock(&threadnproclock);
403 return &proc()->udata;
409 return &proc()->thread->udata;
412 extern Jmp *(*_notejmpbuf)(void);
416 return &proc()->sigjmp;
423 threadsetname(char *fmt, ...)
430 vsnprint(t->name, sizeof t->name, fmt, arg);
437 return proc()->thread->name;
441 threadsetstate(char *fmt, ...)
448 vsnprint(t->state, sizeof t->name, fmt, arg);
470 if((char*)&t <= (char*)t->stk
471 || (char*)&t - (char*)t->stk < 256+n){
472 fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
480 return threadnproc == 1 && _threadprocs->nthread == 1;
487 threadqlock(QLock *l, int block, ulong pc)
489 /*print("threadqlock %p\n", l); */
492 l->owner = (*threadnow)();
493 /*print("qlock %p @%#x by %p\n", l, pc, l->owner); */
494 if(l->owner == nil) {
495 fprint(2, "%s: qlock uncontended owner=nil oops\n", argv0);
506 if(singlethreaded()){
507 fprint(2, "qlock deadlock\n");
511 /*print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)()); */
512 addthread(&l->waiting, (*threadnow)());
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)());
522 if(l->owner == nil) {
523 fprint(2, "%s: qlock threadswitch owner=nil oops\n", argv0);
527 /*print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)()); */
532 threadqunlock(QLock *l, ulong pc)
537 /*print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner); */
539 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
540 argv0, pc, l->owner, (*threadnow)());
543 if((l->owner = ready = l->waiting.head) != nil)
544 delthread(&l->waiting, l->owner);
546 * N.B. Cannot call _threadready() before unlocking l->l,
547 * because the thread we are readying might:
548 * - be in another proc
549 * - start running immediately
550 * - and free l before we get a chance to run again
554 _threadready(l->owner);
558 threadrlock(RWLock *l, int block, ulong pc)
563 if(l->writer == nil && l->wwaiting.head == nil){
572 if(singlethreaded()){
573 fprint(2, "rlock deadlock\n");
576 addthread(&l->rwaiting, (*threadnow)());
583 threadwlock(RWLock *l, int block, ulong pc)
588 if(l->writer == nil && l->readers == 0){
589 l->writer = (*threadnow)();
597 if(singlethreaded()){
598 fprint(2, "wlock deadlock\n");
601 addthread(&l->wwaiting, (*threadnow)());
608 threadrunlock(RWLock *l, ulong pc)
616 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
617 delthread(&l->wwaiting, t);
627 threadwunlock(RWLock *l, ulong pc)
634 assert(l->readers == 0);
635 while((t = l->rwaiting.head) != nil){
636 delthread(&l->rwaiting, t);
641 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
642 delthread(&l->wwaiting, t);
654 threadrsleep(Rendez *r, ulong pc)
656 if(singlethreaded()){
657 fprint(2, "rsleep deadlock\n");
660 addthread(&r->waiting, proc()->thread);
667 threadrwakeup(Rendez *r, int all, ulong pc)
672 _threaddebug(nil, "rwakeup %p %d", r, all);
676 if((t = r->waiting.head) == nil)
678 _threaddebug(nil, "rwakeup %p %d -> wake %d", r, all, t->id);
679 delthread(&r->waiting, t);
681 _threaddebug(nil, "rwakeup %p %d -> loop", r, all);
683 _threaddebug(nil, "rwakeup %p %d -> total %d", r, all, i);
691 static int threadargc;
692 static char **threadargv;
694 extern int _p9usepwlibrary; /* getgrgid etc. smash the stack - tell _p9dir just say no */
696 threadmainstart(void *v)
701 * N.B. This call to proc() is a program's first call (indirectly) to a
702 * pthreads function while executing on a non-pthreads-allocated
703 * stack. If the pthreads implementation is using the stack pointer
704 * to locate the per-thread data, then this call will blow up.
705 * This means the pthread implementation is not suitable for
706 * running under libthread. Time to write your own. Sorry.
709 threadmainproc = proc();
710 threadmain(threadargc, threadargv);
713 extern void (*_sysfatal)(char*, va_list);
716 main(int argc, char **argv)
724 opts = getenv("LIBTHREAD");
728 if(threadmaybackground() && strstr(opts, "nodaemon") == nil && getenv("NOLIBTHREADDAEMONIZE") == nil)
729 _threadsetupdaemonize();
735 * Install locking routines into C library.
738 _unlock = _threadunlock;
739 _qlock = threadqlock;
740 _qunlock = threadqunlock;
741 _rlock = threadrlock;
742 _runlock = threadrunlock;
743 _wlock = threadwlock;
744 _wunlock = threadwunlock;
745 _rsleep = threadrsleep;
746 _rwakeup = threadrwakeup;
747 _notejmpbuf = threadnotejmp;
749 _unpin = threadunpin;
750 _sysfatal = threadsysfatal;
756 if(mainstacksize == 0)
757 mainstacksize = 256*1024;
758 atnotify(threadinfo, 1);
759 t = _threadcreate(p, threadmainstart, nil, mainstacksize);
762 sysfatal("procmain returned in libthread");
763 /* does not return */
768 * hooray for linked lists
771 addthread(_Threadlist *l, _Thread *t)
785 delthread(_Threadlist *l, _Thread *t)
788 t->prev->next = t->next;
792 t->next->prev = t->prev;
797 /* inefficient but rarely used */
799 onlist(_Threadlist *l, _Thread *t)
803 for(tt = l->head; tt; tt=tt->next)
810 addthreadinproc(Proc *p, _Thread *t)
816 l->tail->allnext = t;
817 t->allprev = l->tail;
827 delthreadinproc(Proc *p, _Thread *t)
833 t->allprev->allnext = t->allnext;
835 l->head = t->allnext;
837 t->allnext->allprev = t->allprev;
839 l->tail = t->allprev;
843 Lock _threadprocslock;
844 static Proc *_threadprocstail;
849 lock(&_threadprocslock);
850 if(_threadprocstail){
851 _threadprocstail->next = p;
852 p->prev = _threadprocstail;
857 _threadprocstail = p;
859 unlock(&_threadprocslock);
865 lock(&_threadprocslock);
867 p->prev->next = p->next;
869 _threadprocs = p->next;
871 p->next->prev = p->prev;
873 _threadprocstail = p->prev;
874 unlock(&_threadprocslock);
878 * notify - for now just use the usual mechanisms
881 threadnotify(int (*f)(void*, char*), int in)
887 onrunqueue(Proc *p, _Thread *t)
891 for(tt=p->runqueue.head; tt; tt=tt->next)
898 * print state - called from SIGINFO
901 threadinfo(void *v, char *s)
906 if(strcmp(s, "quit") != 0 && strcmp(s, "sys: status request") != 0)
909 for(p=_threadprocs; p; p=p->next){
910 fprint(2, "proc %p %s%s\n", (void*)p->osprocid, p->msg,
911 p->sysproc ? " (sysproc)": "");
912 for(t=p->allthreads.head; t; t=t->allnext){
913 fprint(2, "\tthread %d %s: %s %s\n",
915 t == p->thread ? "Running" :
916 onrunqueue(p, t) ? "Ready" : "Sleeping",