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 delproc(p);
285 lock(&threadnproclock);
286 if(p->sysproc)
287 --threadnsysproc;
288 if(--threadnproc == threadnsysproc)
289 threadexitsall(p->msg);
290 unlock(&threadnproclock);
291 unlock(&p->lock);
292 free(p);
293 setproc(0);
296 void
297 _threadsetsysproc(void)
299 lock(&threadnproclock);
300 if(++threadnsysproc == threadnproc)
301 exit(0);
302 unlock(&threadnproclock);
303 proc()->sysproc = 1;
306 void**
307 procdata(void)
309 return &proc()->udata;
312 extern Jmp *(*_notejmpbuf)(void);
313 static Jmp*
314 threadnotejmp(void)
316 return &proc()->sigjmp;
319 /*
320 * debugging
321 */
322 void
323 threadsetname(char *fmt, ...)
325 va_list arg;
326 _Thread *t;
328 t = proc()->thread;
329 va_start(arg, fmt);
330 vsnprint(t->name, sizeof t->name, fmt, arg);
331 va_end(arg);
334 char*
335 threadgetname(void)
337 return proc()->thread->name;
340 void
341 threadsetstate(char *fmt, ...)
343 va_list arg;
344 _Thread *t;
346 t = proc()->thread;
347 va_start(arg, fmt);
348 vsnprint(t->state, sizeof t->name, fmt, arg);
349 va_end(arg);
352 void
353 needstack(int n)
355 _Thread *t;
357 t = proc()->thread;
359 if((char*)&t <= (char*)t->stk
360 || (char*)&t - (char*)t->stk < 256+n){
361 fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
362 abort();
366 /*
367 * locking
368 */
369 static int
370 threadqlock(QLock *l, int block, ulong pc)
372 //print("threadqlock %p\n", l);
373 lock(&l->l);
374 if(l->owner == nil){
375 l->owner = (*threadnow)();
376 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
377 unlock(&l->l);
378 return 1;
380 if(!block){
381 unlock(&l->l);
382 return 0;
384 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
385 addthread(&l->waiting, (*threadnow)());
386 unlock(&l->l);
388 _threadswitch();
390 if(l->owner != (*threadnow)()){
391 fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
392 argv0, pc, l->owner, (*threadnow)());
393 abort();
395 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
396 return 1;
399 static void
400 threadqunlock(QLock *l, ulong pc)
402 lock(&l->l);
403 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
404 if(l->owner == 0){
405 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
406 argv0, pc, l->owner, (*threadnow)());
407 abort();
409 if((l->owner = l->waiting.head) != nil){
410 delthread(&l->waiting, l->owner);
411 _threadready(l->owner);
413 unlock(&l->l);
416 static int
417 threadrlock(RWLock *l, int block, ulong pc)
419 USED(pc);
421 lock(&l->l);
422 if(l->writer == nil && l->wwaiting.head == nil){
423 l->readers++;
424 unlock(&l->l);
425 return 1;
427 if(!block){
428 unlock(&l->l);
429 return 0;
431 addthread(&l->rwaiting, (*threadnow)());
432 unlock(&l->l);
433 _threadswitch();
434 return 1;
437 static int
438 threadwlock(RWLock *l, int block, ulong pc)
440 USED(pc);
442 lock(&l->l);
443 if(l->writer == nil && l->readers == 0){
444 l->writer = (*threadnow)();
445 unlock(&l->l);
446 return 1;
448 if(!block){
449 unlock(&l->l);
450 return 0;
452 addthread(&l->wwaiting, (*threadnow)());
453 unlock(&l->l);
454 _threadswitch();
455 return 1;
458 static void
459 threadrunlock(RWLock *l, ulong pc)
461 _Thread *t;
463 USED(pc);
464 lock(&l->l);
465 --l->readers;
466 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
467 delthread(&l->wwaiting, t);
468 l->writer = t;
469 _threadready(t);
471 unlock(&l->l);
474 static void
475 threadwunlock(RWLock *l, ulong pc)
477 _Thread *t;
479 USED(pc);
480 lock(&l->l);
481 l->writer = nil;
482 assert(l->readers == 0);
483 while((t = l->rwaiting.head) != nil){
484 delthread(&l->rwaiting, t);
485 l->readers++;
486 _threadready(t);
488 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
489 delthread(&l->wwaiting, t);
490 l->writer = t;
491 _threadready(t);
493 unlock(&l->l);
496 /*
497 * sleep and wakeup
498 */
499 static void
500 threadrsleep(Rendez *r, ulong pc)
502 addthread(&r->waiting, proc()->thread);
503 qunlock(r->l);
504 _threadswitch();
505 qlock(r->l);
508 static int
509 threadrwakeup(Rendez *r, int all, ulong pc)
511 int i;
512 _Thread *t;
514 for(i=0;; i++){
515 if(i==1 && !all)
516 break;
517 if((t = r->waiting.head) == nil)
518 break;
519 delthread(&r->waiting, t);
520 _threadready(t);
522 return i;
525 /*
526 * startup
527 */
529 static int threadargc;
530 static char **threadargv;
531 int mainstacksize;
533 static void
534 threadmainstart(void *v)
536 USED(v);
538 /*
539 * N.B. This call to proc() is a program's first call (indirectly) to a
540 * pthreads function while executing on a non-pthreads-allocated
541 * stack. If the pthreads implementation is using the stack pointer
542 * to locate the per-thread data, then this call will blow up.
543 * This means the pthread implementation is not suitable for
544 * running under libthread. Time to write your own. Sorry.
545 */
546 threadmainproc = proc();
547 threadmain(threadargc, threadargv);
550 int
551 main(int argc, char **argv)
553 Proc *p;
555 argv0 = argv[0];
557 _threadsetupdaemonize();
559 threadargc = argc;
560 threadargv = argv;
562 /*
563 * Install locking routines into C library.
564 */
565 _lock = _threadlock;
566 _unlock = _threadunlock;
567 _qlock = threadqlock;
568 _qunlock = threadqunlock;
569 _rlock = threadrlock;
570 _runlock = threadrunlock;
571 _wlock = threadwlock;
572 _wunlock = threadwunlock;
573 _rsleep = threadrsleep;
574 _rwakeup = threadrwakeup;
575 _notejmpbuf = threadnotejmp;
577 _pthreadinit();
578 p = procalloc();
579 _threadsetproc(p);
580 if(mainstacksize == 0)
581 mainstacksize = 256*1024;
582 _threadcreate(p, threadmainstart, nil, mainstacksize);
583 procscheduler(p);
584 _threaddaemonize();
585 _threadpexit();
586 return 0;
589 /*
590 * hooray for linked lists
591 */
592 static void
593 addthread(_Threadlist *l, _Thread *t)
595 if(l->tail){
596 l->tail->next = t;
597 t->prev = l->tail;
598 }else{
599 l->head = t;
600 t->prev = nil;
602 l->tail = t;
603 t->next = nil;
606 static void
607 delthread(_Threadlist *l, _Thread *t)
609 if(t->prev)
610 t->prev->next = t->next;
611 else
612 l->head = t->next;
613 if(t->next)
614 t->next->prev = t->prev;
615 else
616 l->tail = t->prev;
619 static void
620 addthreadinproc(Proc *p, _Thread *t)
622 _Threadlist *l;
624 l = &p->allthreads;
625 if(l->tail){
626 l->tail->allnext = t;
627 t->allprev = l->tail;
628 }else{
629 l->head = t;
630 t->allprev = nil;
632 l->tail = t;
633 t->allnext = nil;
636 static void
637 delthreadinproc(Proc *p, _Thread *t)
639 _Threadlist *l;
641 l = &p->allthreads;
642 if(t->allprev)
643 t->allprev->allnext = t->allnext;
644 else
645 l->head = t->allnext;
646 if(t->allnext)
647 t->allnext->allprev = t->allprev;
648 else
649 l->tail = t->allprev;
652 Proc *_threadprocs;
653 Lock _threadprocslock;
654 static Proc *_threadprocstail;
656 static void
657 addproc(Proc *p)
659 lock(&_threadprocslock);
660 if(_threadprocstail){
661 _threadprocstail->next = p;
662 p->prev = _threadprocstail;
663 }else{
664 _threadprocs = p;
665 p->prev = nil;
667 _threadprocstail = p;
668 p->next = nil;
669 unlock(&_threadprocslock);
672 static void
673 delproc(Proc *p)
675 lock(&_threadprocslock);
676 if(p->prev)
677 p->prev->next = p->next;
678 else
679 _threadprocs = p->next;
680 if(p->next)
681 p->next->prev = p->prev;
682 else
683 _threadprocstail = p->prev;
684 unlock(&_threadprocslock);
687 /*
688 * notify - for now just use the usual mechanisms
689 */
690 void
691 threadnotify(int (*f)(void*, char*), int in)
693 atnotify(f, in);