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, ...)
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);
85 //print("threadstart %p\n", v);
86 t->startfn(t->startarg);
87 //print("threadexits %p\n", v);
89 //print("not reacehd\n");
93 threadalloc(void (*fn)(void*), void *arg, uint stack)
98 /* allocate the task and stack together */
99 t = malloc(sizeof *t+stack);
101 sysfatal("threadalloc malloc: %r");
102 memset(t, 0, sizeof *t);
103 t->stk = (uchar*)(t+1);
105 t->id = incref(&threadidref);
109 /* do a reasonable initialization */
110 memset(&t->context.uc, 0, sizeof t->context.uc);
112 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
114 /* must initialize with current context */
115 if(getcontext(&t->context.uc) < 0)
116 sysfatal("threadalloc getcontext: %r");
118 /* call makecontext to do the real work. */
119 /* leave a few words open on both ends */
120 t->context.uc.uc_stack.ss_sp = t->stk+8;
121 t->context.uc.uc_stack.ss_size = t->stksize-64;
122 #ifdef __sun__ /* sigh */
123 /* can avoid this with __MAKECONTEXT_V2_SOURCE but only on SunOS 5.9 */
124 t->context.uc_stack.ss_sp =
125 (char*)t->context.uc_stack.ss_sp
126 +t->context.uc_stack.ss_size;
128 makecontext(&t->context.uc, (void(*)())threadstart, 1, t);
134 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
138 t = threadalloc(fn, arg, stack);
140 addthreadinproc(p, t);
147 threadcreate(void (*fn)(void*), void *arg, uint stack)
151 t = _threadcreate(proc(), fn, arg, stack);
156 proccreate(void (*fn)(void*), void *arg, uint stack)
162 t = _threadcreate(p, fn, arg, stack);
163 _procstart(p, scheduler);
174 //print("threadswtch %p\n", p);
175 contextswitch(&p->thread->context, &p->schedcontext);
179 _threadready(_Thread *t)
185 p->runrend.l = &p->lock;
186 addthread(&p->runqueue, t);
187 //print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid);
189 _procwakeupandunlock(&p->runrend);
202 _threadready(p->thread);
204 return p->nswitch - n;
208 threadexits(char *msg)
215 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
216 proc()->thread->exiting = 1;
221 contextswitch(Context *from, Context *to)
223 if(swapcontext(&from->uc, &to->uc) < 0){
224 fprint(2, "swapcontext failed: %r\n");
235 _threaddebug("scheduler enter");
236 // print("s %p\n", p);
239 while((t = p->runqueue.head) == nil){
242 p->runrend.l = &p->lock;
243 _threaddebug("scheduler sleep");
244 _procsleep(&p->runrend);
245 _threaddebug("scheduler wake");
247 delthread(&p->runqueue, t);
251 _threaddebug("run %d (%s)", t->id, t->name);
252 contextswitch(&p->schedcontext, &t->context);
253 //print("back in scheduler\n");
257 delthreadinproc(p, t);
259 //print("ntrhead %d\n", p->nthread);
265 _threaddebug("scheduler exit");
267 lock(&threadnproclock);
270 if(--threadnproc == threadnsysproc)
271 threadexitsall(p->msg);
272 unlock(&threadnproclock);
279 _threadsetsysproc(void)
281 lock(&threadnproclock);
282 if(++threadnsysproc == threadnproc)
284 unlock(&threadnproclock);
291 return &proc()->udata;
294 extern Jmp *(*_notejmpbuf)(void);
298 return &proc()->sigjmp;
305 threadsetname(char *fmt, ...)
312 vsnprint(t->name, sizeof t->name, fmt, arg);
317 threadsetstate(char *fmt, ...)
324 vsnprint(t->state, sizeof t->name, fmt, arg);
335 if((char*)&t <= (char*)t->stk
336 || (char*)&t - (char*)t->stk < 256+n){
337 fprint(2, "thread stack overflow\n");
346 threadqlock(QLock *l, int block, ulong pc)
348 //print("threadqlock %p\n", l);
351 l->owner = (*threadnow)();
352 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
360 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
361 addthread(&l->waiting, (*threadnow)());
366 if(l->owner != (*threadnow)()){
367 fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
368 argv0, pc, l->owner, (*threadnow)());
371 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
376 threadqunlock(QLock *l, ulong pc)
379 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
381 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
382 argv0, pc, l->owner, (*threadnow)());
385 if((l->owner = l->waiting.head) != nil){
386 delthread(&l->waiting, l->owner);
387 _threadready(l->owner);
393 threadrlock(RWLock *l, int block, ulong pc)
398 if(l->writer == nil && l->wwaiting.head == nil){
407 addthread(&l->rwaiting, (*threadnow)());
414 threadwlock(RWLock *l, int block, ulong pc)
419 if(l->writer == nil && l->readers == 0){
420 l->writer = (*threadnow)();
428 addthread(&l->wwaiting, (*threadnow)());
435 threadrunlock(RWLock *l, ulong pc)
442 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
443 delthread(&l->wwaiting, t);
451 threadwunlock(RWLock *l, ulong pc)
458 assert(l->readers == 0);
459 while((t = l->rwaiting.head) != nil){
460 delthread(&l->rwaiting, t);
464 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
465 delthread(&l->wwaiting, t);
476 threadrsleep(Rendez *r, ulong pc)
478 addthread(&r->waiting, proc()->thread);
485 threadrwakeup(Rendez *r, int all, ulong pc)
493 if((t = r->waiting.head) == nil)
495 delthread(&r->waiting, t);
505 static int threadargc;
506 static char **threadargv;
510 threadmainstart(void *v)
513 threadmainproc = proc();
514 threadmain(threadargc, threadargv);
518 main(int argc, char **argv)
524 _threadsetupdaemonize();
530 * Install locking routines into C library.
533 _unlock = _threadunlock;
534 _qlock = threadqlock;
535 _qunlock = threadqunlock;
536 _rlock = threadrlock;
537 _runlock = threadrunlock;
538 _wlock = threadwlock;
539 _wunlock = threadwunlock;
540 _rsleep = threadrsleep;
541 _rwakeup = threadrwakeup;
542 _notejmpbuf = threadnotejmp;
547 if(mainstacksize == 0)
548 mainstacksize = 65536;
549 _threadcreate(p, threadmainstart, nil, mainstacksize);
557 * hooray for linked lists
560 addthread(_Threadlist *l, _Thread *t)
574 delthread(_Threadlist *l, _Thread *t)
577 t->prev->next = t->next;
581 t->next->prev = t->prev;
587 addthreadinproc(Proc *p, _Thread *t)
593 l->tail->allnext = t;
594 t->allprev = l->tail;
604 delthreadinproc(Proc *p, _Thread *t)
610 t->allprev->allnext = t->allnext;
612 l->head = t->allnext;
614 t->allnext->allprev = t->allprev;
616 l->tail = t->allprev;
620 Lock _threadprocslock;
621 static Proc *_threadprocstail;
626 lock(&_threadprocslock);
627 if(_threadprocstail){
628 _threadprocstail->next = p;
629 p->prev = _threadprocstail;
634 _threadprocstail = p;
636 unlock(&_threadprocslock);
642 lock(&_threadprocslock);
644 p->prev->next = p->next;
646 _threadprocs = p->next;
648 p->next->prev = p->prev;
650 _threadprocstail = p->prev;
651 unlock(&_threadprocslock);
655 * notify - for now just use the usual mechanisms
658 threadnotify(int (*f)(void*, char*), int in)