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 _Thread*
24 getthreadnow(void)
25 {
26 return proc()->thread;
27 }
28 _Thread *(*threadnow)(void) = getthreadnow;
30 static Proc*
31 procalloc(void)
32 {
33 Proc *p;
35 p = malloc(sizeof *p);
36 if(p == nil)
37 sysfatal("procalloc malloc: %r");
38 memset(p, 0, sizeof *p);
39 addproc(p);
40 lock(&threadnproclock);
41 threadnproc++;
42 unlock(&threadnproclock);
43 return p;
44 }
46 static void
47 threadstart(void *v)
48 {
49 _Thread *t;
51 t = v;
52 t->startfn(t->startarg);
53 threadexits(nil);
54 }
56 static _Thread*
57 threadalloc(void (*fn)(void*), void *arg, uint stack)
58 {
59 _Thread *t;
60 sigset_t zero;
62 /* allocate the task and stack together */
63 t = malloc(sizeof *t+stack);
64 if(t == nil)
65 sysfatal("threadalloc malloc: %r");
66 memset(t, 0, sizeof *t);
67 t->stk = (uchar*)(t+1);
68 t->stksize = stack;
69 t->id = incref(&threadidref);
70 t->startfn = fn;
71 t->startarg = arg;
73 /* do a reasonable initialization */
74 memset(&t->context.uc, 0, sizeof t->context.uc);
75 sigemptyset(&zero);
76 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
78 /* on Linux makecontext neglects floating point */
79 getcontext(&t->context.uc);
81 /* call makecontext to do the real work. */
82 /* leave a few words open on both ends */
83 t->context.uc.uc_stack.ss_sp = t->stk+8;
84 t->context.uc.uc_stack.ss_size = t->stksize-16;
85 makecontext(&t->context.uc, (void(*)())threadstart, 1, t);
87 return t;
88 }
90 _Thread*
91 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
92 {
93 _Thread *t;
95 t = threadalloc(fn, arg, stack);
96 t->proc = p;
97 addthreadinproc(p, t);
98 p->nthread++;
99 _threadready(t);
100 return t;
103 int
104 threadcreate(void (*fn)(void*), void *arg, uint stack)
106 _Thread *t;
108 t = _threadcreate(proc(), fn, arg, stack);
109 return t->id;
112 int
113 proccreate(void (*fn)(void*), void *arg, uint stack)
115 _Thread *t;
116 Proc *p;
118 p = procalloc();
119 t = _threadcreate(p, fn, arg, stack);
120 _procstart(p, scheduler);
121 return t->id;
124 void
125 _threadswitch(void)
127 Proc *p;
129 p = proc();
130 contextswitch(&p->thread->context, &p->schedcontext);
133 void
134 _threadready(_Thread *t)
136 Proc *p;
138 p = t->proc;
139 lock(&p->lock);
140 addthread(&p->runqueue, t);
141 _procwakeup(&p->runrend);
142 unlock(&p->lock);
145 void
146 threadyield(void)
148 _threadready(proc()->thread);
149 _threadswitch();
152 void
153 threadexits(char *msg)
155 Proc *p;
157 p = proc();
158 if(msg == nil)
159 msg = "";
160 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
161 proc()->thread->exiting = 1;
162 _threadswitch();
165 static void
166 contextswitch(Context *from, Context *to)
168 if(swapcontext(&from->uc, &to->uc) < 0){
169 fprint(2, "swapcontext failed: %r\n");
170 assert(0);
174 static void
175 scheduler(Proc *p)
177 _Thread *t;
179 setproc(p);
180 // print("s %p %d\n", p, gettid());
181 lock(&p->lock);
182 for(;;){
183 while((t = p->runqueue.head) == nil){
184 if(p->nthread == 0)
185 goto Out;
186 p->runrend.l = &p->lock;
187 _procsleep(&p->runrend);
189 delthread(&p->runqueue, t);
190 unlock(&p->lock);
191 p->thread = t;
192 // print("run %s %d\n", t->name, t->id);
193 contextswitch(&p->schedcontext, &t->context);
194 p->thread = nil;
195 lock(&p->lock);
196 if(t->exiting){
197 delthreadinproc(p, t);
198 p->nthread--;
199 free(t);
203 Out:
204 delproc(p);
205 lock(&threadnproclock);
206 if(p->sysproc)
207 --threadnsysproc;
208 if(--threadnproc == threadnsysproc)
209 threadexitsall(p->msg);
210 unlock(&threadnproclock);
211 unlock(&p->lock);
212 free(p);
213 setproc(0);
216 void
217 _threadsetsysproc(void)
219 lock(&threadnproclock);
220 if(++threadnsysproc == threadnproc)
221 exit(0);
222 unlock(&threadnproclock);
223 proc()->sysproc = 1;
226 void**
227 procdata(void)
229 return &proc()->udata;
232 extern Jmp *(*_notejmpbuf)(void);
233 static Jmp*
234 threadnotejmp(void)
236 return &proc()->sigjmp;
239 /*
240 * debugging
241 */
242 void
243 threadsetname(char *fmt, ...)
245 va_list arg;
246 _Thread *t;
248 t = proc()->thread;
249 va_start(arg, fmt);
250 vsnprint(t->name, sizeof t->name, fmt, arg);
251 va_end(arg);
254 void
255 threadsetstate(char *fmt, ...)
257 va_list arg;
258 _Thread *t;
260 t = proc()->thread;
261 va_start(arg, fmt);
262 vsnprint(t->state, sizeof t->name, fmt, arg);
263 va_end(arg);
266 /*
267 * locking
268 */
269 static int
270 threadqlock(QLock *l, int block, ulong pc)
272 lock(&l->l);
273 if(l->owner == nil){
274 l->owner = (*threadnow)();
275 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
276 unlock(&l->l);
277 return 1;
279 if(!block){
280 unlock(&l->l);
281 return 0;
283 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
284 addthread(&l->waiting, (*threadnow)());
285 unlock(&l->l);
287 _threadswitch();
289 if(l->owner != (*threadnow)()){
290 fprint(2, "qlock pc=0x%lux owner=%p self=%p oops\n", pc, l->owner, (*threadnow)());
291 abort();
293 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
294 return 1;
297 static void
298 threadqunlock(QLock *l, ulong pc)
300 lock(&l->l);
301 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
302 if(l->owner == nil){
303 fprint(2, "qunlock pc=0x%lux owner=%p self=%p oops\n",
304 pc, l->owner, (*threadnow)());
305 abort();
307 if((l->owner = l->waiting.head) != nil){
308 delthread(&l->waiting, l->owner);
309 _threadready(l->owner);
311 unlock(&l->l);
314 static int
315 threadrlock(RWLock *l, int block, ulong pc)
317 USED(pc);
319 lock(&l->l);
320 if(l->writer == nil && l->wwaiting.head == nil){
321 l->readers++;
322 unlock(&l->l);
323 return 1;
325 if(!block){
326 unlock(&l->l);
327 return 0;
329 addthread(&l->rwaiting, (*threadnow)());
330 unlock(&l->l);
331 _threadswitch();
332 return 1;
335 static int
336 threadwlock(RWLock *l, int block, ulong pc)
338 USED(pc);
340 lock(&l->l);
341 if(l->writer == nil && l->readers == 0){
342 l->writer = (*threadnow)();
343 unlock(&l->l);
344 return 1;
346 if(!block){
347 unlock(&l->l);
348 return 0;
350 addthread(&l->wwaiting, (*threadnow)());
351 unlock(&l->l);
352 _threadswitch();
353 return 1;
356 static void
357 threadrunlock(RWLock *l, ulong pc)
359 _Thread *t;
361 USED(pc);
362 lock(&l->l);
363 --l->readers;
364 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
365 delthread(&l->wwaiting, t);
366 l->writer = t;
367 _threadready(t);
369 unlock(&l->l);
372 static void
373 threadwunlock(RWLock *l, ulong pc)
375 _Thread *t;
377 USED(pc);
378 lock(&l->l);
379 l->writer = nil;
380 assert(l->readers == 0);
381 while((t = l->rwaiting.head) != nil){
382 delthread(&l->rwaiting, t);
383 l->readers++;
384 _threadready(t);
386 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
387 delthread(&l->wwaiting, t);
388 l->writer = t;
389 _threadready(t);
391 unlock(&l->l);
394 /*
395 * sleep and wakeup
396 */
397 static void
398 threadrsleep(Rendez *r, ulong pc)
400 addthread(&r->waiting, proc()->thread);
401 qunlock(r->l);
402 _threadswitch();
403 qlock(r->l);
406 static int
407 threadrwakeup(Rendez *r, int all, ulong pc)
409 int i;
410 _Thread *t;
412 for(i=0;; i++){
413 if(i==1 && !all)
414 break;
415 if((t = r->waiting.head) == nil)
416 break;
417 delthread(&r->waiting, t);
418 _threadready(t);
420 return i;
423 /*
424 * startup
425 */
427 static int threadargc;
428 static char **threadargv;
429 int mainstacksize;
431 static void
432 threadmainstart(void *v)
434 USED(v);
435 threadmainproc = proc();
436 threadmain(threadargc, threadargv);
439 int
440 main(int argc, char **argv)
442 Proc *p;
444 threadargc = argc;
445 threadargv = argv;
447 /*
448 * Install locking routines into C library.
449 */
450 _lock = _threadlock;
451 _unlock = _threadunlock;
452 _qlock = threadqlock;
453 _qunlock = threadqunlock;
454 _rlock = threadrlock;
455 _runlock = threadrunlock;
456 _wlock = threadwlock;
457 _wunlock = threadwunlock;
458 _rsleep = threadrsleep;
459 _rwakeup = threadrwakeup;
460 _notejmpbuf = threadnotejmp;
462 _pthreadinit();
463 p = procalloc();
464 _threadsetproc(p);
465 if(mainstacksize == 0)
466 mainstacksize = 65536;
467 _threadcreate(p, threadmainstart, nil, mainstacksize);
468 scheduler(p);
469 return 0; /* not reached */
472 /*
473 * hooray for linked lists
474 */
475 static void
476 addthread(_Threadlist *l, _Thread *t)
478 if(l->tail){
479 l->tail->next = t;
480 t->prev = l->tail;
481 }else{
482 l->head = t;
483 t->prev = nil;
485 l->tail = t;
486 t->next = nil;
489 static void
490 delthread(_Threadlist *l, _Thread *t)
492 if(t->prev)
493 t->prev->next = t->next;
494 else
495 l->head = t->next;
496 if(t->next)
497 t->next->prev = t->prev;
498 else
499 l->tail = t->prev;
502 static void
503 addthreadinproc(Proc *p, _Thread *t)
505 _Threadlist *l;
507 l = &p->allthreads;
508 if(l->tail){
509 l->tail->allnext = t;
510 t->allprev = l->tail;
511 }else{
512 l->head = t;
513 t->allprev = nil;
515 l->tail = t;
516 t->allnext = nil;
519 static void
520 delthreadinproc(Proc *p, _Thread *t)
522 _Threadlist *l;
524 l = &p->allthreads;
525 if(t->allprev)
526 t->allprev->allnext = t->allnext;
527 else
528 l->head = t->allnext;
529 if(t->allnext)
530 t->allnext->allprev = t->allprev;
531 else
532 l->tail = t->allprev;
535 Proc *_threadprocs;
536 Lock _threadprocslock;
537 static Proc *_threadprocstail;
539 static void
540 addproc(Proc *p)
542 lock(&_threadprocslock);
543 if(_threadprocstail){
544 _threadprocstail->next = p;
545 p->prev = _threadprocstail;
546 }else{
547 _threadprocs = p;
548 p->prev = nil;
550 _threadprocstail = p;
551 p->next = nil;
552 unlock(&_threadprocslock);
555 static void
556 delproc(Proc *p)
558 lock(&_threadprocslock);
559 if(p->prev)
560 p->prev->next = p->next;
561 else
562 _threadprocs = p->next;
563 if(p->next)
564 p->next->prev = p->prev;
565 else
566 _threadprocstail = p->prev;
567 unlock(&_threadprocslock);