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*);
20 static void
21 _threaddebug(char *fmt, ...)
22 {
23 va_list arg;
24 char buf[128];
25 _Thread *t;
26 char *p;
27 static int fd = -1;
29 return;
30 va_start(arg, fmt);
31 vfprint(1, fmt, arg);
32 va_end(arg);
33 return;
35 if(fd < 0){
36 p = strrchr(argv0, '/');
37 if(p)
38 p++;
39 else
40 p = argv0;
41 snprint(buf, sizeof buf, "/tmp/%s.tlog", p);
42 if((fd = create(buf, OWRITE, 0666)) < 0)
43 fd = open("/dev/null", OWRITE);
44 }
46 va_start(arg, fmt);
47 vsnprint(buf, sizeof buf, fmt, arg);
48 va_end(arg);
49 t = proc()->thread;
50 if(t)
51 fprint(fd, "%d.%d: %s\n", getpid(), t->id, buf);
52 else
53 fprint(fd, "%d._: %s\n", getpid(), buf);
54 }
56 static _Thread*
57 getthreadnow(void)
58 {
59 return proc()->thread;
60 }
61 _Thread *(*threadnow)(void) = getthreadnow;
63 static Proc*
64 procalloc(void)
65 {
66 Proc *p;
68 p = malloc(sizeof *p);
69 if(p == nil)
70 sysfatal("procalloc malloc: %r");
71 memset(p, 0, sizeof *p);
72 addproc(p);
73 lock(&threadnproclock);
74 threadnproc++;
75 unlock(&threadnproclock);
76 return p;
77 }
79 static void
80 threadstart(uint y, uint x)
81 {
82 _Thread *t;
83 ulong z;
85 z = x<<16; /* hide undefined 32-bit shift from 32-bit compilers */
86 z <<= 16;
87 z |= y;
88 t = (_Thread*)z;
90 //print("threadstart %p\n", v);
91 t->startfn(t->startarg);
92 //print("threadexits %p\n", v);
93 threadexits(nil);
94 //print("not reacehd\n");
95 }
97 static _Thread*
98 threadalloc(void (*fn)(void*), void *arg, uint stack)
99 {
100 _Thread *t;
101 sigset_t zero;
102 uint x, y;
103 ulong z;
105 /* allocate the task and stack together */
106 t = malloc(sizeof *t+stack);
107 if(t == nil)
108 sysfatal("threadalloc malloc: %r");
109 memset(t, 0, sizeof *t);
110 t->stk = (uchar*)(t+1);
111 t->stksize = stack;
112 t->id = incref(&threadidref);
113 t->startfn = fn;
114 t->startarg = arg;
116 /* do a reasonable initialization */
117 memset(&t->context.uc, 0, sizeof t->context.uc);
118 sigemptyset(&zero);
119 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
121 /* must initialize with current context */
122 if(getcontext(&t->context.uc) < 0)
123 sysfatal("threadalloc getcontext: %r");
125 /* call makecontext to do the real work. */
126 /* leave a few words open on both ends */
127 t->context.uc.uc_stack.ss_sp = t->stk+8;
128 t->context.uc.uc_stack.ss_size = t->stksize-64;
129 #ifdef __sun__ /* sigh */
130 /* can avoid this with __MAKECONTEXT_V2_SOURCE but only on SunOS 5.9 */
131 t->context.uc.uc_stack.ss_sp =
132 (char*)t->context.uc.uc_stack.ss_sp
133 +t->context.uc.uc_stack.ss_size;
134 #endif
135 /*
136 * All this magic is because you have to pass makecontext a
137 * function that takes some number of word-sized variables,
138 * and on 64-bit machines pointers are bigger than words.
139 */
140 z = (ulong)t;
141 y = z;
142 z >>= 16; /* hide undefined 32-bit shift from 32-bit compilers */
143 x = z>>16;
144 makecontext(&t->context.uc, (void(*)())threadstart, 2, y, x);
146 return t;
149 _Thread*
150 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
152 _Thread *t;
154 t = threadalloc(fn, arg, stack);
155 t->proc = p;
156 addthreadinproc(p, t);
157 p->nthread++;
158 _threadready(t);
159 return t;
162 int
163 threadcreate(void (*fn)(void*), void *arg, uint stack)
165 _Thread *t;
167 t = _threadcreate(proc(), fn, arg, stack);
168 return t->id;
171 int
172 proccreate(void (*fn)(void*), void *arg, uint stack)
174 int id;
175 _Thread *t;
176 Proc *p;
178 p = procalloc();
179 t = _threadcreate(p, fn, arg, stack);
180 id = t->id; /* t might be freed after _procstart */
181 _procstart(p, procscheduler);
182 return id;
185 void
186 _threadswitch(void)
188 Proc *p;
190 needstack(0);
191 p = proc();
192 //print("threadswtch %p\n", p);
193 contextswitch(&p->thread->context, &p->schedcontext);
196 void
197 _threadready(_Thread *t)
199 Proc *p;
201 p = t->proc;
202 lock(&p->lock);
203 p->runrend.l = &p->lock;
204 addthread(&p->runqueue, t);
205 //print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid);
206 if(p != proc())
207 _procwakeupandunlock(&p->runrend);
208 else
209 unlock(&p->lock);
212 int
213 threadyield(void)
215 int n;
216 Proc *p;
218 p = proc();
219 n = p->nswitch;
220 _threadready(p->thread);
221 _threadswitch();
222 return p->nswitch - n;
225 void
226 threadexits(char *msg)
228 Proc *p;
230 p = proc();
231 if(msg == nil)
232 msg = "";
233 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
234 proc()->thread->exiting = 1;
235 _threadswitch();
238 static void
239 contextswitch(Context *from, Context *to)
241 if(swapcontext(&from->uc, &to->uc) < 0){
242 fprint(2, "swapcontext failed: %r\n");
243 assert(0);
247 static void
248 procscheduler(Proc *p)
250 _Thread *t;
252 setproc(p);
253 _threaddebug("scheduler enter");
254 // print("s %p\n", p);
255 lock(&p->lock);
256 for(;;){
257 while((t = p->runqueue.head) == nil){
258 if(p->nthread == 0)
259 goto Out;
260 p->runrend.l = &p->lock;
261 _threaddebug("scheduler sleep");
262 _procsleep(&p->runrend);
263 _threaddebug("scheduler wake");
265 delthread(&p->runqueue, t);
266 unlock(&p->lock);
267 p->thread = t;
268 p->nswitch++;
269 _threaddebug("run %d (%s)", t->id, t->name);
270 contextswitch(&p->schedcontext, &t->context);
271 //print("back in scheduler\n");
272 p->thread = nil;
273 lock(&p->lock);
274 if(t->exiting){
275 delthreadinproc(p, t);
276 p->nthread--;
277 //print("ntrhead %d\n", p->nthread);
278 free(t);
282 Out:
283 _threaddebug("scheduler exit");
284 if(p->mainproc){
285 /*
286 * Stupid bug - on Linux 2.6 and maybe elsewhere,
287 * if the main thread exits then the others keep running
288 * but the process shows up as a zombie in ps and is not
289 * attachable with ptrace. We'll just sit around pretending
290 * to be a system proc instead of exiting.
291 */
292 _threaddaemonize();
293 lock(&threadnproclock);
294 if(++threadnsysproc == threadnproc)
295 threadexitsall(p->msg);
296 p->sysproc = 1;
297 unlock(&threadnproclock);
298 for(;;)
299 sleep(1000);
302 delproc(p);
303 lock(&threadnproclock);
304 if(p->sysproc)
305 --threadnsysproc;
306 if(--threadnproc == threadnsysproc)
307 threadexitsall(p->msg);
308 unlock(&threadnproclock);
309 unlock(&p->lock);
310 free(p);
311 setproc(0);
314 void
315 _threadsetsysproc(void)
317 lock(&threadnproclock);
318 if(++threadnsysproc == threadnproc)
319 threadexitsall(nil);
320 unlock(&threadnproclock);
321 proc()->sysproc = 1;
324 void**
325 procdata(void)
327 return &proc()->udata;
330 extern Jmp *(*_notejmpbuf)(void);
331 static Jmp*
332 threadnotejmp(void)
334 return &proc()->sigjmp;
337 /*
338 * debugging
339 */
340 void
341 threadsetname(char *fmt, ...)
343 va_list arg;
344 _Thread *t;
346 t = proc()->thread;
347 va_start(arg, fmt);
348 vsnprint(t->name, sizeof t->name, fmt, arg);
349 va_end(arg);
352 char*
353 threadgetname(void)
355 return proc()->thread->name;
358 void
359 threadsetstate(char *fmt, ...)
361 va_list arg;
362 _Thread *t;
364 t = proc()->thread;
365 va_start(arg, fmt);
366 vsnprint(t->state, sizeof t->name, fmt, arg);
367 va_end(arg);
370 void
371 needstack(int n)
373 _Thread *t;
375 t = proc()->thread;
377 if((char*)&t <= (char*)t->stk
378 || (char*)&t - (char*)t->stk < 256+n){
379 fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
380 abort();
384 /*
385 * locking
386 */
387 static int
388 threadqlock(QLock *l, int block, ulong pc)
390 //print("threadqlock %p\n", l);
391 lock(&l->l);
392 if(l->owner == nil){
393 l->owner = (*threadnow)();
394 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
395 unlock(&l->l);
396 return 1;
398 if(!block){
399 unlock(&l->l);
400 return 0;
402 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
403 addthread(&l->waiting, (*threadnow)());
404 unlock(&l->l);
406 _threadswitch();
408 if(l->owner != (*threadnow)()){
409 fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
410 argv0, pc, l->owner, (*threadnow)());
411 abort();
413 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
414 return 1;
417 static void
418 threadqunlock(QLock *l, ulong pc)
420 _Thread *ready;
422 lock(&l->l);
423 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
424 if(l->owner == 0){
425 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
426 argv0, pc, l->owner, (*threadnow)());
427 abort();
429 if((l->owner = ready = l->waiting.head) != nil)
430 delthread(&l->waiting, l->owner);
431 /*
432 * N.B. Cannot call _threadready() before unlocking l->l,
433 * because the thread we are readying might:
434 * - be in another proc
435 * - start running immediately
436 * - and free l before we get a chance to run again
437 */
438 unlock(&l->l);
439 if(ready)
440 _threadready(l->owner);
443 static int
444 threadrlock(RWLock *l, int block, ulong pc)
446 USED(pc);
448 lock(&l->l);
449 if(l->writer == nil && l->wwaiting.head == nil){
450 l->readers++;
451 unlock(&l->l);
452 return 1;
454 if(!block){
455 unlock(&l->l);
456 return 0;
458 addthread(&l->rwaiting, (*threadnow)());
459 unlock(&l->l);
460 _threadswitch();
461 return 1;
464 static int
465 threadwlock(RWLock *l, int block, ulong pc)
467 USED(pc);
469 lock(&l->l);
470 if(l->writer == nil && l->readers == 0){
471 l->writer = (*threadnow)();
472 unlock(&l->l);
473 return 1;
475 if(!block){
476 unlock(&l->l);
477 return 0;
479 addthread(&l->wwaiting, (*threadnow)());
480 unlock(&l->l);
481 _threadswitch();
482 return 1;
485 static void
486 threadrunlock(RWLock *l, ulong pc)
488 _Thread *t;
490 USED(pc);
491 t = nil;
492 lock(&l->l);
493 --l->readers;
494 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
495 delthread(&l->wwaiting, t);
496 l->writer = t;
498 unlock(&l->l);
499 if(t)
500 _threadready(t);
504 static void
505 threadwunlock(RWLock *l, ulong pc)
507 _Thread *t;
509 USED(pc);
510 lock(&l->l);
511 l->writer = nil;
512 assert(l->readers == 0);
513 while((t = l->rwaiting.head) != nil){
514 delthread(&l->rwaiting, t);
515 l->readers++;
516 _threadready(t);
518 t = nil;
519 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
520 delthread(&l->wwaiting, t);
521 l->writer = t;
523 unlock(&l->l);
524 if(t)
525 _threadready(t);
528 /*
529 * sleep and wakeup
530 */
531 static void
532 threadrsleep(Rendez *r, ulong pc)
534 addthread(&r->waiting, proc()->thread);
535 qunlock(r->l);
536 _threadswitch();
537 qlock(r->l);
540 static int
541 threadrwakeup(Rendez *r, int all, ulong pc)
543 int i;
544 _Thread *t;
546 for(i=0;; i++){
547 if(i==1 && !all)
548 break;
549 if((t = r->waiting.head) == nil)
550 break;
551 delthread(&r->waiting, t);
552 _threadready(t);
554 return i;
557 /*
558 * startup
559 */
561 static int threadargc;
562 static char **threadargv;
563 int mainstacksize;
565 static void
566 threadmainstart(void *v)
568 USED(v);
570 /*
571 * N.B. This call to proc() is a program's first call (indirectly) to a
572 * pthreads function while executing on a non-pthreads-allocated
573 * stack. If the pthreads implementation is using the stack pointer
574 * to locate the per-thread data, then this call will blow up.
575 * This means the pthread implementation is not suitable for
576 * running under libthread. Time to write your own. Sorry.
577 */
578 threadmainproc = proc();
579 threadmain(threadargc, threadargv);
582 int
583 main(int argc, char **argv)
585 Proc *p;
587 argv0 = argv[0];
589 _threadsetupdaemonize();
591 threadargc = argc;
592 threadargv = argv;
594 /*
595 * Install locking routines into C library.
596 */
597 _lock = _threadlock;
598 _unlock = _threadunlock;
599 _qlock = threadqlock;
600 _qunlock = threadqunlock;
601 _rlock = threadrlock;
602 _runlock = threadrunlock;
603 _wlock = threadwlock;
604 _wunlock = threadwunlock;
605 _rsleep = threadrsleep;
606 _rwakeup = threadrwakeup;
607 _notejmpbuf = threadnotejmp;
609 _pthreadinit();
610 p = procalloc();
611 p->mainproc = 1;
612 _threadsetproc(p);
613 if(mainstacksize == 0)
614 mainstacksize = 256*1024;
615 _threadcreate(p, threadmainstart, nil, mainstacksize);
616 procscheduler(p);
617 sysfatal("procscheduler returned in threadmain!");
618 /* does not return */
619 return 0;
622 /*
623 * hooray for linked lists
624 */
625 static void
626 addthread(_Threadlist *l, _Thread *t)
628 if(l->tail){
629 l->tail->next = t;
630 t->prev = l->tail;
631 }else{
632 l->head = t;
633 t->prev = nil;
635 l->tail = t;
636 t->next = nil;
639 static void
640 delthread(_Threadlist *l, _Thread *t)
642 if(t->prev)
643 t->prev->next = t->next;
644 else
645 l->head = t->next;
646 if(t->next)
647 t->next->prev = t->prev;
648 else
649 l->tail = t->prev;
652 static void
653 addthreadinproc(Proc *p, _Thread *t)
655 _Threadlist *l;
657 l = &p->allthreads;
658 if(l->tail){
659 l->tail->allnext = t;
660 t->allprev = l->tail;
661 }else{
662 l->head = t;
663 t->allprev = nil;
665 l->tail = t;
666 t->allnext = nil;
669 static void
670 delthreadinproc(Proc *p, _Thread *t)
672 _Threadlist *l;
674 l = &p->allthreads;
675 if(t->allprev)
676 t->allprev->allnext = t->allnext;
677 else
678 l->head = t->allnext;
679 if(t->allnext)
680 t->allnext->allprev = t->allprev;
681 else
682 l->tail = t->allprev;
685 Proc *_threadprocs;
686 Lock _threadprocslock;
687 static Proc *_threadprocstail;
689 static void
690 addproc(Proc *p)
692 lock(&_threadprocslock);
693 if(_threadprocstail){
694 _threadprocstail->next = p;
695 p->prev = _threadprocstail;
696 }else{
697 _threadprocs = p;
698 p->prev = nil;
700 _threadprocstail = p;
701 p->next = nil;
702 unlock(&_threadprocslock);
705 static void
706 delproc(Proc *p)
708 lock(&_threadprocslock);
709 if(p->prev)
710 p->prev->next = p->next;
711 else
712 _threadprocs = p->next;
713 if(p->next)
714 p->next->prev = p->prev;
715 else
716 _threadprocstail = p->prev;
717 unlock(&_threadprocslock);
720 /*
721 * notify - for now just use the usual mechanisms
722 */
723 void
724 threadnotify(int (*f)(void*, char*), int in)
726 atnotify(f, in);