URI: 
       tconfine pthreads to pthread.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit 73722a8bbf80f47ea2df2a212516d1b857ffe29a
   DIR parent e317e37406f8597d42c1e37ab7fcc4f7f901b342
  HTML Author: rsc <devnull@localhost>
       Date:   Mon, 27 Dec 2004 03:49:03 +0000
       
       confine pthreads to pthread.c
       
       Diffstat:
         M src/libthread/channel.c             |       2 ++
         M src/libthread/pthread.c             |      34 ++++++++++++++++++++++++++-----
         M src/libthread/thread.c              |      13 ++++++-------
         M src/libthread/threadimpl.h          |       2 +-
       
       4 files changed, 38 insertions(+), 13 deletions(-)
       ---
   DIR diff --git a/src/libthread/channel.c b/src/libthread/channel.c
       t@@ -22,6 +22,8 @@ chancreate(int elemsize, int bufsize)
                Channel *c;
        
                c = malloc(sizeof *c+bufsize*elemsize);
       +        if(c == nil)
       +                sysfatal("chancreate malloc: %r");
                memset(c, 0, sizeof *c);
                c->elemsize = elemsize;
                c->bufsize = bufsize;
   DIR diff --git a/src/libthread/pthread.c b/src/libthread/pthread.c
       t@@ -71,16 +71,40 @@ _procwakeup(_Procrendez *r)
                }
        }
        
       +static void
       +startprocfn(void *v)
       +{
       +        void **a;
       +        void (*fn)(void*);
       +        Proc *p;
       +
       +        a = (void**)v;
       +        fn = a[0];
       +        p = a[1];
       +        free(a);
       +        p->tid = pthread_self();
       +        pthread_detach(p->tid);
       +
       +        (*fn)(p);
       +
       +        pthread_exit(0);
       +}
       +
        void
       -_procstart(Proc *p, void (*fn)(void*))
       +_procstart(Proc *p, void (*fn)(Proc*))
        {
       -//print("pc\n");
       -        if(pthread_create(&p->tid, nil, (void*(*)(void*))fn, p) < 0){
       -//print("pc1\n");
       +        void **a;
       +
       +        a = malloc(2*sizeof a[0]);
       +        if(a == nil)
       +                sysfatal("_procstart malloc: %r");
       +        a[0] = fn;
       +        a[1] = p;
       +
       +        if(pthread_create(&p->tid, nil, (void*(*)(void*))startprocfn, (void*)a) < 0){
                        fprint(2, "pthread_create: %r\n");
                        abort();
                }
       -//print("pc2\n");
        }
        
        static pthread_key_t prockey;
   DIR diff --git a/src/libthread/thread.c b/src/libthread/thread.c
       t@@ -18,7 +18,7 @@ static        void                delthread(_Threadlist*, _Thread*);
        static        void                addthreadinproc(Proc*, _Thread*);
        static        void                delthreadinproc(Proc*, _Thread*);
        static        void                contextswitch(Context *from, Context *to);
       -static        void                scheduler(void*);
       +static        void                scheduler(Proc*);
        
        static _Thread*
        getthreadnow(void)
       t@@ -33,6 +33,8 @@ procalloc(void)
                Proc *p;
        
                p = malloc(sizeof *p);
       +        if(p == nil)
       +                sysfatal("procalloc malloc: %r");
                memset(p, 0, sizeof *p);
                lock(&threadnproclock);
                threadnproc++;
       t@@ -58,6 +60,8 @@ threadalloc(void (*fn)(void*), void *arg, uint stack)
        
                /* allocate the task and stack together */
                t = malloc(sizeof *t+stack);
       +        if(t == nil)
       +                sysfatal("threadalloc malloc: %r");
                memset(t, 0, sizeof *t);
                t->stk = (uchar*)(t+1);
                t->stksize = stack;
       t@@ -183,16 +187,12 @@ contextswitch(Context *from, Context *to)
        }
        
        static void
       -scheduler(void *v)
       +scheduler(Proc *p)
        {
                _Thread *t;
       -        Proc *p;
        
       -        p = v;
                setproc(p);
                // print("s %p %d\n", p, gettid());
       -        p->tid = pthread_self();
       -        pthread_detach(p->tid);
                lock(&p->lock);
                for(;;){
                        while((t = p->runqueue.head) == nil){
       t@@ -225,7 +225,6 @@ Out:
                unlock(&p->lock);
                free(p);
                setproc(0);
       -        pthread_exit(nil);
        }
        
        void
   DIR diff --git a/src/libthread/threadimpl.h b/src/libthread/threadimpl.h
       t@@ -67,7 +67,7 @@ extern Proc *xxx;
        #define proc() _threadproc()
        #define setproc(p) _threadsetproc(p)
        
       -extern void _procstart(Proc*, void (*fn)(void*));
       +extern void _procstart(Proc*, void (*fn)(Proc*));
        extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
        extern void _threadexit(void);
        extern Proc *_threadproc(void);