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*);
26 return proc()->thread;
28 _Thread *(*threadnow)(void) = getthreadnow;
35 p = malloc(sizeof *p);
37 sysfatal("procalloc malloc: %r");
38 memset(p, 0, sizeof *p);
40 lock(&threadnproclock);
42 unlock(&threadnproclock);
52 t->startfn(t->startarg);
57 threadalloc(void (*fn)(void*), void *arg, uint stack)
62 /* allocate the task and stack together */
63 t = malloc(sizeof *t+stack);
65 sysfatal("threadalloc malloc: %r");
66 memset(t, 0, sizeof *t);
67 t->stk = (uchar*)(t+1);
69 t->id = incref(&threadidref);
73 /* do a reasonable initialization */
74 memset(&t->context.uc, 0, sizeof t->context.uc);
76 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
78 /* on Linux makecontext neglects floating point */
79 getcontext(&t->context.uc);
81 /* call makecontext to do the real work. */
82 /* leave a few words open on both ends */
83 t->context.uc.uc_stack.ss_sp = t->stk+8;
84 t->context.uc.uc_stack.ss_size = t->stksize-16;
85 makecontext(&t->context.uc, (void(*)())threadstart, 1, t);
91 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
95 t = threadalloc(fn, arg, stack);
97 addthreadinproc(p, t);
104 threadcreate(void (*fn)(void*), void *arg, uint stack)
108 t = _threadcreate(proc(), fn, arg, stack);
113 proccreate(void (*fn)(void*), void *arg, uint stack)
119 t = _threadcreate(p, fn, arg, stack);
120 _procstart(p, scheduler);
130 contextswitch(&p->thread->context, &p->schedcontext);
134 _threadready(_Thread *t)
140 addthread(&p->runqueue, t);
141 _procwakeup(&p->runrend);
148 _threadready(proc()->thread);
153 threadexits(char *msg)
160 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
161 proc()->thread->exiting = 1;
166 contextswitch(Context *from, Context *to)
168 if(swapcontext(&from->uc, &to->uc) < 0){
169 fprint(2, "swapcontext failed: %r\n");
180 // print("s %p %d\n", p, gettid());
183 while((t = p->runqueue.head) == nil){
186 p->runrend.l = &p->lock;
187 _procsleep(&p->runrend);
189 delthread(&p->runqueue, t);
192 // print("run %s %d\n", t->name, t->id);
193 contextswitch(&p->schedcontext, &t->context);
197 delthreadinproc(p, t);
205 lock(&threadnproclock);
208 if(--threadnproc == threadnsysproc)
209 threadexitsall(p->msg);
210 unlock(&threadnproclock);
217 _threadsetsysproc(void)
219 lock(&threadnproclock);
220 if(++threadnsysproc == threadnproc)
222 unlock(&threadnproclock);
229 return &proc()->udata;
232 extern Jmp *(*_notejmpbuf)(void);
236 return &proc()->sigjmp;
243 threadsetname(char *fmt, ...)
250 vsnprint(t->name, sizeof t->name, fmt, arg);
255 threadsetstate(char *fmt, ...)
262 vsnprint(t->state, sizeof t->name, fmt, arg);
270 threadqlock(QLock *l, int block, ulong pc)
274 l->owner = (*threadnow)();
275 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
283 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
284 addthread(&l->waiting, (*threadnow)());
289 if(l->owner != (*threadnow)()){
290 fprint(2, "qlock pc=0x%lux owner=%p self=%p oops\n", pc, l->owner, (*threadnow)());
293 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
298 threadqunlock(QLock *l, ulong pc)
301 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
303 fprint(2, "qunlock pc=0x%lux owner=%p self=%p oops\n",
304 pc, l->owner, (*threadnow)());
307 if((l->owner = l->waiting.head) != nil){
308 delthread(&l->waiting, l->owner);
309 _threadready(l->owner);
315 threadrlock(RWLock *l, int block, ulong pc)
320 if(l->writer == nil && l->wwaiting.head == nil){
329 addthread(&l->rwaiting, (*threadnow)());
336 threadwlock(RWLock *l, int block, ulong pc)
341 if(l->writer == nil && l->readers == 0){
342 l->writer = (*threadnow)();
350 addthread(&l->wwaiting, (*threadnow)());
357 threadrunlock(RWLock *l, ulong pc)
364 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
365 delthread(&l->wwaiting, t);
373 threadwunlock(RWLock *l, ulong pc)
380 assert(l->readers == 0);
381 while((t = l->rwaiting.head) != nil){
382 delthread(&l->rwaiting, t);
386 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
387 delthread(&l->wwaiting, t);
398 threadrsleep(Rendez *r, ulong pc)
400 addthread(&r->waiting, proc()->thread);
407 threadrwakeup(Rendez *r, int all, ulong pc)
415 if((t = r->waiting.head) == nil)
417 delthread(&r->waiting, t);
427 static int threadargc;
428 static char **threadargv;
432 threadmainstart(void *v)
435 threadmainproc = proc();
436 threadmain(threadargc, threadargv);
440 main(int argc, char **argv)
448 * Install locking routines into C library.
451 _unlock = _threadunlock;
452 _qlock = threadqlock;
453 _qunlock = threadqunlock;
454 _rlock = threadrlock;
455 _runlock = threadrunlock;
456 _wlock = threadwlock;
457 _wunlock = threadwunlock;
458 _rsleep = threadrsleep;
459 _rwakeup = threadrwakeup;
460 _notejmpbuf = threadnotejmp;
465 if(mainstacksize == 0)
466 mainstacksize = 65536;
467 _threadcreate(p, threadmainstart, nil, mainstacksize);
469 return 0; /* not reached */
473 * hooray for linked lists
476 addthread(_Threadlist *l, _Thread *t)
490 delthread(_Threadlist *l, _Thread *t)
493 t->prev->next = t->next;
497 t->next->prev = t->prev;
503 addthreadinproc(Proc *p, _Thread *t)
509 l->tail->allnext = t;
510 t->allprev = l->tail;
520 delthreadinproc(Proc *p, _Thread *t)
526 t->allprev->allnext = t->allnext;
528 l->head = t->allnext;
530 t->allnext->allprev = t->allprev;
532 l->tail = t->allprev;
536 Lock _threadprocslock;
537 static Proc *_threadprocstail;
542 lock(&_threadprocslock);
543 if(_threadprocstail){
544 _threadprocstail->next = p;
545 p->prev = _threadprocstail;
550 _threadprocstail = p;
552 unlock(&_threadprocslock);
558 lock(&_threadprocslock);
560 p->prev->next = p->next;
562 _threadprocs = p->next;
564 p->next->prev = p->prev;
566 _threadprocstail = p->prev;
567 unlock(&_threadprocslock);