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;
27 return;
29 va_start(arg, fmt);
30 vsnprint(buf, sizeof buf, fmt, arg);
31 va_end(arg);
32 t = proc()->thread;
33 if(t)
34 fprint(2, "%d.%d: %s\n", getpid(), t->id, buf);
35 else
36 fprint(2, "%d._: %s\n", getpid(), buf);
37 }
39 static _Thread*
40 getthreadnow(void)
41 {
42 return proc()->thread;
43 }
44 _Thread *(*threadnow)(void) = getthreadnow;
46 static Proc*
47 procalloc(void)
48 {
49 Proc *p;
51 p = malloc(sizeof *p);
52 if(p == nil)
53 sysfatal("procalloc malloc: %r");
54 memset(p, 0, sizeof *p);
55 addproc(p);
56 lock(&threadnproclock);
57 threadnproc++;
58 unlock(&threadnproclock);
59 return p;
60 }
62 static void
63 threadstart(void *v)
64 {
65 _Thread *t;
67 t = v;
68 t->startfn(t->startarg);
69 memset(&v, 0xff, 32); /* try to cut off stack traces */
70 threadexits(nil);
71 }
73 static _Thread*
74 threadalloc(void (*fn)(void*), void *arg, uint stack)
75 {
76 _Thread *t;
77 sigset_t zero;
79 /* allocate the task and stack together */
80 t = malloc(sizeof *t+stack);
81 if(t == nil)
82 sysfatal("threadalloc malloc: %r");
83 memset(t, 0, sizeof *t);
84 t->stk = (uchar*)(t+1);
85 t->stksize = stack;
86 t->id = incref(&threadidref);
87 t->startfn = fn;
88 t->startarg = arg;
90 /* do a reasonable initialization */
91 memset(&t->context.uc, 0, sizeof t->context.uc);
92 sigemptyset(&zero);
93 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
95 /* must initialize with current context */
96 getcontext(&t->context.uc);
98 /* call makecontext to do the real work. */
99 /* leave a few words open on both ends */
100 t->context.uc.uc_stack.ss_sp = t->stk+8;
101 t->context.uc.uc_stack.ss_size = t->stksize-64;
102 makecontext(&t->context.uc, (void(*)())threadstart, 1, t);
104 return t;
107 _Thread*
108 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
110 _Thread *t;
112 t = threadalloc(fn, arg, stack);
113 t->proc = p;
114 addthreadinproc(p, t);
115 p->nthread++;
116 _threadready(t);
117 return t;
120 int
121 threadcreate(void (*fn)(void*), void *arg, uint stack)
123 _Thread *t;
125 t = _threadcreate(proc(), fn, arg, stack);
126 return t->id;
129 int
130 proccreate(void (*fn)(void*), void *arg, uint stack)
132 _Thread *t;
133 Proc *p;
135 p = procalloc();
136 t = _threadcreate(p, fn, arg, stack);
137 _procstart(p, scheduler);
138 return t->id;
141 void
142 _threadswitch(void)
144 Proc *p;
146 p = proc();
147 contextswitch(&p->thread->context, &p->schedcontext);
150 void
151 _threadready(_Thread *t)
153 Proc *p;
155 p = t->proc;
156 lock(&p->lock);
157 addthread(&p->runqueue, t);
158 //print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid);
159 if(p != proc())
160 _procwakeup(&p->runrend);
161 unlock(&p->lock);
164 int
165 threadyield(void)
167 int n;
168 Proc *p;
170 p = proc();
171 n = p->nswitch;
172 _threadready(p->thread);
173 _threadswitch();
174 return p->nswitch - n;
177 void
178 threadexits(char *msg)
180 Proc *p;
182 p = proc();
183 if(msg == nil)
184 msg = "";
185 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
186 proc()->thread->exiting = 1;
187 _threadswitch();
190 static void
191 contextswitch(Context *from, Context *to)
193 if(swapcontext(&from->uc, &to->uc) < 0){
194 fprint(2, "swapcontext failed: %r\n");
195 assert(0);
199 static void
200 scheduler(Proc *p)
202 _Thread *t;
204 setproc(p);
205 _threaddebug("scheduler enter");
206 // print("s %p %d\n", p, gettid());
207 lock(&p->lock);
208 for(;;){
209 while((t = p->runqueue.head) == nil){
210 if(p->nthread == 0)
211 goto Out;
212 p->runrend.l = &p->lock;
213 _threaddebug("scheduler sleep");
214 _procsleep(&p->runrend);
215 _threaddebug("scheduler wake");
217 delthread(&p->runqueue, t);
218 unlock(&p->lock);
219 p->thread = t;
220 p->nswitch++;
221 _threaddebug("run %d (%s)", t->id, t->name);
222 contextswitch(&p->schedcontext, &t->context);
223 p->thread = nil;
224 lock(&p->lock);
225 if(t->exiting){
226 delthreadinproc(p, t);
227 p->nthread--;
228 free(t);
232 Out:
233 _threaddebug("scheduler exit");
234 delproc(p);
235 lock(&threadnproclock);
236 if(p->sysproc)
237 --threadnsysproc;
238 if(--threadnproc == threadnsysproc)
239 threadexitsall(p->msg);
240 unlock(&threadnproclock);
241 unlock(&p->lock);
242 free(p);
243 setproc(0);
246 void
247 _threadsetsysproc(void)
249 lock(&threadnproclock);
250 if(++threadnsysproc == threadnproc)
251 exit(0);
252 unlock(&threadnproclock);
253 proc()->sysproc = 1;
256 void**
257 procdata(void)
259 return &proc()->udata;
262 extern Jmp *(*_notejmpbuf)(void);
263 static Jmp*
264 threadnotejmp(void)
266 return &proc()->sigjmp;
269 /*
270 * debugging
271 */
272 void
273 threadsetname(char *fmt, ...)
275 va_list arg;
276 _Thread *t;
278 t = proc()->thread;
279 va_start(arg, fmt);
280 vsnprint(t->name, sizeof t->name, fmt, arg);
281 va_end(arg);
284 void
285 threadsetstate(char *fmt, ...)
287 va_list arg;
288 _Thread *t;
290 t = proc()->thread;
291 va_start(arg, fmt);
292 vsnprint(t->state, sizeof t->name, fmt, arg);
293 va_end(arg);
296 /*
297 * locking
298 */
299 static int
300 threadqlock(QLock *l, int block, ulong pc)
302 lock(&l->l);
303 if(l->owner == nil){
304 l->owner = (*threadnow)();
305 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
306 unlock(&l->l);
307 return 1;
309 if(!block){
310 unlock(&l->l);
311 return 0;
313 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
314 addthread(&l->waiting, (*threadnow)());
315 unlock(&l->l);
317 _threadswitch();
319 if(l->owner != (*threadnow)()){
320 fprint(2, "qlock pc=0x%lux owner=%p self=%p oops\n", pc, l->owner, (*threadnow)());
321 abort();
323 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
324 return 1;
327 static void
328 threadqunlock(QLock *l, ulong pc)
330 lock(&l->l);
331 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
332 if(l->owner == nil){
333 fprint(2, "qunlock pc=0x%lux owner=%p self=%p oops\n",
334 pc, l->owner, (*threadnow)());
335 abort();
337 if((l->owner = l->waiting.head) != nil){
338 delthread(&l->waiting, l->owner);
339 _threadready(l->owner);
341 unlock(&l->l);
344 static int
345 threadrlock(RWLock *l, int block, ulong pc)
347 USED(pc);
349 lock(&l->l);
350 if(l->writer == nil && l->wwaiting.head == nil){
351 l->readers++;
352 unlock(&l->l);
353 return 1;
355 if(!block){
356 unlock(&l->l);
357 return 0;
359 addthread(&l->rwaiting, (*threadnow)());
360 unlock(&l->l);
361 _threadswitch();
362 return 1;
365 static int
366 threadwlock(RWLock *l, int block, ulong pc)
368 USED(pc);
370 lock(&l->l);
371 if(l->writer == nil && l->readers == 0){
372 l->writer = (*threadnow)();
373 unlock(&l->l);
374 return 1;
376 if(!block){
377 unlock(&l->l);
378 return 0;
380 addthread(&l->wwaiting, (*threadnow)());
381 unlock(&l->l);
382 _threadswitch();
383 return 1;
386 static void
387 threadrunlock(RWLock *l, ulong pc)
389 _Thread *t;
391 USED(pc);
392 lock(&l->l);
393 --l->readers;
394 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
395 delthread(&l->wwaiting, t);
396 l->writer = t;
397 _threadready(t);
399 unlock(&l->l);
402 static void
403 threadwunlock(RWLock *l, ulong pc)
405 _Thread *t;
407 USED(pc);
408 lock(&l->l);
409 l->writer = nil;
410 assert(l->readers == 0);
411 while((t = l->rwaiting.head) != nil){
412 delthread(&l->rwaiting, t);
413 l->readers++;
414 _threadready(t);
416 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
417 delthread(&l->wwaiting, t);
418 l->writer = t;
419 _threadready(t);
421 unlock(&l->l);
424 /*
425 * sleep and wakeup
426 */
427 static void
428 threadrsleep(Rendez *r, ulong pc)
430 addthread(&r->waiting, proc()->thread);
431 qunlock(r->l);
432 _threadswitch();
433 qlock(r->l);
436 static int
437 threadrwakeup(Rendez *r, int all, ulong pc)
439 int i;
440 _Thread *t;
442 for(i=0;; i++){
443 if(i==1 && !all)
444 break;
445 if((t = r->waiting.head) == nil)
446 break;
447 delthread(&r->waiting, t);
448 _threadready(t);
450 return i;
453 /*
454 * startup
455 */
457 static int threadargc;
458 static char **threadargv;
459 int mainstacksize;
461 static void
462 threadmainstart(void *v)
464 USED(v);
465 threadmainproc = proc();
466 threadmain(threadargc, threadargv);
469 void
470 threadlinklibrary(void)
474 int
475 main(int argc, char **argv)
477 Proc *p;
479 argv0 = argv[0];
481 _threadsetupdaemonize();
483 threadargc = argc;
484 threadargv = argv;
486 /*
487 * Install locking routines into C library.
488 */
489 _lock = _threadlock;
490 _unlock = _threadunlock;
491 _qlock = threadqlock;
492 _qunlock = threadqunlock;
493 _rlock = threadrlock;
494 _runlock = threadrunlock;
495 _wlock = threadwlock;
496 _wunlock = threadwunlock;
497 _rsleep = threadrsleep;
498 _rwakeup = threadrwakeup;
499 _notejmpbuf = threadnotejmp;
501 _pthreadinit();
502 p = procalloc();
503 _threadsetproc(p);
504 if(mainstacksize == 0)
505 mainstacksize = 65536;
506 _threadcreate(p, threadmainstart, nil, mainstacksize);
507 scheduler(p);
508 threaddaemonize();
509 _threadpexit();
512 /*
513 * hooray for linked lists
514 */
515 static void
516 addthread(_Threadlist *l, _Thread *t)
518 if(l->tail){
519 l->tail->next = t;
520 t->prev = l->tail;
521 }else{
522 l->head = t;
523 t->prev = nil;
525 l->tail = t;
526 t->next = nil;
529 static void
530 delthread(_Threadlist *l, _Thread *t)
532 if(t->prev)
533 t->prev->next = t->next;
534 else
535 l->head = t->next;
536 if(t->next)
537 t->next->prev = t->prev;
538 else
539 l->tail = t->prev;
542 static void
543 addthreadinproc(Proc *p, _Thread *t)
545 _Threadlist *l;
547 l = &p->allthreads;
548 if(l->tail){
549 l->tail->allnext = t;
550 t->allprev = l->tail;
551 }else{
552 l->head = t;
553 t->allprev = nil;
555 l->tail = t;
556 t->allnext = nil;
559 static void
560 delthreadinproc(Proc *p, _Thread *t)
562 _Threadlist *l;
564 l = &p->allthreads;
565 if(t->allprev)
566 t->allprev->allnext = t->allnext;
567 else
568 l->head = t->allnext;
569 if(t->allnext)
570 t->allnext->allprev = t->allprev;
571 else
572 l->tail = t->allprev;
575 Proc *_threadprocs;
576 Lock _threadprocslock;
577 static Proc *_threadprocstail;
579 static void
580 addproc(Proc *p)
582 lock(&_threadprocslock);
583 if(_threadprocstail){
584 _threadprocstail->next = p;
585 p->prev = _threadprocstail;
586 }else{
587 _threadprocs = p;
588 p->prev = nil;
590 _threadprocstail = p;
591 p->next = nil;
592 unlock(&_threadprocslock);
595 static void
596 delproc(Proc *p)
598 lock(&_threadprocslock);
599 if(p->prev)
600 p->prev->next = p->next;
601 else
602 _threadprocs = p->next;
603 if(p->next)
604 p->next->prev = p->prev;
605 else
606 _threadprocstail = p->prev;
607 unlock(&_threadprocslock);