Blob


1 #include "u.h"
2 #include "libc.h"
3 #include "thread.h"
4 #include "threadimpl.h"
6 int _threaddebuglevel;
8 static uint threadnproc;
9 static uint threadnsysproc;
10 static Lock threadnproclock;
11 static Ref threadidref;
12 static Proc *threadmainproc;
14 static void addproc(Proc*);
15 static void delproc(Proc*);
16 static void addthread(_Threadlist*, _Thread*);
17 static void delthread(_Threadlist*, _Thread*);
18 static void addthreadinproc(Proc*, _Thread*);
19 static void delthreadinproc(Proc*, _Thread*);
20 static void contextswitch(Context *from, Context *to);
21 static void scheduler(Proc*);
23 static void
24 _threaddebug(char *fmt, ...)
25 {
26 va_list arg;
27 char buf[128];
28 _Thread *t;
30 return;
32 va_start(arg, fmt);
33 vsnprint(buf, sizeof buf, fmt, arg);
34 va_end(arg);
35 t = proc()->thread;
36 if(t)
37 fprint(2, "%d.%d: %s\n", getpid(), t->id, buf);
38 else
39 fprint(2, "%d._: %s\n", getpid(), buf);
40 }
42 static _Thread*
43 getthreadnow(void)
44 {
45 return proc()->thread;
46 }
47 _Thread *(*threadnow)(void) = getthreadnow;
49 static Proc*
50 procalloc(void)
51 {
52 Proc *p;
54 p = malloc(sizeof *p);
55 if(p == nil)
56 sysfatal("procalloc malloc: %r");
57 memset(p, 0, sizeof *p);
58 addproc(p);
59 lock(&threadnproclock);
60 threadnproc++;
61 unlock(&threadnproclock);
62 return p;
63 }
65 static void
66 threadstart(void *v)
67 {
68 _Thread *t;
70 t = v;
71 t->startfn(t->startarg);
72 memset(&v, 0xff, 32); /* try to cut off stack traces */
73 threadexits(nil);
74 }
76 static _Thread*
77 threadalloc(void (*fn)(void*), void *arg, uint stack)
78 {
79 _Thread *t;
80 sigset_t zero;
82 /* allocate the task and stack together */
83 t = malloc(sizeof *t+stack);
84 if(t == nil)
85 sysfatal("threadalloc malloc: %r");
86 memset(t, 0, sizeof *t);
87 t->stk = (uchar*)(t+1);
88 t->stksize = stack;
89 t->id = incref(&threadidref);
90 t->startfn = fn;
91 t->startarg = arg;
93 /* do a reasonable initialization */
94 memset(&t->context.uc, 0, sizeof t->context.uc);
95 sigemptyset(&zero);
96 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
98 /* on Linux makecontext neglects floating point */
99 getcontext(&t->context.uc);
101 /* call makecontext to do the real work. */
102 /* leave a few words open on both ends */
103 t->context.uc.uc_stack.ss_sp = t->stk+8;
104 t->context.uc.uc_stack.ss_size = t->stksize-64;
105 makecontext(&t->context.uc, (void(*)())threadstart, 1, t);
107 return t;
110 _Thread*
111 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
113 _Thread *t;
115 t = threadalloc(fn, arg, stack);
116 t->proc = p;
117 addthreadinproc(p, t);
118 p->nthread++;
119 _threadready(t);
120 return t;
123 int
124 threadcreate(void (*fn)(void*), void *arg, uint stack)
126 _Thread *t;
128 t = _threadcreate(proc(), fn, arg, stack);
129 return t->id;
132 int
133 proccreate(void (*fn)(void*), void *arg, uint stack)
135 _Thread *t;
136 Proc *p;
138 p = procalloc();
139 t = _threadcreate(p, fn, arg, stack);
140 _procstart(p, scheduler);
141 return t->id;
144 void
145 _threadswitch(void)
147 Proc *p;
149 p = proc();
150 contextswitch(&p->thread->context, &p->schedcontext);
153 void
154 _threadready(_Thread *t)
156 Proc *p;
158 p = t->proc;
159 lock(&p->lock);
160 addthread(&p->runqueue, t);
161 //print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid);
162 if(p != proc())
163 _procwakeup(&p->runrend);
164 unlock(&p->lock);
167 int
168 threadyield(void)
170 int n;
171 Proc *p;
173 p = proc();
174 n = p->nswitch;
175 _threadready(p->thread);
176 _threadswitch();
177 return p->nswitch - n;
180 void
181 threadexits(char *msg)
183 Proc *p;
185 p = proc();
186 if(msg == nil)
187 msg = "";
188 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
189 proc()->thread->exiting = 1;
190 _threadswitch();
193 static void
194 contextswitch(Context *from, Context *to)
196 if(swapcontext(&from->uc, &to->uc) < 0){
197 fprint(2, "swapcontext failed: %r\n");
198 assert(0);
202 static void
203 scheduler(Proc *p)
205 _Thread *t;
207 setproc(p);
208 _threaddebug("scheduler enter");
209 // print("s %p %d\n", p, gettid());
210 lock(&p->lock);
211 for(;;){
212 while((t = p->runqueue.head) == nil){
213 if(p->nthread == 0)
214 goto Out;
215 p->runrend.l = &p->lock;
216 _threaddebug("scheduler sleep");
217 _procsleep(&p->runrend);
218 _threaddebug("scheduler wake");
220 delthread(&p->runqueue, t);
221 unlock(&p->lock);
222 p->thread = t;
223 p->nswitch++;
224 _threaddebug("run %d (%s)", t->id, t->name);
225 contextswitch(&p->schedcontext, &t->context);
226 p->thread = nil;
227 lock(&p->lock);
228 if(t->exiting){
229 delthreadinproc(p, t);
230 p->nthread--;
231 free(t);
235 Out:
236 _threaddebug("scheduler exit");
237 delproc(p);
238 lock(&threadnproclock);
239 if(p->sysproc)
240 --threadnsysproc;
241 if(--threadnproc == threadnsysproc)
242 threadexitsall(p->msg);
243 unlock(&threadnproclock);
244 unlock(&p->lock);
245 free(p);
246 setproc(0);
249 void
250 _threadsetsysproc(void)
252 lock(&threadnproclock);
253 if(++threadnsysproc == threadnproc)
254 exit(0);
255 unlock(&threadnproclock);
256 proc()->sysproc = 1;
259 void**
260 procdata(void)
262 return &proc()->udata;
265 extern Jmp *(*_notejmpbuf)(void);
266 static Jmp*
267 threadnotejmp(void)
269 return &proc()->sigjmp;
272 /*
273 * debugging
274 */
275 void
276 threadsetname(char *fmt, ...)
278 va_list arg;
279 _Thread *t;
281 t = proc()->thread;
282 va_start(arg, fmt);
283 vsnprint(t->name, sizeof t->name, fmt, arg);
284 va_end(arg);
287 void
288 threadsetstate(char *fmt, ...)
290 va_list arg;
291 _Thread *t;
293 t = proc()->thread;
294 va_start(arg, fmt);
295 vsnprint(t->state, sizeof t->name, fmt, arg);
296 va_end(arg);
299 /*
300 * locking
301 */
302 static int
303 threadqlock(QLock *l, int block, ulong pc)
305 lock(&l->l);
306 if(l->owner == nil){
307 l->owner = (*threadnow)();
308 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
309 unlock(&l->l);
310 return 1;
312 if(!block){
313 unlock(&l->l);
314 return 0;
316 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
317 addthread(&l->waiting, (*threadnow)());
318 unlock(&l->l);
320 _threadswitch();
322 if(l->owner != (*threadnow)()){
323 fprint(2, "qlock pc=0x%lux owner=%p self=%p oops\n", pc, l->owner, (*threadnow)());
324 abort();
326 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
327 return 1;
330 static void
331 threadqunlock(QLock *l, ulong pc)
333 lock(&l->l);
334 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
335 if(l->owner == nil){
336 fprint(2, "qunlock pc=0x%lux owner=%p self=%p oops\n",
337 pc, l->owner, (*threadnow)());
338 abort();
340 if((l->owner = l->waiting.head) != nil){
341 delthread(&l->waiting, l->owner);
342 _threadready(l->owner);
344 unlock(&l->l);
347 static int
348 threadrlock(RWLock *l, int block, ulong pc)
350 USED(pc);
352 lock(&l->l);
353 if(l->writer == nil && l->wwaiting.head == nil){
354 l->readers++;
355 unlock(&l->l);
356 return 1;
358 if(!block){
359 unlock(&l->l);
360 return 0;
362 addthread(&l->rwaiting, (*threadnow)());
363 unlock(&l->l);
364 _threadswitch();
365 return 1;
368 static int
369 threadwlock(RWLock *l, int block, ulong pc)
371 USED(pc);
373 lock(&l->l);
374 if(l->writer == nil && l->readers == 0){
375 l->writer = (*threadnow)();
376 unlock(&l->l);
377 return 1;
379 if(!block){
380 unlock(&l->l);
381 return 0;
383 addthread(&l->wwaiting, (*threadnow)());
384 unlock(&l->l);
385 _threadswitch();
386 return 1;
389 static void
390 threadrunlock(RWLock *l, ulong pc)
392 _Thread *t;
394 USED(pc);
395 lock(&l->l);
396 --l->readers;
397 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
398 delthread(&l->wwaiting, t);
399 l->writer = t;
400 _threadready(t);
402 unlock(&l->l);
405 static void
406 threadwunlock(RWLock *l, ulong pc)
408 _Thread *t;
410 USED(pc);
411 lock(&l->l);
412 l->writer = nil;
413 assert(l->readers == 0);
414 while((t = l->rwaiting.head) != nil){
415 delthread(&l->rwaiting, t);
416 l->readers++;
417 _threadready(t);
419 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
420 delthread(&l->wwaiting, t);
421 l->writer = t;
422 _threadready(t);
424 unlock(&l->l);
427 /*
428 * sleep and wakeup
429 */
430 static void
431 threadrsleep(Rendez *r, ulong pc)
433 addthread(&r->waiting, proc()->thread);
434 qunlock(r->l);
435 _threadswitch();
436 qlock(r->l);
439 static int
440 threadrwakeup(Rendez *r, int all, ulong pc)
442 int i;
443 _Thread *t;
445 for(i=0;; i++){
446 if(i==1 && !all)
447 break;
448 if((t = r->waiting.head) == nil)
449 break;
450 delthread(&r->waiting, t);
451 _threadready(t);
453 return i;
456 /*
457 * startup
458 */
460 static int threadargc;
461 static char **threadargv;
462 int mainstacksize;
464 static void
465 threadmainstart(void *v)
467 USED(v);
468 threadmainproc = proc();
469 threadmain(threadargc, threadargv);
472 void
473 threadlinklibrary(void)
477 int
478 main(int argc, char **argv)
480 Proc *p;
482 _threadsetupdaemonize();
484 threadargc = argc;
485 threadargv = argv;
487 /*
488 * Install locking routines into C library.
489 */
490 _lock = _threadlock;
491 _unlock = _threadunlock;
492 _qlock = threadqlock;
493 _qunlock = threadqunlock;
494 _rlock = threadrlock;
495 _runlock = threadrunlock;
496 _wlock = threadwlock;
497 _wunlock = threadwunlock;
498 _rsleep = threadrsleep;
499 _rwakeup = threadrwakeup;
500 _notejmpbuf = threadnotejmp;
502 _pthreadinit();
503 p = procalloc();
504 _threadsetproc(p);
505 if(mainstacksize == 0)
506 mainstacksize = 65536;
507 _threadcreate(p, threadmainstart, nil, mainstacksize);
508 scheduler(p);
509 return 0; /* not reached */
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);