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);
313 void
314 _threadsetsysproc(void)
316 lock(&threadnproclock);
317 if(++threadnsysproc == threadnproc)
318 threadexitsall(nil);
319 unlock(&threadnproclock);
320 proc()->sysproc = 1;
323 void**
324 procdata(void)
326 return &proc()->udata;
329 void**
330 threaddata(void)
332 return &proc()->thread->udata;
335 extern Jmp *(*_notejmpbuf)(void);
336 static Jmp*
337 threadnotejmp(void)
339 return &proc()->sigjmp;
342 /*
343 * debugging
344 */
345 void
346 threadsetname(char *fmt, ...)
348 va_list arg;
349 _Thread *t;
351 t = proc()->thread;
352 va_start(arg, fmt);
353 vsnprint(t->name, sizeof t->name, fmt, arg);
354 va_end(arg);
357 char*
358 threadgetname(void)
360 return proc()->thread->name;
363 void
364 threadsetstate(char *fmt, ...)
366 va_list arg;
367 _Thread *t;
369 t = proc()->thread;
370 va_start(arg, fmt);
371 vsnprint(t->state, sizeof t->name, fmt, arg);
372 va_end(arg);
375 void
376 needstack(int n)
378 _Thread *t;
380 t = proc()->thread;
382 if((char*)&t <= (char*)t->stk
383 || (char*)&t - (char*)t->stk < 256+n){
384 fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
385 abort();
389 /*
390 * locking
391 */
392 static int
393 threadqlock(QLock *l, int block, ulong pc)
395 //print("threadqlock %p\n", l);
396 lock(&l->l);
397 if(l->owner == nil){
398 l->owner = (*threadnow)();
399 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
400 unlock(&l->l);
401 return 1;
403 if(!block){
404 unlock(&l->l);
405 return 0;
407 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
408 addthread(&l->waiting, (*threadnow)());
409 unlock(&l->l);
411 _threadswitch();
413 if(l->owner != (*threadnow)()){
414 fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
415 argv0, pc, l->owner, (*threadnow)());
416 abort();
418 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
419 return 1;
422 static void
423 threadqunlock(QLock *l, ulong pc)
425 _Thread *ready;
427 lock(&l->l);
428 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
429 if(l->owner == 0){
430 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
431 argv0, pc, l->owner, (*threadnow)());
432 abort();
434 if((l->owner = ready = l->waiting.head) != nil)
435 delthread(&l->waiting, l->owner);
436 /*
437 * N.B. Cannot call _threadready() before unlocking l->l,
438 * because the thread we are readying might:
439 * - be in another proc
440 * - start running immediately
441 * - and free l before we get a chance to run again
442 */
443 unlock(&l->l);
444 if(ready)
445 _threadready(l->owner);
448 static int
449 threadrlock(RWLock *l, int block, ulong pc)
451 USED(pc);
453 lock(&l->l);
454 if(l->writer == nil && l->wwaiting.head == nil){
455 l->readers++;
456 unlock(&l->l);
457 return 1;
459 if(!block){
460 unlock(&l->l);
461 return 0;
463 addthread(&l->rwaiting, (*threadnow)());
464 unlock(&l->l);
465 _threadswitch();
466 return 1;
469 static int
470 threadwlock(RWLock *l, int block, ulong pc)
472 USED(pc);
474 lock(&l->l);
475 if(l->writer == nil && l->readers == 0){
476 l->writer = (*threadnow)();
477 unlock(&l->l);
478 return 1;
480 if(!block){
481 unlock(&l->l);
482 return 0;
484 addthread(&l->wwaiting, (*threadnow)());
485 unlock(&l->l);
486 _threadswitch();
487 return 1;
490 static void
491 threadrunlock(RWLock *l, ulong pc)
493 _Thread *t;
495 USED(pc);
496 t = nil;
497 lock(&l->l);
498 --l->readers;
499 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
500 delthread(&l->wwaiting, t);
501 l->writer = t;
503 unlock(&l->l);
504 if(t)
505 _threadready(t);
509 static void
510 threadwunlock(RWLock *l, ulong pc)
512 _Thread *t;
514 USED(pc);
515 lock(&l->l);
516 l->writer = nil;
517 assert(l->readers == 0);
518 while((t = l->rwaiting.head) != nil){
519 delthread(&l->rwaiting, t);
520 l->readers++;
521 _threadready(t);
523 t = nil;
524 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
525 delthread(&l->wwaiting, t);
526 l->writer = t;
528 unlock(&l->l);
529 if(t)
530 _threadready(t);
533 /*
534 * sleep and wakeup
535 */
536 static void
537 threadrsleep(Rendez *r, ulong pc)
539 addthread(&r->waiting, proc()->thread);
540 qunlock(r->l);
541 _threadswitch();
542 qlock(r->l);
545 static int
546 threadrwakeup(Rendez *r, int all, ulong pc)
548 int i;
549 _Thread *t;
551 for(i=0;; i++){
552 if(i==1 && !all)
553 break;
554 if((t = r->waiting.head) == nil)
555 break;
556 delthread(&r->waiting, t);
557 _threadready(t);
559 return i;
562 /*
563 * startup
564 */
566 static int threadargc;
567 static char **threadargv;
568 int mainstacksize;
570 static void
571 threadmainstart(void *v)
573 USED(v);
575 /*
576 * N.B. This call to proc() is a program's first call (indirectly) to a
577 * pthreads function while executing on a non-pthreads-allocated
578 * stack. If the pthreads implementation is using the stack pointer
579 * to locate the per-thread data, then this call will blow up.
580 * This means the pthread implementation is not suitable for
581 * running under libthread. Time to write your own. Sorry.
582 */
583 threadmainproc = proc();
584 threadmain(threadargc, threadargv);
587 int
588 main(int argc, char **argv)
590 Proc *p;
592 argv0 = argv[0];
594 _threadsetupdaemonize();
596 threadargc = argc;
597 threadargv = argv;
599 /*
600 * Install locking routines into C library.
601 */
602 _lock = _threadlock;
603 _unlock = _threadunlock;
604 _qlock = threadqlock;
605 _qunlock = threadqunlock;
606 _rlock = threadrlock;
607 _runlock = threadrunlock;
608 _wlock = threadwlock;
609 _wunlock = threadwunlock;
610 _rsleep = threadrsleep;
611 _rwakeup = threadrwakeup;
612 _notejmpbuf = threadnotejmp;
614 _pthreadinit();
615 p = procalloc();
616 p->mainproc = 1;
617 _threadsetproc(p);
618 if(mainstacksize == 0)
619 mainstacksize = 256*1024;
620 _threadcreate(p, threadmainstart, nil, mainstacksize);
621 procscheduler(p);
622 sysfatal("procscheduler returned in threadmain!");
623 /* does not return */
624 return 0;
627 /*
628 * hooray for linked lists
629 */
630 static void
631 addthread(_Threadlist *l, _Thread *t)
633 if(l->tail){
634 l->tail->next = t;
635 t->prev = l->tail;
636 }else{
637 l->head = t;
638 t->prev = nil;
640 l->tail = t;
641 t->next = nil;
644 static void
645 delthread(_Threadlist *l, _Thread *t)
647 if(t->prev)
648 t->prev->next = t->next;
649 else
650 l->head = t->next;
651 if(t->next)
652 t->next->prev = t->prev;
653 else
654 l->tail = t->prev;
657 static void
658 addthreadinproc(Proc *p, _Thread *t)
660 _Threadlist *l;
662 l = &p->allthreads;
663 if(l->tail){
664 l->tail->allnext = t;
665 t->allprev = l->tail;
666 }else{
667 l->head = t;
668 t->allprev = nil;
670 l->tail = t;
671 t->allnext = nil;
674 static void
675 delthreadinproc(Proc *p, _Thread *t)
677 _Threadlist *l;
679 l = &p->allthreads;
680 if(t->allprev)
681 t->allprev->allnext = t->allnext;
682 else
683 l->head = t->allnext;
684 if(t->allnext)
685 t->allnext->allprev = t->allprev;
686 else
687 l->tail = t->allprev;
690 Proc *_threadprocs;
691 Lock _threadprocslock;
692 static Proc *_threadprocstail;
694 static void
695 addproc(Proc *p)
697 lock(&_threadprocslock);
698 if(_threadprocstail){
699 _threadprocstail->next = p;
700 p->prev = _threadprocstail;
701 }else{
702 _threadprocs = p;
703 p->prev = nil;
705 _threadprocstail = p;
706 p->next = nil;
707 unlock(&_threadprocslock);
710 static void
711 delproc(Proc *p)
713 lock(&_threadprocslock);
714 if(p->prev)
715 p->prev->next = p->next;
716 else
717 _threadprocs = p->next;
718 if(p->next)
719 p->next->prev = p->prev;
720 else
721 _threadprocstail = p->prev;
722 unlock(&_threadprocslock);
725 /*
726 * notify - for now just use the usual mechanisms
727 */
728 void
729 threadnotify(int (*f)(void*, char*), int in)
731 atnotify(f, in);