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 scheduler(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(void *v)
81 {
82 _Thread *t;
84 t = v;
85 //print("threadstart %p\n", v);
86 t->startfn(t->startarg);
87 //print("threadexits %p\n", v);
88 threadexits(nil);
89 //print("not reacehd\n");
90 }
92 static _Thread*
93 threadalloc(void (*fn)(void*), void *arg, uint stack)
94 {
95 _Thread *t;
96 sigset_t zero;
98 /* allocate the task and stack together */
99 t = malloc(sizeof *t+stack);
100 if(t == nil)
101 sysfatal("threadalloc malloc: %r");
102 memset(t, 0, sizeof *t);
103 t->stk = (uchar*)(t+1);
104 t->stksize = stack;
105 t->id = incref(&threadidref);
106 t->startfn = fn;
107 t->startarg = arg;
109 /* do a reasonable initialization */
110 memset(&t->context.uc, 0, sizeof t->context.uc);
111 sigemptyset(&zero);
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;
127 #endif
128 makecontext(&t->context.uc, (void(*)())threadstart, 1, t);
130 return t;
133 _Thread*
134 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
136 _Thread *t;
138 t = threadalloc(fn, arg, stack);
139 t->proc = p;
140 addthreadinproc(p, t);
141 p->nthread++;
142 _threadready(t);
143 return t;
146 int
147 threadcreate(void (*fn)(void*), void *arg, uint stack)
149 _Thread *t;
151 t = _threadcreate(proc(), fn, arg, stack);
152 return t->id;
155 int
156 proccreate(void (*fn)(void*), void *arg, uint stack)
158 _Thread *t;
159 Proc *p;
161 p = procalloc();
162 t = _threadcreate(p, fn, arg, stack);
163 _procstart(p, scheduler);
164 return t->id;
167 void
168 _threadswitch(void)
170 Proc *p;
172 needstack(0);
173 p = proc();
174 //print("threadswtch %p\n", p);
175 contextswitch(&p->thread->context, &p->schedcontext);
178 void
179 _threadready(_Thread *t)
181 Proc *p;
183 p = t->proc;
184 lock(&p->lock);
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);
188 if(p != proc())
189 _procwakeupandunlock(&p->runrend);
190 else
191 unlock(&p->lock);
194 int
195 threadyield(void)
197 int n;
198 Proc *p;
200 p = proc();
201 n = p->nswitch;
202 _threadready(p->thread);
203 _threadswitch();
204 return p->nswitch - n;
207 void
208 threadexits(char *msg)
210 Proc *p;
212 p = proc();
213 if(msg == nil)
214 msg = "";
215 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
216 proc()->thread->exiting = 1;
217 _threadswitch();
220 static void
221 contextswitch(Context *from, Context *to)
223 if(swapcontext(&from->uc, &to->uc) < 0){
224 fprint(2, "swapcontext failed: %r\n");
225 assert(0);
229 static void
230 scheduler(Proc *p)
232 _Thread *t;
234 setproc(p);
235 _threaddebug("scheduler enter");
236 // print("s %p\n", p);
237 lock(&p->lock);
238 for(;;){
239 while((t = p->runqueue.head) == nil){
240 if(p->nthread == 0)
241 goto Out;
242 p->runrend.l = &p->lock;
243 _threaddebug("scheduler sleep");
244 _procsleep(&p->runrend);
245 _threaddebug("scheduler wake");
247 delthread(&p->runqueue, t);
248 unlock(&p->lock);
249 p->thread = t;
250 p->nswitch++;
251 _threaddebug("run %d (%s)", t->id, t->name);
252 contextswitch(&p->schedcontext, &t->context);
253 //print("back in scheduler\n");
254 p->thread = nil;
255 lock(&p->lock);
256 if(t->exiting){
257 delthreadinproc(p, t);
258 p->nthread--;
259 //print("ntrhead %d\n", p->nthread);
260 free(t);
264 Out:
265 _threaddebug("scheduler exit");
266 delproc(p);
267 lock(&threadnproclock);
268 if(p->sysproc)
269 --threadnsysproc;
270 if(--threadnproc == threadnsysproc)
271 threadexitsall(p->msg);
272 unlock(&threadnproclock);
273 unlock(&p->lock);
274 free(p);
275 setproc(0);
278 void
279 _threadsetsysproc(void)
281 lock(&threadnproclock);
282 if(++threadnsysproc == threadnproc)
283 exit(0);
284 unlock(&threadnproclock);
285 proc()->sysproc = 1;
288 void**
289 procdata(void)
291 return &proc()->udata;
294 extern Jmp *(*_notejmpbuf)(void);
295 static Jmp*
296 threadnotejmp(void)
298 return &proc()->sigjmp;
301 /*
302 * debugging
303 */
304 void
305 threadsetname(char *fmt, ...)
307 va_list arg;
308 _Thread *t;
310 t = proc()->thread;
311 va_start(arg, fmt);
312 vsnprint(t->name, sizeof t->name, fmt, arg);
313 va_end(arg);
316 void
317 threadsetstate(char *fmt, ...)
319 va_list arg;
320 _Thread *t;
322 t = proc()->thread;
323 va_start(arg, fmt);
324 vsnprint(t->state, sizeof t->name, fmt, arg);
325 va_end(arg);
328 void
329 needstack(int n)
331 _Thread *t;
333 t = proc()->thread;
335 if((char*)&t <= (char*)t->stk
336 || (char*)&t - (char*)t->stk < 256+n){
337 fprint(2, "thread stack overflow\n");
338 abort();
342 /*
343 * locking
344 */
345 static int
346 threadqlock(QLock *l, int block, ulong pc)
348 //print("threadqlock %p\n", l);
349 lock(&l->l);
350 if(l->owner == nil){
351 l->owner = (*threadnow)();
352 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
353 unlock(&l->l);
354 return 1;
356 if(!block){
357 unlock(&l->l);
358 return 0;
360 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
361 addthread(&l->waiting, (*threadnow)());
362 unlock(&l->l);
364 _threadswitch();
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)());
369 abort();
371 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
372 return 1;
375 static void
376 threadqunlock(QLock *l, ulong pc)
378 lock(&l->l);
379 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
380 if(l->owner == 0){
381 fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
382 argv0, pc, l->owner, (*threadnow)());
383 abort();
385 if((l->owner = l->waiting.head) != nil){
386 delthread(&l->waiting, l->owner);
387 _threadready(l->owner);
389 unlock(&l->l);
392 static int
393 threadrlock(RWLock *l, int block, ulong pc)
395 USED(pc);
397 lock(&l->l);
398 if(l->writer == nil && l->wwaiting.head == nil){
399 l->readers++;
400 unlock(&l->l);
401 return 1;
403 if(!block){
404 unlock(&l->l);
405 return 0;
407 addthread(&l->rwaiting, (*threadnow)());
408 unlock(&l->l);
409 _threadswitch();
410 return 1;
413 static int
414 threadwlock(RWLock *l, int block, ulong pc)
416 USED(pc);
418 lock(&l->l);
419 if(l->writer == nil && l->readers == 0){
420 l->writer = (*threadnow)();
421 unlock(&l->l);
422 return 1;
424 if(!block){
425 unlock(&l->l);
426 return 0;
428 addthread(&l->wwaiting, (*threadnow)());
429 unlock(&l->l);
430 _threadswitch();
431 return 1;
434 static void
435 threadrunlock(RWLock *l, ulong pc)
437 _Thread *t;
439 USED(pc);
440 lock(&l->l);
441 --l->readers;
442 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
443 delthread(&l->wwaiting, t);
444 l->writer = t;
445 _threadready(t);
447 unlock(&l->l);
450 static void
451 threadwunlock(RWLock *l, ulong pc)
453 _Thread *t;
455 USED(pc);
456 lock(&l->l);
457 l->writer = nil;
458 assert(l->readers == 0);
459 while((t = l->rwaiting.head) != nil){
460 delthread(&l->rwaiting, t);
461 l->readers++;
462 _threadready(t);
464 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
465 delthread(&l->wwaiting, t);
466 l->writer = t;
467 _threadready(t);
469 unlock(&l->l);
472 /*
473 * sleep and wakeup
474 */
475 static void
476 threadrsleep(Rendez *r, ulong pc)
478 addthread(&r->waiting, proc()->thread);
479 qunlock(r->l);
480 _threadswitch();
481 qlock(r->l);
484 static int
485 threadrwakeup(Rendez *r, int all, ulong pc)
487 int i;
488 _Thread *t;
490 for(i=0;; i++){
491 if(i==1 && !all)
492 break;
493 if((t = r->waiting.head) == nil)
494 break;
495 delthread(&r->waiting, t);
496 _threadready(t);
498 return i;
501 /*
502 * startup
503 */
505 static int threadargc;
506 static char **threadargv;
507 int mainstacksize;
509 static void
510 threadmainstart(void *v)
512 USED(v);
513 threadmainproc = proc();
514 threadmain(threadargc, threadargv);
517 int
518 main(int argc, char **argv)
520 Proc *p;
522 argv0 = argv[0];
524 _threadsetupdaemonize();
526 threadargc = argc;
527 threadargv = argv;
529 /*
530 * Install locking routines into C library.
531 */
532 _lock = _threadlock;
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;
544 _pthreadinit();
545 p = procalloc();
546 _threadsetproc(p);
547 if(mainstacksize == 0)
548 mainstacksize = 65536;
549 _threadcreate(p, threadmainstart, nil, mainstacksize);
550 scheduler(p);
551 _threaddaemonize();
552 _threadpexit();
553 return 0;
556 /*
557 * hooray for linked lists
558 */
559 static void
560 addthread(_Threadlist *l, _Thread *t)
562 if(l->tail){
563 l->tail->next = t;
564 t->prev = l->tail;
565 }else{
566 l->head = t;
567 t->prev = nil;
569 l->tail = t;
570 t->next = nil;
573 static void
574 delthread(_Threadlist *l, _Thread *t)
576 if(t->prev)
577 t->prev->next = t->next;
578 else
579 l->head = t->next;
580 if(t->next)
581 t->next->prev = t->prev;
582 else
583 l->tail = t->prev;
586 static void
587 addthreadinproc(Proc *p, _Thread *t)
589 _Threadlist *l;
591 l = &p->allthreads;
592 if(l->tail){
593 l->tail->allnext = t;
594 t->allprev = l->tail;
595 }else{
596 l->head = t;
597 t->allprev = nil;
599 l->tail = t;
600 t->allnext = nil;
603 static void
604 delthreadinproc(Proc *p, _Thread *t)
606 _Threadlist *l;
608 l = &p->allthreads;
609 if(t->allprev)
610 t->allprev->allnext = t->allnext;
611 else
612 l->head = t->allnext;
613 if(t->allnext)
614 t->allnext->allprev = t->allprev;
615 else
616 l->tail = t->allprev;
619 Proc *_threadprocs;
620 Lock _threadprocslock;
621 static Proc *_threadprocstail;
623 static void
624 addproc(Proc *p)
626 lock(&_threadprocslock);
627 if(_threadprocstail){
628 _threadprocstail->next = p;
629 p->prev = _threadprocstail;
630 }else{
631 _threadprocs = p;
632 p->prev = nil;
634 _threadprocstail = p;
635 p->next = nil;
636 unlock(&_threadprocslock);
639 static void
640 delproc(Proc *p)
642 lock(&_threadprocslock);
643 if(p->prev)
644 p->prev->next = p->next;
645 else
646 _threadprocs = p->next;
647 if(p->next)
648 p->next->prev = p->prev;
649 else
650 _threadprocstail = p->prev;
651 unlock(&_threadprocslock);
654 /*
655 * notify - for now just use the usual mechanisms
656 */
657 void
658 threadnotify(int (*f)(void*, char*), int in)
660 atnotify(f, in);