Blob


1 #include "threadimpl.h"
3 int _threaddebuglevel;
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 procscheduler(Proc*);
19 static int threadinfo(void*, char*);
21 static void
22 _threaddebug(char *fmt, ...)
23 {
24 va_list arg;
25 char buf[128];
26 _Thread *t;
27 char *p;
28 static int fd = -1;
30 if(_threaddebuglevel == 0)
31 return;
33 if(fd < 0){
34 p = strrchr(argv0, '/');
35 if(p)
36 p++;
37 else
38 p = argv0;
39 snprint(buf, sizeof buf, "/tmp/%s.tlog", p);
40 if((fd = create(buf, OWRITE, 0666)) < 0)
41 fd = open("/dev/null", OWRITE);
42 }
44 va_start(arg, fmt);
45 vsnprint(buf, sizeof buf, fmt, arg);
46 va_end(arg);
47 t = proc()->thread;
48 if(t)
49 fprint(fd, "%d.%d: %s\n", getpid(), t->id, buf);
50 else
51 fprint(fd, "%d._: %s\n", getpid(), buf);
52 }
54 static _Thread*
55 getthreadnow(void)
56 {
57 return proc()->thread;
58 }
59 _Thread *(*threadnow)(void) = getthreadnow;
61 static Proc*
62 procalloc(void)
63 {
64 Proc *p;
66 p = malloc(sizeof *p);
67 if(p == nil)
68 sysfatal("procalloc malloc: %r");
69 memset(p, 0, sizeof *p);
70 addproc(p);
71 lock(&threadnproclock);
72 threadnproc++;
73 unlock(&threadnproclock);
74 return p;
75 }
77 static void
78 threadstart(uint y, uint x)
79 {
80 _Thread *t;
81 ulong z;
83 z = x<<16; /* hide undefined 32-bit shift from 32-bit compilers */
84 z <<= 16;
85 z |= y;
86 t = (_Thread*)z;
88 //print("threadstart %p\n", v);
89 t->startfn(t->startarg);
90 //print("threadexits %p\n", v);
91 threadexits(nil);
92 //print("not reacehd\n");
93 }
95 static _Thread*
96 threadalloc(void (*fn)(void*), void *arg, uint stack)
97 {
98 _Thread *t;
99 sigset_t zero;
100 uint x, y;
101 ulong z;
103 /* allocate the task and stack together */
104 t = malloc(sizeof *t+stack);
105 if(t == nil)
106 sysfatal("threadalloc malloc: %r");
107 memset(t, 0, sizeof *t);
108 t->stk = (uchar*)(t+1);
109 t->stksize = stack;
110 t->id = incref(&threadidref);
111 t->startfn = fn;
112 t->startarg = arg;
114 /* do a reasonable initialization */
115 memset(&t->context.uc, 0, sizeof t->context.uc);
116 sigemptyset(&zero);
117 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
119 /* must initialize with current context */
120 if(getcontext(&t->context.uc) < 0)
121 sysfatal("threadalloc getcontext: %r");
123 /* call makecontext to do the real work. */
124 /* leave a few words open on both ends */
125 t->context.uc.uc_stack.ss_sp = t->stk+8;
126 t->context.uc.uc_stack.ss_size = t->stksize-64;
127 #if defined(__sun__) && !defined(__MAKECONTEXT_V2_SOURCE) /* sigh */
128 /* can avoid this with __MAKECONTEXT_V2_SOURCE but only on SunOS 5.9 */
129 t->context.uc.uc_stack.ss_sp =
130 (char*)t->context.uc.uc_stack.ss_sp
131 +t->context.uc.uc_stack.ss_size;
132 #endif
133 /*
134 * All this magic is because you have to pass makecontext a
135 * function that takes some number of word-sized variables,
136 * and on 64-bit machines pointers are bigger than words.
137 */
138 z = (ulong)t;
139 y = z;
140 z >>= 16; /* hide undefined 32-bit shift from 32-bit compilers */
141 x = z>>16;
142 makecontext(&t->context.uc, (void(*)(void))threadstart, 2, y, x);
144 return t;
147 _Thread*
148 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
150 _Thread *t;
152 t = threadalloc(fn, arg, stack);
153 t->proc = p;
154 addthreadinproc(p, t);
155 p->nthread++;
156 _threadready(t);
157 return t;
160 int
161 threadcreate(void (*fn)(void*), void *arg, uint stack)
163 _Thread *t;
165 t = _threadcreate(proc(), fn, arg, stack);
166 return t->id;
169 int
170 proccreate(void (*fn)(void*), void *arg, uint stack)
172 int id;
173 _Thread *t;
174 Proc *p;
176 p = procalloc();
177 t = _threadcreate(p, fn, arg, stack);
178 id = t->id; /* t might be freed after _procstart */
179 _procstart(p, procscheduler);
180 return id;
183 void
184 _threadswitch(void)
186 Proc *p;
188 needstack(0);
189 p = proc();
190 //print("threadswtch %p\n", p);
191 contextswitch(&p->thread->context, &p->schedcontext);
194 void
195 _threadready(_Thread *t)
197 Proc *p;
199 p = t->proc;
200 lock(&p->lock);
201 p->runrend.l = &p->lock;
202 addthread(&p->runqueue, t);
203 //print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid);
204 if(p != proc())
205 _procwakeupandunlock(&p->runrend);
206 else
207 unlock(&p->lock);
210 int
211 threadyield(void)
213 int n;
214 Proc *p;
216 p = proc();
217 n = p->nswitch;
218 _threadready(p->thread);
219 _threadswitch();
220 return p->nswitch - n;
223 void
224 threadexits(char *msg)
226 Proc *p;
228 p = proc();
229 if(msg == nil)
230 msg = "";
231 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
232 proc()->thread->exiting = 1;
233 _threadswitch();
236 static void
237 contextswitch(Context *from, Context *to)
239 if(swapcontext(&from->uc, &to->uc) < 0){
240 fprint(2, "swapcontext failed: %r\n");
241 assert(0);
245 static void
246 procscheduler(Proc *p)
248 _Thread *t;
250 setproc(p);
251 _threaddebug("scheduler enter");
252 // print("s %p\n", p);
253 lock(&p->lock);
254 for(;;){
255 while((t = p->runqueue.head) == nil){
256 if(p->nthread == 0)
257 goto Out;
258 p->runrend.l = &p->lock;
259 _threaddebug("scheduler sleep");
260 _procsleep(&p->runrend);
261 _threaddebug("scheduler wake");
263 delthread(&p->runqueue, t);
264 unlock(&p->lock);
265 p->thread = t;
266 p->nswitch++;
267 _threaddebug("run %d (%s)", t->id, t->name);
268 contextswitch(&p->schedcontext, &t->context);
269 //print("back in scheduler\n");
270 p->thread = nil;
271 lock(&p->lock);
272 if(t->exiting){
273 delthreadinproc(p, t);
274 p->nthread--;
275 //print("ntrhead %d\n", p->nthread);
276 free(t);
280 Out:
281 _threaddebug("scheduler exit");
282 if(p->mainproc){
283 /*
284 * Stupid bug - on Linux 2.6 and maybe elsewhere,
285 * if the main thread exits then the others keep running
286 * but the process shows up as a zombie in ps and is not
287 * attachable with ptrace. We'll just sit around pretending
288 * to be a system proc instead of exiting.
289 */
290 _threaddaemonize();
291 lock(&threadnproclock);
292 if(++threadnsysproc == threadnproc)
293 threadexitsall(p->msg);
294 p->sysproc = 1;
295 unlock(&threadnproclock);
296 for(;;)
297 sleep(1000);
300 delproc(p);
301 lock(&threadnproclock);
302 if(p->sysproc)
303 --threadnsysproc;
304 if(--threadnproc == threadnsysproc)
305 threadexitsall(p->msg);
306 unlock(&threadnproclock);
307 unlock(&p->lock);
308 free(p);
311 void
312 _threadsetsysproc(void)
314 lock(&threadnproclock);
315 if(++threadnsysproc == threadnproc)
316 threadexitsall(nil);
317 unlock(&threadnproclock);
318 proc()->sysproc = 1;
321 void**
322 procdata(void)
324 return &proc()->udata;
327 void**
328 threaddata(void)
330 return &proc()->thread->udata;
333 extern Jmp *(*_notejmpbuf)(void);
334 static Jmp*
335 threadnotejmp(void)
337 return &proc()->sigjmp;
340 /*
341 * debugging
342 */
343 void
344 threadsetname(char *fmt, ...)
346 va_list arg;
347 _Thread *t;
349 t = proc()->thread;
350 va_start(arg, fmt);
351 vsnprint(t->name, sizeof t->name, fmt, arg);
352 va_end(arg);
355 char*
356 threadgetname(void)
358 return proc()->thread->name;
361 void
362 threadsetstate(char *fmt, ...)
364 va_list arg;
365 _Thread *t;
367 t = proc()->thread;
368 va_start(arg, fmt);
369 vsnprint(t->state, sizeof t->name, fmt, arg);
370 va_end(arg);
373 void
374 needstack(int n)
376 _Thread *t;
378 t = proc()->thread;
380 if((char*)&t <= (char*)t->stk
381 || (char*)&t - (char*)t->stk < 256+n){
382 fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
383 abort();
387 /*
388 * locking
389 */
390 static int
391 threadqlock(QLock *l, int block, ulong pc)
393 //print("threadqlock %p\n", l);
394 lock(&l->l);
395 if(l->owner == nil){
396 l->owner = (*threadnow)();
397 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
398 unlock(&l->l);
399 return 1;
401 if(!block){
402 unlock(&l->l);
403 return 0;
405 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
406 addthread(&l->waiting, (*threadnow)());
407 unlock(&l->l);
409 _threadswitch();
411 if(l->owner != (*threadnow)()){
412 fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
413 argv0, pc, l->owner, (*threadnow)());
414 abort();
416 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
417 return 1;
420 static void
421 threadqunlock(QLock *l, ulong pc)
423 _Thread *ready;
425 lock(&l->l);
426 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
427 if(l->owner == 0){
428 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
429 argv0, pc, l->owner, (*threadnow)());
430 abort();
432 if((l->owner = ready = l->waiting.head) != nil)
433 delthread(&l->waiting, l->owner);
434 /*
435 * N.B. Cannot call _threadready() before unlocking l->l,
436 * because the thread we are readying might:
437 * - be in another proc
438 * - start running immediately
439 * - and free l before we get a chance to run again
440 */
441 unlock(&l->l);
442 if(ready)
443 _threadready(l->owner);
446 static int
447 threadrlock(RWLock *l, int block, ulong pc)
449 USED(pc);
451 lock(&l->l);
452 if(l->writer == nil && l->wwaiting.head == nil){
453 l->readers++;
454 unlock(&l->l);
455 return 1;
457 if(!block){
458 unlock(&l->l);
459 return 0;
461 addthread(&l->rwaiting, (*threadnow)());
462 unlock(&l->l);
463 _threadswitch();
464 return 1;
467 static int
468 threadwlock(RWLock *l, int block, ulong pc)
470 USED(pc);
472 lock(&l->l);
473 if(l->writer == nil && l->readers == 0){
474 l->writer = (*threadnow)();
475 unlock(&l->l);
476 return 1;
478 if(!block){
479 unlock(&l->l);
480 return 0;
482 addthread(&l->wwaiting, (*threadnow)());
483 unlock(&l->l);
484 _threadswitch();
485 return 1;
488 static void
489 threadrunlock(RWLock *l, ulong pc)
491 _Thread *t;
493 USED(pc);
494 t = nil;
495 lock(&l->l);
496 --l->readers;
497 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
498 delthread(&l->wwaiting, t);
499 l->writer = t;
501 unlock(&l->l);
502 if(t)
503 _threadready(t);
507 static void
508 threadwunlock(RWLock *l, ulong pc)
510 _Thread *t;
512 USED(pc);
513 lock(&l->l);
514 l->writer = nil;
515 assert(l->readers == 0);
516 while((t = l->rwaiting.head) != nil){
517 delthread(&l->rwaiting, t);
518 l->readers++;
519 _threadready(t);
521 t = nil;
522 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
523 delthread(&l->wwaiting, t);
524 l->writer = t;
526 unlock(&l->l);
527 if(t)
528 _threadready(t);
531 /*
532 * sleep and wakeup
533 */
534 static void
535 threadrsleep(Rendez *r, ulong pc)
537 addthread(&r->waiting, proc()->thread);
538 qunlock(r->l);
539 _threadswitch();
540 qlock(r->l);
543 static int
544 threadrwakeup(Rendez *r, int all, ulong pc)
546 int i;
547 _Thread *t;
549 for(i=0;; i++){
550 if(i==1 && !all)
551 break;
552 if((t = r->waiting.head) == nil)
553 break;
554 delthread(&r->waiting, t);
555 _threadready(t);
557 return i;
560 /*
561 * startup
562 */
564 static int threadargc;
565 static char **threadargv;
566 int mainstacksize;
568 static void
569 threadmainstart(void *v)
571 USED(v);
573 /*
574 * N.B. This call to proc() is a program's first call (indirectly) to a
575 * pthreads function while executing on a non-pthreads-allocated
576 * stack. If the pthreads implementation is using the stack pointer
577 * to locate the per-thread data, then this call will blow up.
578 * This means the pthread implementation is not suitable for
579 * running under libthread. Time to write your own. Sorry.
580 */
581 threadmainproc = proc();
582 threadmain(threadargc, threadargv);
585 int
586 main(int argc, char **argv)
588 Proc *p;
590 argv0 = argv[0];
592 _threadsetupdaemonize();
594 threadargc = argc;
595 threadargv = argv;
597 /*
598 * Install locking routines into C library.
599 */
600 _lock = _threadlock;
601 _unlock = _threadunlock;
602 _qlock = threadqlock;
603 _qunlock = threadqunlock;
604 _rlock = threadrlock;
605 _runlock = threadrunlock;
606 _wlock = threadwlock;
607 _wunlock = threadwunlock;
608 _rsleep = threadrsleep;
609 _rwakeup = threadrwakeup;
610 _notejmpbuf = threadnotejmp;
612 _pthreadinit();
613 p = procalloc();
614 p->mainproc = 1;
615 _threadsetproc(p);
616 if(mainstacksize == 0)
617 mainstacksize = 256*1024;
618 atnotify(threadinfo, 1);
619 _threadcreate(p, threadmainstart, nil, mainstacksize);
620 procscheduler(p);
621 sysfatal("procscheduler returned in threadmain!");
622 /* does not return */
623 return 0;
626 /*
627 * hooray for linked lists
628 */
629 static void
630 addthread(_Threadlist *l, _Thread *t)
632 if(l->tail){
633 l->tail->next = t;
634 t->prev = l->tail;
635 }else{
636 l->head = t;
637 t->prev = nil;
639 l->tail = t;
640 t->next = nil;
643 static void
644 delthread(_Threadlist *l, _Thread *t)
646 if(t->prev)
647 t->prev->next = t->next;
648 else
649 l->head = t->next;
650 if(t->next)
651 t->next->prev = t->prev;
652 else
653 l->tail = t->prev;
656 static void
657 addthreadinproc(Proc *p, _Thread *t)
659 _Threadlist *l;
661 l = &p->allthreads;
662 if(l->tail){
663 l->tail->allnext = t;
664 t->allprev = l->tail;
665 }else{
666 l->head = t;
667 t->allprev = nil;
669 l->tail = t;
670 t->allnext = nil;
673 static void
674 delthreadinproc(Proc *p, _Thread *t)
676 _Threadlist *l;
678 l = &p->allthreads;
679 if(t->allprev)
680 t->allprev->allnext = t->allnext;
681 else
682 l->head = t->allnext;
683 if(t->allnext)
684 t->allnext->allprev = t->allprev;
685 else
686 l->tail = t->allprev;
689 Proc *_threadprocs;
690 Lock _threadprocslock;
691 static Proc *_threadprocstail;
693 static void
694 addproc(Proc *p)
696 lock(&_threadprocslock);
697 if(_threadprocstail){
698 _threadprocstail->next = p;
699 p->prev = _threadprocstail;
700 }else{
701 _threadprocs = p;
702 p->prev = nil;
704 _threadprocstail = p;
705 p->next = nil;
706 unlock(&_threadprocslock);
709 static void
710 delproc(Proc *p)
712 lock(&_threadprocslock);
713 if(p->prev)
714 p->prev->next = p->next;
715 else
716 _threadprocs = p->next;
717 if(p->next)
718 p->next->prev = p->prev;
719 else
720 _threadprocstail = p->prev;
721 unlock(&_threadprocslock);
724 /*
725 * notify - for now just use the usual mechanisms
726 */
727 void
728 threadnotify(int (*f)(void*, char*), int in)
730 atnotify(f, in);
733 static int
734 onrunqueue(Proc *p, _Thread *t)
736 _Thread *tt;
738 for(tt=p->runqueue.head; tt; tt=tt->next)
739 if(tt == t)
740 return 1;
741 return 0;
744 /*
745 * print state - called from SIGINFO
746 */
747 static int
748 threadinfo(void *v, char *s)
750 Proc *p;
751 _Thread *t;
753 if(strcmp(s, "quit") != 0 && strcmp(s, "sys: status request") != 0)
754 return 0;
756 for(p=_threadprocs; p; p=p->next){
757 fprint(2, "proc %p %s%s\n", (void*)p->osprocid, p->msg,
758 p->sysproc ? " (sysproc)": "");
759 for(t=p->allthreads.head; t; t=t->allnext){
760 fprint(2, "\tthread %d %s: %s %s\n",
761 t->id,
762 t == p->thread ? "Running" :
763 onrunqueue(p, t) ? "Ready" : "Sleeping",
764 t->state, t->name);
767 return 1;