Blob


1 #include "u.h"
2 #include <linux/unistd.h>
3 #include "libc.h"
4 #include "thread.h"
5 #include "threadimpl.h"
7 _syscall0(pid_t,gettid)
9 int _threaddebuglevel;
11 static uint threadnproc;
12 static uint threadnsysproc;
13 static Lock threadnproclock;
14 static Ref threadidref;
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(void*);
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 memset(p, 0, sizeof *p);
37 lock(&threadnproclock);
38 threadnproc++;
39 unlock(&threadnproclock);
40 return p;
41 }
43 static void
44 threadstart(void *v)
45 {
46 _Thread *t;
48 t = v;
49 t->startfn(t->startarg);
50 _threadexit();
51 }
53 static _Thread*
54 threadalloc(void (*fn)(void*), void *arg, uint stack)
55 {
56 _Thread *t;
57 sigset_t zero;
59 /* allocate the task and stack together */
60 t = malloc(sizeof *t+stack);
61 memset(t, 0, sizeof *t);
62 t->stk = (uchar*)(t+1);
63 t->stksize = stack;
64 t->id = incref(&threadidref);
65 t->startfn = fn;
66 t->startarg = arg;
68 /* do a reasonable initialization */
69 memset(&t->context.uc, 0, sizeof t->context.uc);
70 sigemptyset(&zero);
71 sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
73 /* on Linux makecontext neglects floating point */
74 getcontext(&t->context.uc);
76 /* call makecontext to do the real work. */
77 /* leave a few words open on both ends */
78 t->context.uc.uc_stack.ss_sp = t->stk+8;
79 t->context.uc.uc_stack.ss_size = t->stksize-16;
80 makecontext(&t->context.uc, (void(*)())threadstart, 1, t);
82 return t;
83 }
85 _Thread*
86 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
87 {
88 _Thread *t;
90 t = threadalloc(fn, arg, stack);
91 t->proc = p;
92 addthreadinproc(p, t);
93 p->nthread++;
94 _threadready(t);
95 return t;
96 }
98 int
99 threadcreate(void (*fn)(void*), void *arg, uint stack)
101 _Thread *t;
103 t = _threadcreate(proc(), fn, arg, stack);
104 return t->id;
107 int
108 proccreate(void (*fn)(void*), void *arg, uint stack)
110 _Thread *t;
111 Proc *p;
113 p = procalloc();
114 //print("pa %p\n", p);
115 t = _threadcreate(p, fn, arg, stack);
116 //print("ps %p\n", p);
117 _procstart(p, scheduler);
118 return t->id;
121 void
122 _threadswitch(void)
124 Proc *p;
126 p = proc();
127 contextswitch(&p->thread->context, &p->schedcontext);
130 void
131 _threadready(_Thread *t)
133 Proc *p;
135 p = t->proc;
136 lock(&p->lock);
137 addthread(&p->runqueue, t);
138 _procwakeup(&p->runrend);
139 unlock(&p->lock);
142 void
143 threadyield(void)
145 _threadready(proc()->thread);
146 _threadswitch();
149 void
150 _threadexit(void)
152 proc()->thread->exiting = 1;
153 _threadswitch();
156 void
157 threadexits(char *msg)
159 /*
160 Proc *p;
162 p = proc();
163 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
164 */
165 _threadexit();
168 void
169 threadexitsall(char *msg)
171 if(msg && msg[0])
172 exit(1);
173 exit(0);
176 static void
177 contextswitch(Context *from, Context *to)
179 if(swapcontext(&from->uc, &to->uc) < 0){
180 fprint(2, "swapcontext failed: %r\n");
181 assert(0);
185 static void
186 scheduler(void *v)
188 _Thread *t;
189 Proc *p;
191 p = v;
192 setproc(p);
193 print("s %p %d\n", p, gettid());
194 p->tid = pthread_self();
195 pthread_detach(p->tid);
196 lock(&p->lock);
197 for(;;){
198 while((t = p->runqueue.head) == nil){
199 if(p->nthread == 0)
200 goto Out;
201 p->runrend.l = &p->lock;
202 _procsleep(&p->runrend);
204 delthread(&p->runqueue, t);
205 unlock(&p->lock);
206 p->thread = t;
207 // print("run %s %d\n", t->name, t->id);
208 contextswitch(&p->schedcontext, &t->context);
209 p->thread = nil;
210 lock(&p->lock);
211 if(t->exiting){
212 delthreadinproc(p, t);
213 p->nthread--;
214 free(t);
218 Out:
219 lock(&threadnproclock);
220 if(p->sysproc)
221 --threadnsysproc;
222 if(--threadnproc == threadnsysproc)
223 exit(0);
224 unlock(&threadnproclock);
225 unlock(&p->lock);
226 free(p);
227 setproc(0);
228 print("e %p (tid %d)\n", p, gettid());
229 pthread_exit(nil);
232 void
233 _threadsetsysproc(void)
235 lock(&threadnproclock);
236 if(++threadnsysproc == threadnproc)
237 exit(0);
238 unlock(&threadnproclock);
239 proc()->sysproc = 1;
242 /*
243 * debugging
244 */
245 void
246 threadsetname(char *fmt, ...)
248 va_list arg;
249 _Thread *t;
251 t = proc()->thread;
252 va_start(arg, fmt);
253 vsnprint(t->name, sizeof t->name, fmt, arg);
254 va_end(arg);
257 void
258 threadsetstate(char *fmt, ...)
260 va_list arg;
261 _Thread *t;
263 t = proc()->thread;
264 va_start(arg, fmt);
265 vsnprint(t->state, sizeof t->name, fmt, arg);
266 va_end(arg);
269 /*
270 * locking
271 */
272 static int
273 threadqlock(QLock *l, int block, ulong pc)
275 lock(&l->l);
276 if(l->owner == nil){
277 l->owner = (*threadnow)();
278 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
279 unlock(&l->l);
280 return 1;
282 if(!block){
283 unlock(&l->l);
284 return 0;
286 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
287 addthread(&l->waiting, (*threadnow)());
288 unlock(&l->l);
290 _threadswitch();
292 if(l->owner != (*threadnow)()){
293 fprint(2, "qlock pc=0x%lux owner=%p self=%p oops\n", pc, l->owner, (*threadnow)());
294 abort();
296 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
297 return 1;
300 static void
301 threadqunlock(QLock *l, ulong pc)
303 lock(&l->l);
304 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
305 if(l->owner == nil){
306 fprint(2, "qunlock pc=0x%lux owner=%p self=%p oops\n",
307 pc, l->owner, (*threadnow)());
308 abort();
310 if((l->owner = l->waiting.head) != nil){
311 delthread(&l->waiting, l->owner);
312 _threadready(l->owner);
314 unlock(&l->l);
317 static int
318 threadrlock(RWLock *l, int block, ulong pc)
320 USED(pc);
322 lock(&l->l);
323 if(l->writer == nil && l->wwaiting.head == nil){
324 l->readers++;
325 unlock(&l->l);
326 return 1;
328 if(!block){
329 unlock(&l->l);
330 return 0;
332 addthread(&l->rwaiting, (*threadnow)());
333 unlock(&l->l);
334 _threadswitch();
335 return 1;
338 static int
339 threadwlock(RWLock *l, int block, ulong pc)
341 USED(pc);
343 lock(&l->l);
344 if(l->writer == nil && l->readers == 0){
345 l->writer = (*threadnow)();
346 unlock(&l->l);
347 return 1;
349 if(!block){
350 unlock(&l->l);
351 return 0;
353 addthread(&l->wwaiting, (*threadnow)());
354 unlock(&l->l);
355 _threadswitch();
356 return 1;
359 static void
360 threadrunlock(RWLock *l, ulong pc)
362 _Thread *t;
364 USED(pc);
365 lock(&l->l);
366 --l->readers;
367 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
368 delthread(&l->wwaiting, t);
369 l->writer = t;
370 _threadready(t);
372 unlock(&l->l);
375 static void
376 threadwunlock(RWLock *l, ulong pc)
378 _Thread *t;
380 USED(pc);
381 lock(&l->l);
382 l->writer = nil;
383 assert(l->readers == 0);
384 while((t = l->rwaiting.head) != nil){
385 delthread(&l->rwaiting, t);
386 l->readers++;
387 _threadready(t);
389 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
390 delthread(&l->wwaiting, t);
391 l->writer = t;
392 _threadready(t);
394 unlock(&l->l);
397 /*
398 * sleep and wakeup
399 */
400 static void
401 threadrsleep(Rendez *r, ulong pc)
403 addthread(&r->waiting, proc()->thread);
404 qunlock(r->l);
405 _threadswitch();
406 qlock(r->l);
409 static int
410 threadrwakeup(Rendez *r, int all, ulong pc)
412 int i;
413 _Thread *t;
415 for(i=0;; i++){
416 if(i==1 && !all)
417 break;
418 if((t = r->waiting.head) == nil)
419 break;
420 delthread(&r->waiting, t);
421 _threadready(t);
423 return i;
426 /*
427 * hooray for linked lists
428 */
429 static void
430 addthread(_Threadlist *l, _Thread *t)
432 if(l->tail){
433 l->tail->next = t;
434 t->prev = l->tail;
435 }else{
436 l->head = t;
437 t->prev = nil;
439 l->tail = t;
440 t->next = nil;
443 static void
444 delthread(_Threadlist *l, _Thread *t)
446 if(t->prev)
447 t->prev->next = t->next;
448 else
449 l->head = t->next;
450 if(t->next)
451 t->next->prev = t->prev;
452 else
453 l->tail = t->prev;
456 static void
457 addthreadinproc(Proc *p, _Thread *t)
459 _Threadlist *l;
461 l = &p->allthreads;
462 if(l->tail){
463 l->tail->allnext = t;
464 t->allprev = l->tail;
465 }else{
466 l->head = t;
467 t->allprev = nil;
469 l->tail = t;
470 t->allnext = nil;
473 static void
474 delthreadinproc(Proc *p, _Thread *t)
476 _Threadlist *l;
478 l = &p->allthreads;
479 if(t->allprev)
480 t->allprev->allnext = t->allnext;
481 else
482 l->head = t->allnext;
483 if(t->allnext)
484 t->allnext->allprev = t->allprev;
485 else
486 l->tail = t->allprev;
489 void**
490 procdata(void)
492 return &proc()->udata;
495 static int threadargc;
496 static char **threadargv;
497 int mainstacksize;
499 static void
500 threadmainstart(void *v)
502 USED(v);
503 threadmain(threadargc, threadargv);
506 int
507 main(int argc, char **argv)
509 Proc *p;
511 threadargc = argc;
512 threadargv = argv;
514 /*
515 * Install locking routines into C library.
516 */
517 _lock = _threadlock;
518 _unlock = _threadunlock;
519 _qlock = threadqlock;
520 _qunlock = threadqunlock;
521 _rlock = threadrlock;
522 _runlock = threadrunlock;
523 _wlock = threadwlock;
524 _wunlock = threadwunlock;
525 _rsleep = threadrsleep;
526 _rwakeup = threadrwakeup;
528 pthreadinit();
529 p = procalloc();
530 if(mainstacksize == 0)
531 mainstacksize = 65536;
532 _threadcreate(p, threadmainstart, nil, mainstacksize);
533 scheduler(p);
534 return 0; /* not reached */