From 4dbefdd41ca866a10cee633a3ee9a67d9204b052 Mon Sep 17 00:00:00 2001 From: rsc Date: Mon, 27 Dec 2004 16:52:26 +0000 Subject: start linux pre-2.6 port --- src/libthread/thread.c | 173 ++++++++++++++++++++++++++++--------------------- 1 file changed, 100 insertions(+), 73 deletions(-) (limited to 'src/libthread/thread.c') diff --git a/src/libthread/thread.c b/src/libthread/thread.c index 158f6e9c..4116ac22 100644 --- a/src/libthread/thread.c +++ b/src/libthread/thread.c @@ -1,18 +1,18 @@ #include "u.h" -#include #include "libc.h" #include "thread.h" #include "threadimpl.h" - _syscall0(pid_t,gettid) - int _threaddebuglevel; static uint threadnproc; static uint threadnsysproc; static Lock threadnproclock; static Ref threadidref; +static Proc *threadmainproc; +static void addproc(Proc*); +static void delproc(Proc*); static void addthread(_Threadlist*, _Thread*); static void delthread(_Threadlist*, _Thread*); static void addthreadinproc(Proc*, _Thread*); @@ -36,6 +36,7 @@ procalloc(void) if(p == nil) sysfatal("procalloc malloc: %r"); memset(p, 0, sizeof *p); + addproc(p); lock(&threadnproclock); threadnproc++; unlock(&threadnproclock); @@ -49,7 +50,7 @@ threadstart(void *v) t = v; t->startfn(t->startarg); - _threadexit(); + threadexits(nil); } static _Thread* @@ -115,9 +116,7 @@ proccreate(void (*fn)(void*), void *arg, uint stack) Proc *p; p = procalloc(); -//print("pa %p\n", p); t = _threadcreate(p, fn, arg, stack); -//print("ps %p\n", p); _procstart(p, scheduler); return t->id; } @@ -150,31 +149,17 @@ threadyield(void) _threadswitch(); } -void -_threadexit(void) -{ - proc()->thread->exiting = 1; - _threadswitch(); -} - void threadexits(char *msg) { -/* Proc *p; p = proc(); + if(msg == nil) + msg = ""; utfecpy(p->msg, p->msg+sizeof p->msg, msg); -*/ - _threadexit(); -} - -void -threadexitsall(char *msg) -{ - if(msg && msg[0]) - exit(1); - exit(0); + proc()->thread->exiting = 1; + _threadswitch(); } static void @@ -216,11 +201,12 @@ scheduler(Proc *p) } Out: + delproc(p); lock(&threadnproclock); if(p->sysproc) --threadnsysproc; if(--threadnproc == threadnsysproc) - exit(0); + threadexitsall(p->msg); unlock(&threadnproclock); unlock(&p->lock); free(p); @@ -237,6 +223,19 @@ _threadsetsysproc(void) proc()->sysproc = 1; } +void** +procdata(void) +{ + return &proc()->udata; +} + +extern Jmp *(*_notejmpbuf)(void); +static Jmp* +threadnotejmp(void) +{ + return &proc()->sigjmp; +} + /* * debugging */ @@ -421,6 +420,55 @@ threadrwakeup(Rendez *r, int all, ulong pc) return i; } +/* + * startup + */ + +static int threadargc; +static char **threadargv; +int mainstacksize; + +static void +threadmainstart(void *v) +{ + USED(v); + threadmainproc = proc(); + threadmain(threadargc, threadargv); +} + +int +main(int argc, char **argv) +{ + Proc *p; + + threadargc = argc; + threadargv = argv; + + /* + * Install locking routines into C library. + */ + _lock = _threadlock; + _unlock = _threadunlock; + _qlock = threadqlock; + _qunlock = threadqunlock; + _rlock = threadrlock; + _runlock = threadrunlock; + _wlock = threadwlock; + _wunlock = threadwunlock; + _rsleep = threadrsleep; + _rwakeup = threadrwakeup; + _notejmpbuf = threadnotejmp; + + _pthreadinit(); + p = procalloc(); + _threadsetproc(p); + if(mainstacksize == 0) + mainstacksize = 65536; + _threadcreate(p, threadmainstart, nil, mainstacksize); + scheduler(p); + return 0; /* not reached */ +} + /* * hooray for linked lists */ @@ -484,58 +532,37 @@ delthreadinproc(Proc *p, _Thread *t) l->tail = t->allprev; } -void** -procdata(void) -{ - return &proc()->udata; -} - -static int threadargc; -static char **threadargv; -int mainstacksize; +Proc *_threadprocs; +Lock _threadprocslock; +static Proc *_threadprocstail; static void -threadmainstart(void *v) -{ - USED(v); - threadmain(threadargc, threadargv); -} - -extern Jmp *(*_notejmpbuf)(void); -static Jmp* -threadnotejmp(void) +addproc(Proc *p) { - return &proc()->sigjmp; + lock(&_threadprocslock); + if(_threadprocstail){ + _threadprocstail->next = p; + p->prev = _threadprocstail; + }else{ + _threadprocs = p; + p->prev = nil; + } + _threadprocstail = p; + p->next = nil; + unlock(&_threadprocslock); } -int -main(int argc, char **argv) +static void +delproc(Proc *p) { - Proc *p; - - threadargc = argc; - threadargv = argv; - - /* - * Install locking routines into C library. - */ - _lock = _threadlock; - _unlock = _threadunlock; - _qlock = threadqlock; - _qunlock = threadqunlock; - _rlock = threadrlock; - _runlock = threadrunlock; - _wlock = threadwlock; - _wunlock = threadwunlock; - _rsleep = threadrsleep; - _rwakeup = threadrwakeup; - _notejmpbuf = threadnotejmp; - - _pthreadinit(); - p = procalloc(); - if(mainstacksize == 0) - mainstacksize = 65536; - _threadcreate(p, threadmainstart, nil, mainstacksize); - scheduler(p); - return 0; /* not reached */ + lock(&_threadprocslock); + if(p->prev) + p->prev->next = p->next; + else + _threadprocs = p->next; + if(p->next) + p->next->prev = p->prev; + else + _threadprocstail = p->prev; + unlock(&_threadprocslock); } -- cgit v1.2.3