2 #include <linux/unistd.h>
5 #include "threadimpl.h"
7 _syscall0(pid_t,gettid)
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*);
26 return proc()->thread;
28 _Thread *(*threadnow)(void) = getthreadnow;
35 p = malloc(sizeof *p);
36 memset(p, 0, sizeof *p);
37 lock(&threadnproclock);
39 unlock(&threadnproclock);
49 t->startfn(t->startarg);
54 threadalloc(void (*fn)(void*), void *arg, uint stack)
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);
64 t->id = incref(&threadidref);
68 /* do a reasonable initialization */
69 memset(&t->context.uc, 0, sizeof t->context.uc);
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);
86 _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
90 t = threadalloc(fn, arg, stack);
92 addthreadinproc(p, t);
99 threadcreate(void (*fn)(void*), void *arg, uint stack)
103 t = _threadcreate(proc(), fn, arg, stack);
108 proccreate(void (*fn)(void*), void *arg, uint stack)
114 //print("pa %p\n", p);
115 t = _threadcreate(p, fn, arg, stack);
116 //print("ps %p\n", p);
117 _procstart(p, scheduler);
127 contextswitch(&p->thread->context, &p->schedcontext);
131 _threadready(_Thread *t)
137 addthread(&p->runqueue, t);
138 _procwakeup(&p->runrend);
145 _threadready(proc()->thread);
152 proc()->thread->exiting = 1;
157 threadexits(char *msg)
163 utfecpy(p->msg, p->msg+sizeof p->msg, msg);
169 threadexitsall(char *msg)
177 contextswitch(Context *from, Context *to)
179 if(swapcontext(&from->uc, &to->uc) < 0){
180 fprint(2, "swapcontext failed: %r\n");
193 print("s %p %d\n", p, gettid());
194 p->tid = pthread_self();
195 pthread_detach(p->tid);
198 while((t = p->runqueue.head) == nil){
201 p->runrend.l = &p->lock;
202 _procsleep(&p->runrend);
204 delthread(&p->runqueue, t);
207 // print("run %s %d\n", t->name, t->id);
208 contextswitch(&p->schedcontext, &t->context);
212 delthreadinproc(p, t);
219 lock(&threadnproclock);
222 if(--threadnproc == threadnsysproc)
224 unlock(&threadnproclock);
228 print("e %p (tid %d)\n", p, gettid());
233 _threadsetsysproc(void)
235 lock(&threadnproclock);
236 if(++threadnsysproc == threadnproc)
238 unlock(&threadnproclock);
246 threadsetname(char *fmt, ...)
253 vsnprint(t->name, sizeof t->name, fmt, arg);
258 threadsetstate(char *fmt, ...)
265 vsnprint(t->state, sizeof t->name, fmt, arg);
273 threadqlock(QLock *l, int block, ulong pc)
277 l->owner = (*threadnow)();
278 //print("qlock %p @%#x by %p\n", l, pc, l->owner);
286 //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
287 addthread(&l->waiting, (*threadnow)());
292 if(l->owner != (*threadnow)()){
293 fprint(2, "qlock pc=0x%lux owner=%p self=%p oops\n", pc, l->owner, (*threadnow)());
296 //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
301 threadqunlock(QLock *l, ulong pc)
304 //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
306 fprint(2, "qunlock pc=0x%lux owner=%p self=%p oops\n",
307 pc, l->owner, (*threadnow)());
310 if((l->owner = l->waiting.head) != nil){
311 delthread(&l->waiting, l->owner);
312 _threadready(l->owner);
318 threadrlock(RWLock *l, int block, ulong pc)
323 if(l->writer == nil && l->wwaiting.head == nil){
332 addthread(&l->rwaiting, (*threadnow)());
339 threadwlock(RWLock *l, int block, ulong pc)
344 if(l->writer == nil && l->readers == 0){
345 l->writer = (*threadnow)();
353 addthread(&l->wwaiting, (*threadnow)());
360 threadrunlock(RWLock *l, ulong pc)
367 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
368 delthread(&l->wwaiting, t);
376 threadwunlock(RWLock *l, ulong pc)
383 assert(l->readers == 0);
384 while((t = l->rwaiting.head) != nil){
385 delthread(&l->rwaiting, t);
389 if(l->readers == 0 && (t = l->wwaiting.head) != nil){
390 delthread(&l->wwaiting, t);
401 threadrsleep(Rendez *r, ulong pc)
403 addthread(&r->waiting, proc()->thread);
410 threadrwakeup(Rendez *r, int all, ulong pc)
418 if((t = r->waiting.head) == nil)
420 delthread(&r->waiting, t);
427 * hooray for linked lists
430 addthread(_Threadlist *l, _Thread *t)
444 delthread(_Threadlist *l, _Thread *t)
447 t->prev->next = t->next;
451 t->next->prev = t->prev;
457 addthreadinproc(Proc *p, _Thread *t)
463 l->tail->allnext = t;
464 t->allprev = l->tail;
474 delthreadinproc(Proc *p, _Thread *t)
480 t->allprev->allnext = t->allnext;
482 l->head = t->allnext;
484 t->allnext->allprev = t->allprev;
486 l->tail = t->allprev;
492 return &proc()->udata;
495 static int threadargc;
496 static char **threadargv;
500 threadmainstart(void *v)
503 threadmain(threadargc, threadargv);
507 main(int argc, char **argv)
515 * Install locking routines into C library.
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;
530 if(mainstacksize == 0)
531 mainstacksize = 65536;
532 _threadcreate(p, threadmainstart, nil, mainstacksize);
534 return 0; /* not reached */