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 void addthreadinproc(Proc*, _Thread*);
16 static void delthreadinproc(Proc*, _Thread*);
17 static void contextswitch(Context *from, Context *to);
18 static void procscheduler(Proc*);
21 _threaddebug(char *fmt, ...)
36 p = strrchr(argv0, '/');
41 snprint(buf, sizeof buf, "/tmp/%s.tlog", p);
42 if((fd = create(buf, OWRITE, 0666)) < 0)
43 fd = open("/dev/null", OWRITE);
47 vsnprint(buf, sizeof buf, fmt, arg);
51 fprint(fd, "%d.%d: %s\n", getpid(), t->id, buf);
53 fprint(fd, "%d._: %s\n", getpid(), buf);
59 return proc()->thread;
61 _Thread *(*threadnow)(void) = getthreadnow;
68 p = malloc(sizeof *p);
70 sysfatal("procalloc malloc: %r");
71 memset(p, 0, sizeof *p);
73 lock(&threadnproclock);
75 unlock(&threadnproclock);
80 threadstart(uint y, uint x)
85 z = x<<16; /* hide undefined 32-bit shift from 32-bit compilers */
90 //print("threadstart %p\n", v);
91 t->startfn(t->startarg);
92 //print("threadexits %p\n", v);
94 //print("not reacehd\n");
98 threadalloc(void (*fn)(void*), void *arg, uint stack)
105 /* allocate the task and stack together */
106 t = malloc(sizeof *t+stack);
108 sysfatal("threadalloc malloc: %r");
109 memset(t, 0, sizeof *t);
110 t->stk = (uchar*)(t+1);
112 t->id = incref(&threadidref);
116 /* do a reasonable initialization */
117 memset(&t->context.uc, 0, sizeof t->context.uc);
119 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
121 /* must initialize with current context */
122 if(getcontext(&t->context.uc) < 0)
123 sysfatal("threadalloc getcontext: %r");
125 /* call makecontext to do the real work. */
126 /* leave a few words open on both ends */
127 t->context.uc.uc_stack.ss_sp = t->stk+8;
128 t->context.uc.uc_stack.ss_size = t->stksize-64;
129 #ifdef __sun__ /* sigh */
130 /* can avoid this with __MAKECONTEXT_V2_SOURCE but only on SunOS 5.9 */
131 t->context.uc.uc_stack.ss_sp =
132 (char*)t->context.uc.uc_stack.ss_sp
133 +t->context.uc.uc_stack.ss_size;
136 * All this magic is because you have to pass makecontext a
137 * function that takes some number of word-sized variables,
138 * and on 64-bit machines pointers are bigger than words.
142 z >>= 16; /* hide undefined 32-bit shift from 32-bit compilers */
144 makecontext(&t->context.uc, (void(*)())threadstart, 2, y, x);
150 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
154 t = threadalloc(fn, arg, stack);
156 addthreadinproc(p, t);
163 threadcreate(void (*fn)(void*), void *arg, uint stack)
167 t = _threadcreate(proc(), fn, arg, stack);
172 proccreate(void (*fn)(void*), void *arg, uint stack)
179 t = _threadcreate(p, fn, arg, stack);
180 id = t->id; /* t might be freed after _procstart */
181 _procstart(p, procscheduler);
192 //print("threadswtch %p\n", p);
193 contextswitch(&p->thread->context, &p->schedcontext);
197 _threadready(_Thread *t)
203 p->runrend.l = &p->lock;
204 addthread(&p->runqueue, t);
205 //print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid);
207 _procwakeupandunlock(&p->runrend);
220 _threadready(p->thread);
222 return p->nswitch - n;
226 threadexits(char *msg)
233 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
234 proc()->thread->exiting = 1;
239 contextswitch(Context *from, Context *to)
241 if(swapcontext(&from->uc, &to->uc) < 0){
242 fprint(2, "swapcontext failed: %r\n");
248 procscheduler(Proc *p)
253 _threaddebug("scheduler enter");
254 // print("s %p\n", p);
257 while((t = p->runqueue.head) == nil){
260 p->runrend.l = &p->lock;
261 _threaddebug("scheduler sleep");
262 _procsleep(&p->runrend);
263 _threaddebug("scheduler wake");
265 delthread(&p->runqueue, t);
269 _threaddebug("run %d (%s)", t->id, t->name);
270 contextswitch(&p->schedcontext, &t->context);
271 //print("back in scheduler\n");
275 delthreadinproc(p, t);
277 //print("ntrhead %d\n", p->nthread);
283 _threaddebug("scheduler exit");
286 * Stupid bug - on Linux 2.6 and maybe elsewhere,
287 * if the main thread exits then the others keep running
288 * but the process shows up as a zombie in ps and is not
289 * attachable with ptrace. We'll just sit around pretending
290 * to be a system proc instead of exiting.
293 lock(&threadnproclock);
294 if(++threadnsysproc == threadnproc)
295 threadexitsall(p->msg);
297 unlock(&threadnproclock);
303 lock(&threadnproclock);
306 if(--threadnproc == threadnsysproc)
307 threadexitsall(p->msg);
308 unlock(&threadnproclock);
315 _threadsetsysproc(void)
317 lock(&threadnproclock);
318 if(++threadnsysproc == threadnproc)
320 unlock(&threadnproclock);
327 return &proc()->udata;
330 extern Jmp *(*_notejmpbuf)(void);
334 return &proc()->sigjmp;
341 threadsetname(char *fmt, ...)
348 vsnprint(t->name, sizeof t->name, fmt, arg);
355 return proc()->thread->name;
359 threadsetstate(char *fmt, ...)
366 vsnprint(t->state, sizeof t->name, fmt, arg);
377 if((char*)&t <= (char*)t->stk
378 || (char*)&t - (char*)t->stk < 256+n){
379 fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
388 threadqlock(QLock *l, int block, ulong pc)
390 //print("threadqlock %p\n", l);
393 l->owner = (*threadnow)();
394 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
402 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
403 addthread(&l->waiting, (*threadnow)());
408 if(l->owner != (*threadnow)()){
409 fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
410 argv0, pc, l->owner, (*threadnow)());
413 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
418 threadqunlock(QLock *l, ulong pc)
423 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
425 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
426 argv0, pc, l->owner, (*threadnow)());
429 if((l->owner = ready = l->waiting.head) != nil)
430 delthread(&l->waiting, l->owner);
432 * N.B. Cannot call _threadready() before unlocking l->l,
433 * because the thread we are readying might:
434 * - be in another proc
435 * - start running immediately
436 * - and free l before we get a chance to run again
440 _threadready(l->owner);
444 threadrlock(RWLock *l, int block, ulong pc)
449 if(l->writer == nil && l->wwaiting.head == nil){
458 addthread(&l->rwaiting, (*threadnow)());
465 threadwlock(RWLock *l, int block, ulong pc)
470 if(l->writer == nil && l->readers == 0){
471 l->writer = (*threadnow)();
479 addthread(&l->wwaiting, (*threadnow)());
486 threadrunlock(RWLock *l, ulong pc)
494 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
495 delthread(&l->wwaiting, t);
505 threadwunlock(RWLock *l, ulong pc)
512 assert(l->readers == 0);
513 while((t = l->rwaiting.head) != nil){
514 delthread(&l->rwaiting, t);
519 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
520 delthread(&l->wwaiting, t);
532 threadrsleep(Rendez *r, ulong pc)
534 addthread(&r->waiting, proc()->thread);
541 threadrwakeup(Rendez *r, int all, ulong pc)
549 if((t = r->waiting.head) == nil)
551 delthread(&r->waiting, t);
561 static int threadargc;
562 static char **threadargv;
566 threadmainstart(void *v)
571 * N.B. This call to proc() is a program's first call (indirectly) to a
572 * pthreads function while executing on a non-pthreads-allocated
573 * stack. If the pthreads implementation is using the stack pointer
574 * to locate the per-thread data, then this call will blow up.
575 * This means the pthread implementation is not suitable for
576 * running under libthread. Time to write your own. Sorry.
578 threadmainproc = proc();
579 threadmain(threadargc, threadargv);
583 main(int argc, char **argv)
589 _threadsetupdaemonize();
595 * Install locking routines into C library.
598 _unlock = _threadunlock;
599 _qlock = threadqlock;
600 _qunlock = threadqunlock;
601 _rlock = threadrlock;
602 _runlock = threadrunlock;
603 _wlock = threadwlock;
604 _wunlock = threadwunlock;
605 _rsleep = threadrsleep;
606 _rwakeup = threadrwakeup;
607 _notejmpbuf = threadnotejmp;
613 if(mainstacksize == 0)
614 mainstacksize = 256*1024;
615 _threadcreate(p, threadmainstart, nil, mainstacksize);
617 sysfatal("procscheduler returned in threadmain!");
618 /* does not return */
623 * hooray for linked lists
626 addthread(_Threadlist *l, _Thread *t)
640 delthread(_Threadlist *l, _Thread *t)
643 t->prev->next = t->next;
647 t->next->prev = t->prev;
653 addthreadinproc(Proc *p, _Thread *t)
659 l->tail->allnext = t;
660 t->allprev = l->tail;
670 delthreadinproc(Proc *p, _Thread *t)
676 t->allprev->allnext = t->allnext;
678 l->head = t->allnext;
680 t->allnext->allprev = t->allprev;
682 l->tail = t->allprev;
686 Lock _threadprocslock;
687 static Proc *_threadprocstail;
692 lock(&_threadprocslock);
693 if(_threadprocstail){
694 _threadprocstail->next = p;
695 p->prev = _threadprocstail;
700 _threadprocstail = p;
702 unlock(&_threadprocslock);
708 lock(&_threadprocslock);
710 p->prev->next = p->next;
712 _threadprocs = p->next;
714 p->next->prev = p->prev;
716 _threadprocstail = p->prev;
717 unlock(&_threadprocslock);
721 * notify - for now just use the usual mechanisms
724 threadnotify(int (*f)(void*, char*), int in)