1 #include "threadimpl.h"
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*);
23 _threaddebug(char *fmt, ...)
31 if(_threaddebuglevel == 0)
35 p = strrchr(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){
51 vsnprint(buf, sizeof buf, fmt, arg);
55 fprint(fd, "%d.%d: %s\n", getpid(), t->id, buf);
57 fprint(fd, "%d._: %s\n", getpid(), buf);
63 return proc()->thread;
65 _Thread *(*threadnow)(void) = getthreadnow;
72 p = malloc(sizeof *p);
74 sysfatal("procalloc malloc: %r");
75 memset(p, 0, sizeof *p);
77 lock(&threadnproclock);
79 unlock(&threadnproclock);
84 threadstart(uint y, uint x)
89 z = x<<16; /* hide undefined 32-bit shift from 32-bit compilers */
94 /*print("threadstart %p\n", v); */
95 t->startfn(t->startarg);
96 /*print("threadexits %p\n", v); */
98 /*print("not reacehd\n"); */
102 threadalloc(void (*fn)(void*), void *arg, uint stack)
109 /* allocate the task and stack together */
110 t = malloc(sizeof *t+stack);
112 sysfatal("threadalloc malloc: %r");
113 memset(t, 0, sizeof *t);
114 t->stk = (uchar*)(t+1);
116 t->id = incref(&threadidref);
120 /* do a reasonable initialization */
121 memset(&t->context.uc, 0, sizeof t->context.uc);
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;
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.
146 z >>= 16; /* hide undefined 32-bit shift from 32-bit compilers */
148 makecontext(&t->context.uc, (void(*)(void))threadstart, 2, y, x);
154 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
158 t = threadalloc(fn, arg, stack);
160 addthreadinproc(p, t);
167 threadcreate(void (*fn)(void*), void *arg, uint stack)
171 t = _threadcreate(proc(), fn, arg, stack);
176 proccreate(void (*fn)(void*), void *arg, uint stack)
183 t = _threadcreate(p, fn, arg, stack);
184 id = t->id; /* t might be freed after _procstart */
185 _procstart(p, procscheduler);
196 /*print("threadswtch %p\n", p); */
197 contextswitch(&p->thread->context, &p->schedcontext);
201 _threadready(_Thread *t)
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); */
211 _procwakeupandunlock(&p->runrend);
225 p->runrend.l = &p->lock;
226 addthread(&p->idlequeue, p->thread);
229 return p->nswitch - n;
240 _threadready(p->thread);
242 return p->nswitch - n;
246 threadexits(char *msg)
253 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
254 proc()->thread->exiting = 1;
265 fprint(2, "already pinning a thread - %p %p\n", p->pinthread, p->thread);
268 p->pinthread = p->thread;
277 if(p->pinthread != p->thread){
278 fprint(2, "wrong pinthread - %p %p\n", p->pinthread, p->thread);
285 contextswitch(Context *from, Context *to)
287 if(swapcontext(&from->uc, &to->uc) < 0){
288 fprint(2, "swapcontext failed: %r\n");
294 procscheduler(Proc *p)
299 _threaddebug("scheduler enter");
300 /* print("s %p\n", p); */
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)");
311 while((t = p->runqueue.head) == nil){
314 if((t = p->idlequeue.head) != nil){
316 * Run all the idling threads once.
318 while((t = p->idlequeue.head) != nil){
319 delthread(&p->idlequeue, t);
320 addthread(&p->runqueue, t);
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);
336 _threaddebug("run %d (%s)", t->id, t->name);
337 contextswitch(&p->schedcontext, &t->context);
338 /*print("back in scheduler\n"); */
342 delthreadinproc(p, t);
344 /*print("nthread %d\n", p->nthread); */
350 _threaddebug("scheduler exit");
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.
360 lock(&threadnproclock);
361 if(++threadnsysproc == threadnproc)
362 threadexitsall(p->msg);
364 unlock(&threadnproclock);
370 lock(&threadnproclock);
373 if(--threadnproc == threadnsysproc)
374 threadexitsall(p->msg);
375 unlock(&threadnproclock);
382 _threadsetsysproc(void)
384 lock(&threadnproclock);
385 if(++threadnsysproc == threadnproc)
387 unlock(&threadnproclock);
394 return &proc()->udata;
400 return &proc()->thread->udata;
403 extern Jmp *(*_notejmpbuf)(void);
407 return &proc()->sigjmp;
414 threadsetname(char *fmt, ...)
421 vsnprint(t->name, sizeof t->name, fmt, arg);
428 return proc()->thread->name;
432 threadsetstate(char *fmt, ...)
439 vsnprint(t->state, sizeof t->name, fmt, arg);
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);
470 threadqlock(QLock *l, int block, ulong pc)
472 /*print("threadqlock %p\n", l); */
475 l->owner = (*threadnow)();
476 /*print("qlock %p @%#x by %p\n", l, pc, l->owner); */
484 /*print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)()); */
485 addthread(&l->waiting, (*threadnow)());
490 if(l->owner != (*threadnow)()){
491 fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
492 argv0, pc, l->owner, (*threadnow)());
495 /*print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)()); */
500 threadqunlock(QLock *l, ulong pc)
505 /*print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner); */
507 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
508 argv0, pc, l->owner, (*threadnow)());
511 if((l->owner = ready = l->waiting.head) != nil)
512 delthread(&l->waiting, l->owner);
514 * N.B. Cannot call _threadready() before unlocking l->l,
515 * because the thread we are readying might:
516 * - be in another proc
517 * - start running immediately
518 * - and free l before we get a chance to run again
522 _threadready(l->owner);
526 threadrlock(RWLock *l, int block, ulong pc)
531 if(l->writer == nil && l->wwaiting.head == nil){
540 addthread(&l->rwaiting, (*threadnow)());
547 threadwlock(RWLock *l, int block, ulong pc)
552 if(l->writer == nil && l->readers == 0){
553 l->writer = (*threadnow)();
561 addthread(&l->wwaiting, (*threadnow)());
568 threadrunlock(RWLock *l, ulong pc)
576 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
577 delthread(&l->wwaiting, t);
587 threadwunlock(RWLock *l, ulong pc)
594 assert(l->readers == 0);
595 while((t = l->rwaiting.head) != nil){
596 delthread(&l->rwaiting, t);
601 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
602 delthread(&l->wwaiting, t);
614 threadrsleep(Rendez *r, ulong pc)
616 addthread(&r->waiting, proc()->thread);
623 threadrwakeup(Rendez *r, int all, ulong pc)
631 if((t = r->waiting.head) == nil)
633 delthread(&r->waiting, t);
643 static int threadargc;
644 static char **threadargv;
646 extern int _p9usepwlibrary; /* getgrgid etc. smash the stack - tell _p9dir just say no */
648 threadmainstart(void *v)
653 * N.B. This call to proc() is a program's first call (indirectly) to a
654 * pthreads function while executing on a non-pthreads-allocated
655 * stack. If the pthreads implementation is using the stack pointer
656 * to locate the per-thread data, then this call will blow up.
657 * This means the pthread implementation is not suitable for
658 * running under libthread. Time to write your own. Sorry.
661 threadmainproc = proc();
662 threadmain(threadargc, threadargv);
666 main(int argc, char **argv)
673 if(getenv("NOLIBTHREADDAEMONIZE") == nil)
674 _threadsetupdaemonize();
680 * Install locking routines into C library.
683 _unlock = _threadunlock;
684 _qlock = threadqlock;
685 _qunlock = threadqunlock;
686 _rlock = threadrlock;
687 _runlock = threadrunlock;
688 _wlock = threadwlock;
689 _wunlock = threadwunlock;
690 _rsleep = threadrsleep;
691 _rwakeup = threadrwakeup;
692 _notejmpbuf = threadnotejmp;
694 _unpin = threadunpin;
700 if(mainstacksize == 0)
701 mainstacksize = 256*1024;
702 atnotify(threadinfo, 1);
703 _threadcreate(p, threadmainstart, nil, mainstacksize);
705 sysfatal("procscheduler returned in threadmain!");
706 /* does not return */
711 * hooray for linked lists
714 addthread(_Threadlist *l, _Thread *t)
728 delthread(_Threadlist *l, _Thread *t)
731 t->prev->next = t->next;
735 t->next->prev = t->prev;
740 /* inefficient but rarely used */
742 onlist(_Threadlist *l, _Thread *t)
746 for(tt = l->head; tt; tt=tt->next)
753 addthreadinproc(Proc *p, _Thread *t)
759 l->tail->allnext = t;
760 t->allprev = l->tail;
770 delthreadinproc(Proc *p, _Thread *t)
776 t->allprev->allnext = t->allnext;
778 l->head = t->allnext;
780 t->allnext->allprev = t->allprev;
782 l->tail = t->allprev;
786 Lock _threadprocslock;
787 static Proc *_threadprocstail;
792 lock(&_threadprocslock);
793 if(_threadprocstail){
794 _threadprocstail->next = p;
795 p->prev = _threadprocstail;
800 _threadprocstail = p;
802 unlock(&_threadprocslock);
808 lock(&_threadprocslock);
810 p->prev->next = p->next;
812 _threadprocs = p->next;
814 p->next->prev = p->prev;
816 _threadprocstail = p->prev;
817 unlock(&_threadprocslock);
821 * notify - for now just use the usual mechanisms
824 threadnotify(int (*f)(void*, char*), int in)
830 onrunqueue(Proc *p, _Thread *t)
834 for(tt=p->runqueue.head; tt; tt=tt->next)
841 * print state - called from SIGINFO
844 threadinfo(void *v, char *s)
849 if(strcmp(s, "quit") != 0 && strcmp(s, "sys: status request") != 0)
852 for(p=_threadprocs; p; p=p->next){
853 fprint(2, "proc %p %s%s\n", (void*)p->osprocid, p->msg,
854 p->sysproc ? " (sysproc)": "");
855 for(t=p->allthreads.head; t; t=t->allnext){
856 fprint(2, "\tthread %d %s: %s %s\n",
858 t == p->thread ? "Running" :
859 onrunqueue(p, t) ? "Ready" : "Sleeping",