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);
314 _threadsetsysproc(void)
316 lock(&threadnproclock);
317 if(++threadnsysproc == threadnproc)
319 unlock(&threadnproclock);
326 return &proc()->udata;
332 return &proc()->thread->udata;
335 extern Jmp *(*_notejmpbuf)(void);
339 return &proc()->sigjmp;
346 threadsetname(char *fmt, ...)
353 vsnprint(t->name, sizeof t->name, fmt, arg);
360 return proc()->thread->name;
364 threadsetstate(char *fmt, ...)
371 vsnprint(t->state, sizeof t->name, fmt, arg);
382 if((char*)&t <= (char*)t->stk
383 || (char*)&t - (char*)t->stk < 256+n){
384 fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
393 threadqlock(QLock *l, int block, ulong pc)
395 //print("threadqlock %p\n", l);
398 l->owner = (*threadnow)();
399 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
407 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
408 addthread(&l->waiting, (*threadnow)());
413 if(l->owner != (*threadnow)()){
414 fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
415 argv0, pc, l->owner, (*threadnow)());
418 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
423 threadqunlock(QLock *l, ulong pc)
428 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
430 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
431 argv0, pc, l->owner, (*threadnow)());
434 if((l->owner = ready = l->waiting.head) != nil)
435 delthread(&l->waiting, l->owner);
437 * N.B. Cannot call _threadready() before unlocking l->l,
438 * because the thread we are readying might:
439 * - be in another proc
440 * - start running immediately
441 * - and free l before we get a chance to run again
445 _threadready(l->owner);
449 threadrlock(RWLock *l, int block, ulong pc)
454 if(l->writer == nil && l->wwaiting.head == nil){
463 addthread(&l->rwaiting, (*threadnow)());
470 threadwlock(RWLock *l, int block, ulong pc)
475 if(l->writer == nil && l->readers == 0){
476 l->writer = (*threadnow)();
484 addthread(&l->wwaiting, (*threadnow)());
491 threadrunlock(RWLock *l, ulong pc)
499 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
500 delthread(&l->wwaiting, t);
510 threadwunlock(RWLock *l, ulong pc)
517 assert(l->readers == 0);
518 while((t = l->rwaiting.head) != nil){
519 delthread(&l->rwaiting, t);
524 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
525 delthread(&l->wwaiting, t);
537 threadrsleep(Rendez *r, ulong pc)
539 addthread(&r->waiting, proc()->thread);
546 threadrwakeup(Rendez *r, int all, ulong pc)
554 if((t = r->waiting.head) == nil)
556 delthread(&r->waiting, t);
566 static int threadargc;
567 static char **threadargv;
571 threadmainstart(void *v)
576 * N.B. This call to proc() is a program's first call (indirectly) to a
577 * pthreads function while executing on a non-pthreads-allocated
578 * stack. If the pthreads implementation is using the stack pointer
579 * to locate the per-thread data, then this call will blow up.
580 * This means the pthread implementation is not suitable for
581 * running under libthread. Time to write your own. Sorry.
583 threadmainproc = proc();
584 threadmain(threadargc, threadargv);
588 main(int argc, char **argv)
594 _threadsetupdaemonize();
600 * Install locking routines into C library.
603 _unlock = _threadunlock;
604 _qlock = threadqlock;
605 _qunlock = threadqunlock;
606 _rlock = threadrlock;
607 _runlock = threadrunlock;
608 _wlock = threadwlock;
609 _wunlock = threadwunlock;
610 _rsleep = threadrsleep;
611 _rwakeup = threadrwakeup;
612 _notejmpbuf = threadnotejmp;
618 if(mainstacksize == 0)
619 mainstacksize = 256*1024;
620 _threadcreate(p, threadmainstart, nil, mainstacksize);
622 sysfatal("procscheduler returned in threadmain!");
623 /* does not return */
628 * hooray for linked lists
631 addthread(_Threadlist *l, _Thread *t)
645 delthread(_Threadlist *l, _Thread *t)
648 t->prev->next = t->next;
652 t->next->prev = t->prev;
658 addthreadinproc(Proc *p, _Thread *t)
664 l->tail->allnext = t;
665 t->allprev = l->tail;
675 delthreadinproc(Proc *p, _Thread *t)
681 t->allprev->allnext = t->allnext;
683 l->head = t->allnext;
685 t->allnext->allprev = t->allprev;
687 l->tail = t->allprev;
691 Lock _threadprocslock;
692 static Proc *_threadprocstail;
697 lock(&_threadprocslock);
698 if(_threadprocstail){
699 _threadprocstail->next = p;
700 p->prev = _threadprocstail;
705 _threadprocstail = p;
707 unlock(&_threadprocslock);
713 lock(&_threadprocslock);
715 p->prev->next = p->next;
717 _threadprocs = p->next;
719 p->next->prev = p->prev;
721 _threadprocstail = p->prev;
722 unlock(&_threadprocslock);
726 * notify - for now just use the usual mechanisms
729 threadnotify(int (*f)(void*, char*), int in)