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, ...)
31 p = strrchr(argv0, '/');
36 snprint(buf, sizeof buf, "/tmp/%s.tlog", p);
37 if((fd = create(buf, OWRITE, 0666)) < 0)
38 fd = open("/dev/null", OWRITE);
42 vsnprint(buf, sizeof buf, fmt, arg);
46 fprint(fd, "%d.%d: %s\n", getpid(), t->id, buf);
48 fprint(fd, "%d._: %s\n", getpid(), buf);
54 return proc()->thread;
56 _Thread *(*threadnow)(void) = getthreadnow;
63 p = malloc(sizeof *p);
65 sysfatal("procalloc malloc: %r");
66 memset(p, 0, sizeof *p);
68 lock(&threadnproclock);
70 unlock(&threadnproclock);
80 t->startfn(t->startarg);
81 memset(&v, 0xff, 32); /* try to cut off stack traces */
86 threadalloc(void (*fn)(void*), void *arg, uint stack)
91 /* allocate the task and stack together */
92 t = malloc(sizeof *t+stack);
94 sysfatal("threadalloc malloc: %r");
95 memset(t, 0, sizeof *t);
96 t->stk = (uchar*)(t+1);
98 t->id = incref(&threadidref);
102 /* do a reasonable initialization */
103 memset(&t->context.uc, 0, sizeof t->context.uc);
105 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
107 /* must initialize with current context */
108 getcontext(&t->context.uc);
110 /* call makecontext to do the real work. */
111 /* leave a few words open on both ends */
112 t->context.uc.uc_stack.ss_sp = t->stk+8;
113 t->context.uc.uc_stack.ss_size = t->stksize-64;
114 makecontext(&t->context.uc, (void(*)())threadstart, 1, t);
120 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
124 t = threadalloc(fn, arg, stack);
126 addthreadinproc(p, t);
133 threadcreate(void (*fn)(void*), void *arg, uint stack)
137 t = _threadcreate(proc(), fn, arg, stack);
142 proccreate(void (*fn)(void*), void *arg, uint stack)
148 t = _threadcreate(p, fn, arg, stack);
149 _procstart(p, scheduler);
159 contextswitch(&p->thread->context, &p->schedcontext);
163 _threadready(_Thread *t)
169 p->runrend.l = &p->lock;
170 addthread(&p->runqueue, t);
171 //print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid);
173 _procwakeupandunlock(&p->runrend);
186 _threadready(p->thread);
188 return p->nswitch - n;
192 threadexits(char *msg)
199 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
200 proc()->thread->exiting = 1;
205 contextswitch(Context *from, Context *to)
207 if(swapcontext(&from->uc, &to->uc) < 0){
208 fprint(2, "swapcontext failed: %r\n");
219 _threaddebug("scheduler enter");
220 // print("s %p %d\n", p, gettid());
223 while((t = p->runqueue.head) == nil){
226 p->runrend.l = &p->lock;
227 _threaddebug("scheduler sleep");
228 _procsleep(&p->runrend);
229 _threaddebug("scheduler wake");
231 delthread(&p->runqueue, t);
235 _threaddebug("run %d (%s)", t->id, t->name);
236 contextswitch(&p->schedcontext, &t->context);
240 delthreadinproc(p, t);
247 _threaddebug("scheduler exit");
249 lock(&threadnproclock);
252 if(--threadnproc == threadnsysproc)
253 threadexitsall(p->msg);
254 unlock(&threadnproclock);
261 _threadsetsysproc(void)
263 lock(&threadnproclock);
264 if(++threadnsysproc == threadnproc)
266 unlock(&threadnproclock);
273 return &proc()->udata;
276 extern Jmp *(*_notejmpbuf)(void);
280 return &proc()->sigjmp;
287 threadsetname(char *fmt, ...)
294 vsnprint(t->name, sizeof t->name, fmt, arg);
299 threadsetstate(char *fmt, ...)
306 vsnprint(t->state, sizeof t->name, fmt, arg);
314 threadqlock(QLock *l, int block, ulong pc)
318 l->owner = (*threadnow)();
319 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
327 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
328 addthread(&l->waiting, (*threadnow)());
333 if(l->owner != (*threadnow)()){
334 fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
335 argv0, pc, l->owner, (*threadnow)());
338 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
343 threadqunlock(QLock *l, ulong pc)
346 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
347 if(l->owner != (*threadnow)()){
348 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
349 argv0, pc, l->owner, (*threadnow)());
351 if((l->owner = l->waiting.head) != nil){
352 delthread(&l->waiting, l->owner);
353 _threadready(l->owner);
359 threadrlock(RWLock *l, int block, ulong pc)
364 if(l->writer == nil && l->wwaiting.head == nil){
373 addthread(&l->rwaiting, (*threadnow)());
380 threadwlock(RWLock *l, int block, ulong pc)
385 if(l->writer == nil && l->readers == 0){
386 l->writer = (*threadnow)();
394 addthread(&l->wwaiting, (*threadnow)());
401 threadrunlock(RWLock *l, ulong pc)
408 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
409 delthread(&l->wwaiting, t);
417 threadwunlock(RWLock *l, ulong pc)
424 assert(l->readers == 0);
425 while((t = l->rwaiting.head) != nil){
426 delthread(&l->rwaiting, t);
430 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
431 delthread(&l->wwaiting, t);
442 threadrsleep(Rendez *r, ulong pc)
444 addthread(&r->waiting, proc()->thread);
451 threadrwakeup(Rendez *r, int all, ulong pc)
459 if((t = r->waiting.head) == nil)
461 delthread(&r->waiting, t);
471 static int threadargc;
472 static char **threadargv;
476 threadmainstart(void *v)
479 threadmainproc = proc();
480 threadmain(threadargc, threadargv);
484 threadlinklibrary(void)
489 main(int argc, char **argv)
495 _threadsetupdaemonize();
501 * Install locking routines into C library.
504 _unlock = _threadunlock;
505 _qlock = threadqlock;
506 _qunlock = threadqunlock;
507 _rlock = threadrlock;
508 _runlock = threadrunlock;
509 _wlock = threadwlock;
510 _wunlock = threadwunlock;
511 _rsleep = threadrsleep;
512 _rwakeup = threadrwakeup;
513 _notejmpbuf = threadnotejmp;
518 if(mainstacksize == 0)
519 mainstacksize = 65536;
520 _threadcreate(p, threadmainstart, nil, mainstacksize);
528 * hooray for linked lists
531 addthread(_Threadlist *l, _Thread *t)
545 delthread(_Threadlist *l, _Thread *t)
548 t->prev->next = t->next;
552 t->next->prev = t->prev;
558 addthreadinproc(Proc *p, _Thread *t)
564 l->tail->allnext = t;
565 t->allprev = l->tail;
575 delthreadinproc(Proc *p, _Thread *t)
581 t->allprev->allnext = t->allnext;
583 l->head = t->allnext;
585 t->allnext->allprev = t->allprev;
587 l->tail = t->allprev;
591 Lock _threadprocslock;
592 static Proc *_threadprocstail;
597 lock(&_threadprocslock);
598 if(_threadprocstail){
599 _threadprocstail->next = p;
600 p->prev = _threadprocstail;
605 _threadprocstail = p;
607 unlock(&_threadprocslock);
613 lock(&_threadprocslock);
615 p->prev->next = p->next;
617 _threadprocs = p->next;
619 p->next->prev = p->prev;
621 _threadprocstail = p->prev;
622 unlock(&_threadprocslock);
626 * notify - for now just use the usual mechanisms
629 threadnotify(int (*f)(void*, char*), int in)