4 #include "threadimpl.h"
8 static uint threadnproc;
9 static uint threadnsysproc;
10 static Lock threadnproclock;
11 static Ref threadidref;
12 static Proc *threadmainproc;
14 static void addproc(Proc*);
15 static void delproc(Proc*);
16 static void addthread(_Threadlist*, _Thread*);
17 static void delthread(_Threadlist*, _Thread*);
18 static void addthreadinproc(Proc*, _Thread*);
19 static void delthreadinproc(Proc*, _Thread*);
20 static void contextswitch(Context *from, Context *to);
21 static void scheduler(Proc*);
24 _threaddebug(char *fmt, ...)
33 vsnprint(buf, sizeof buf, fmt, arg);
37 fprint(2, "%d.%d: %s\n", getpid(), t->id, buf);
39 fprint(2, "%d._: %s\n", getpid(), buf);
45 return proc()->thread;
47 _Thread *(*threadnow)(void) = getthreadnow;
54 p = malloc(sizeof *p);
56 sysfatal("procalloc malloc: %r");
57 memset(p, 0, sizeof *p);
59 lock(&threadnproclock);
61 unlock(&threadnproclock);
71 t->startfn(t->startarg);
72 memset(&v, 0xff, 32); /* try to cut off stack traces */
77 threadalloc(void (*fn)(void*), void *arg, uint stack)
82 /* allocate the task and stack together */
83 t = malloc(sizeof *t+stack);
85 sysfatal("threadalloc malloc: %r");
86 memset(t, 0, sizeof *t);
87 t->stk = (uchar*)(t+1);
89 t->id = incref(&threadidref);
93 /* do a reasonable initialization */
94 memset(&t->context.uc, 0, sizeof t->context.uc);
96 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
98 /* on Linux makecontext neglects floating point */
99 getcontext(&t->context.uc);
101 /* call makecontext to do the real work. */
102 /* leave a few words open on both ends */
103 t->context.uc.uc_stack.ss_sp = t->stk+8;
104 t->context.uc.uc_stack.ss_size = t->stksize-64;
105 makecontext(&t->context.uc, (void(*)())threadstart, 1, t);
111 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
115 t = threadalloc(fn, arg, stack);
117 addthreadinproc(p, t);
124 threadcreate(void (*fn)(void*), void *arg, uint stack)
128 t = _threadcreate(proc(), fn, arg, stack);
133 proccreate(void (*fn)(void*), void *arg, uint stack)
139 t = _threadcreate(p, fn, arg, stack);
140 _procstart(p, scheduler);
150 contextswitch(&p->thread->context, &p->schedcontext);
154 _threadready(_Thread *t)
160 addthread(&p->runqueue, t);
161 //print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid);
163 _procwakeup(&p->runrend);
175 _threadready(p->thread);
177 return p->nswitch - n;
181 threadexits(char *msg)
188 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
189 proc()->thread->exiting = 1;
194 contextswitch(Context *from, Context *to)
196 if(swapcontext(&from->uc, &to->uc) < 0){
197 fprint(2, "swapcontext failed: %r\n");
208 _threaddebug("scheduler enter");
209 // print("s %p %d\n", p, gettid());
212 while((t = p->runqueue.head) == nil){
215 p->runrend.l = &p->lock;
216 _threaddebug("scheduler sleep");
217 _procsleep(&p->runrend);
218 _threaddebug("scheduler wake");
220 delthread(&p->runqueue, t);
224 _threaddebug("run %d (%s)", t->id, t->name);
225 contextswitch(&p->schedcontext, &t->context);
229 delthreadinproc(p, t);
236 _threaddebug("scheduler exit");
238 lock(&threadnproclock);
241 if(--threadnproc == threadnsysproc)
242 threadexitsall(p->msg);
243 unlock(&threadnproclock);
250 _threadsetsysproc(void)
252 lock(&threadnproclock);
253 if(++threadnsysproc == threadnproc)
255 unlock(&threadnproclock);
262 return &proc()->udata;
265 extern Jmp *(*_notejmpbuf)(void);
269 return &proc()->sigjmp;
276 threadsetname(char *fmt, ...)
283 vsnprint(t->name, sizeof t->name, fmt, arg);
288 threadsetstate(char *fmt, ...)
295 vsnprint(t->state, sizeof t->name, fmt, arg);
303 threadqlock(QLock *l, int block, ulong pc)
307 l->owner = (*threadnow)();
308 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
316 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
317 addthread(&l->waiting, (*threadnow)());
322 if(l->owner != (*threadnow)()){
323 fprint(2, "qlock pc=0x%lux owner=%p self=%p oops\n", pc, l->owner, (*threadnow)());
326 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
331 threadqunlock(QLock *l, ulong pc)
334 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
336 fprint(2, "qunlock pc=0x%lux owner=%p self=%p oops\n",
337 pc, l->owner, (*threadnow)());
340 if((l->owner = l->waiting.head) != nil){
341 delthread(&l->waiting, l->owner);
342 _threadready(l->owner);
348 threadrlock(RWLock *l, int block, ulong pc)
353 if(l->writer == nil && l->wwaiting.head == nil){
362 addthread(&l->rwaiting, (*threadnow)());
369 threadwlock(RWLock *l, int block, ulong pc)
374 if(l->writer == nil && l->readers == 0){
375 l->writer = (*threadnow)();
383 addthread(&l->wwaiting, (*threadnow)());
390 threadrunlock(RWLock *l, ulong pc)
397 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
398 delthread(&l->wwaiting, t);
406 threadwunlock(RWLock *l, ulong pc)
413 assert(l->readers == 0);
414 while((t = l->rwaiting.head) != nil){
415 delthread(&l->rwaiting, t);
419 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
420 delthread(&l->wwaiting, t);
431 threadrsleep(Rendez *r, ulong pc)
433 addthread(&r->waiting, proc()->thread);
440 threadrwakeup(Rendez *r, int all, ulong pc)
448 if((t = r->waiting.head) == nil)
450 delthread(&r->waiting, t);
460 static int threadargc;
461 static char **threadargv;
465 threadmainstart(void *v)
468 threadmainproc = proc();
469 threadmain(threadargc, threadargv);
473 threadlinklibrary(void)
478 main(int argc, char **argv)
482 _threadsetupdaemonize();
488 * Install locking routines into C library.
491 _unlock = _threadunlock;
492 _qlock = threadqlock;
493 _qunlock = threadqunlock;
494 _rlock = threadrlock;
495 _runlock = threadrunlock;
496 _wlock = threadwlock;
497 _wunlock = threadwunlock;
498 _rsleep = threadrsleep;
499 _rwakeup = threadrwakeup;
500 _notejmpbuf = threadnotejmp;
505 if(mainstacksize == 0)
506 mainstacksize = 65536;
507 _threadcreate(p, threadmainstart, nil, mainstacksize);
509 return 0; /* not reached */
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);