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 scheduler(Proc*);
21 _threaddebug(char *fmt, ...)
30 vsnprint(buf, sizeof buf, fmt, arg);
34 fprint(2, "%d.%d: %s\n", getpid(), t->id, buf);
36 fprint(2, "%d._: %s\n", getpid(), buf);
42 return proc()->thread;
44 _Thread *(*threadnow)(void) = getthreadnow;
51 p = malloc(sizeof *p);
53 sysfatal("procalloc malloc: %r");
54 memset(p, 0, sizeof *p);
56 lock(&threadnproclock);
58 unlock(&threadnproclock);
68 t->startfn(t->startarg);
69 memset(&v, 0xff, 32); /* try to cut off stack traces */
74 threadalloc(void (*fn)(void*), void *arg, uint stack)
79 /* allocate the task and stack together */
80 t = malloc(sizeof *t+stack);
82 sysfatal("threadalloc malloc: %r");
83 memset(t, 0, sizeof *t);
84 t->stk = (uchar*)(t+1);
86 t->id = incref(&threadidref);
90 /* do a reasonable initialization */
91 memset(&t->context.uc, 0, sizeof t->context.uc);
93 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
95 /* must initialize with current context */
96 getcontext(&t->context.uc);
98 /* call makecontext to do the real work. */
99 /* leave a few words open on both ends */
100 t->context.uc.uc_stack.ss_sp = t->stk+8;
101 t->context.uc.uc_stack.ss_size = t->stksize-64;
102 makecontext(&t->context.uc, (void(*)())threadstart, 1, t);
108 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
112 t = threadalloc(fn, arg, stack);
114 addthreadinproc(p, t);
121 threadcreate(void (*fn)(void*), void *arg, uint stack)
125 t = _threadcreate(proc(), fn, arg, stack);
130 proccreate(void (*fn)(void*), void *arg, uint stack)
136 t = _threadcreate(p, fn, arg, stack);
137 _procstart(p, scheduler);
147 contextswitch(&p->thread->context, &p->schedcontext);
151 _threadready(_Thread *t)
157 addthread(&p->runqueue, t);
158 //print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid);
160 _procwakeup(&p->runrend);
172 _threadready(p->thread);
174 return p->nswitch - n;
178 threadexits(char *msg)
185 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
186 proc()->thread->exiting = 1;
191 contextswitch(Context *from, Context *to)
193 if(swapcontext(&from->uc, &to->uc) < 0){
194 fprint(2, "swapcontext failed: %r\n");
205 _threaddebug("scheduler enter");
206 // print("s %p %d\n", p, gettid());
209 while((t = p->runqueue.head) == nil){
212 p->runrend.l = &p->lock;
213 _threaddebug("scheduler sleep");
214 _procsleep(&p->runrend);
215 _threaddebug("scheduler wake");
217 delthread(&p->runqueue, t);
221 _threaddebug("run %d (%s)", t->id, t->name);
222 contextswitch(&p->schedcontext, &t->context);
226 delthreadinproc(p, t);
233 _threaddebug("scheduler exit");
235 lock(&threadnproclock);
238 if(--threadnproc == threadnsysproc)
239 threadexitsall(p->msg);
240 unlock(&threadnproclock);
247 _threadsetsysproc(void)
249 lock(&threadnproclock);
250 if(++threadnsysproc == threadnproc)
252 unlock(&threadnproclock);
259 return &proc()->udata;
262 extern Jmp *(*_notejmpbuf)(void);
266 return &proc()->sigjmp;
273 threadsetname(char *fmt, ...)
280 vsnprint(t->name, sizeof t->name, fmt, arg);
285 threadsetstate(char *fmt, ...)
292 vsnprint(t->state, sizeof t->name, fmt, arg);
300 threadqlock(QLock *l, int block, ulong pc)
304 l->owner = (*threadnow)();
305 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
313 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
314 addthread(&l->waiting, (*threadnow)());
319 if(l->owner != (*threadnow)()){
320 fprint(2, "qlock pc=0x%lux owner=%p self=%p oops\n", pc, l->owner, (*threadnow)());
323 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
328 threadqunlock(QLock *l, ulong pc)
331 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
333 fprint(2, "qunlock pc=0x%lux owner=%p self=%p oops\n",
334 pc, l->owner, (*threadnow)());
337 if((l->owner = l->waiting.head) != nil){
338 delthread(&l->waiting, l->owner);
339 _threadready(l->owner);
345 threadrlock(RWLock *l, int block, ulong pc)
350 if(l->writer == nil && l->wwaiting.head == nil){
359 addthread(&l->rwaiting, (*threadnow)());
366 threadwlock(RWLock *l, int block, ulong pc)
371 if(l->writer == nil && l->readers == 0){
372 l->writer = (*threadnow)();
380 addthread(&l->wwaiting, (*threadnow)());
387 threadrunlock(RWLock *l, ulong pc)
394 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
395 delthread(&l->wwaiting, t);
403 threadwunlock(RWLock *l, ulong pc)
410 assert(l->readers == 0);
411 while((t = l->rwaiting.head) != nil){
412 delthread(&l->rwaiting, t);
416 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
417 delthread(&l->wwaiting, t);
428 threadrsleep(Rendez *r, ulong pc)
430 addthread(&r->waiting, proc()->thread);
437 threadrwakeup(Rendez *r, int all, ulong pc)
445 if((t = r->waiting.head) == nil)
447 delthread(&r->waiting, t);
457 static int threadargc;
458 static char **threadargv;
462 threadmainstart(void *v)
465 threadmainproc = proc();
466 threadmain(threadargc, threadargv);
470 threadlinklibrary(void)
475 main(int argc, char **argv)
481 _threadsetupdaemonize();
487 * Install locking routines into C library.
490 _unlock = _threadunlock;
491 _qlock = threadqlock;
492 _qunlock = threadqunlock;
493 _rlock = threadrlock;
494 _runlock = threadrunlock;
495 _wlock = threadwlock;
496 _wunlock = threadwunlock;
497 _rsleep = threadrsleep;
498 _rwakeup = threadrwakeup;
499 _notejmpbuf = threadnotejmp;
504 if(mainstacksize == 0)
505 mainstacksize = 65536;
506 _threadcreate(p, threadmainstart, nil, mainstacksize);
513 * hooray for linked lists
516 addthread(_Threadlist *l, _Thread *t)
530 delthread(_Threadlist *l, _Thread *t)
533 t->prev->next = t->next;
537 t->next->prev = t->prev;
543 addthreadinproc(Proc *p, _Thread *t)
549 l->tail->allnext = t;
550 t->allprev = l->tail;
560 delthreadinproc(Proc *p, _Thread *t)
566 t->allprev->allnext = t->allnext;
568 l->head = t->allnext;
570 t->allnext->allprev = t->allprev;
572 l->tail = t->allprev;
576 Lock _threadprocslock;
577 static Proc *_threadprocstail;
582 lock(&_threadprocslock);
583 if(_threadprocstail){
584 _threadprocstail->next = p;
585 p->prev = _threadprocstail;
590 _threadprocstail = p;
592 unlock(&_threadprocslock);
598 lock(&_threadprocslock);
600 p->prev->next = p->next;
602 _threadprocs = p->next;
604 p->next->prev = p->prev;
606 _threadprocstail = p->prev;
607 unlock(&_threadprocslock);