tadd threadidle - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 615e0f9fb2df0e782f6b32ea9f0f67254e1ddeaf
 (DIR) parent 8b9a1d4cfefb87cb89835880d528ce8d8b5eecdc
 (HTM) Author: rsc <devnull@localhost>
       Date:   Tue,  7 Feb 2006 17:02:05 +0000
       
       add threadidle
       
       Diffstat:
         M include/thread.h                    |       1 +
         M src/libthread/thread.c              |      31 +++++++++++++++++++++++++++++--
         M src/libthread/threadimpl.h          |       1 +
       
       3 files changed, 31 insertions(+), 2 deletions(-)
       ---
 (DIR) diff --git a/include/thread.h b/include/thread.h
       t@@ -17,6 +17,7 @@ void                threadsetname(char*, ...);
        void                threadsetstate(char*, ...);
        char                *threadgetname(void);
        int                        threadyield(void);
       +int                        threadidle(void);
        void                _threadready(_Thread*);
        void                _threadswitch(void);
        void                _threadsetsysproc(void);
 (DIR) diff --git a/src/libthread/thread.c b/src/libthread/thread.c
       t@@ -208,6 +208,22 @@ _threadready(_Thread *t)
        }
        
        int
       +threadidle(void)
       +{
       +        int n;
       +        Proc *p;
       +        
       +        p = proc();
       +        n = p->nswitch;
       +        lock(&p->lock);
       +        p->runrend.l = &p->lock;
       +        addthread(&p->idlequeue, p->thread);
       +        unlock(&p->lock);
       +        _threadswitch();
       +        return p->nswitch - n;
       +}
       +
       +int
        threadyield(void)
        {
                int n;
       t@@ -255,6 +271,16 @@ procscheduler(Proc *p)
                        while((t = p->runqueue.head) == nil){
                                if(p->nthread == 0)
                                        goto Out;
       +                        if((t = p->idlequeue.head) != nil){
       +                                /*
       +                                 * Run all the idling threads once.
       +                                 */
       +                                while((t = p->idlequeue.head) != nil){
       +                                        delthread(&p->idlequeue, t);
       +                                        addthread(&p->runqueue, t);
       +                                }
       +                                continue;
       +                        }
                                p->runrend.l = &p->lock;
                                _threaddebug("scheduler sleep");
                                _procsleep(&p->runrend);
       t@@ -272,7 +298,7 @@ procscheduler(Proc *p)
                        if(t->exiting){
                                delthreadinproc(p, t);
                                p->nthread--;
       -//print("ntrhead %d\n", p->nthread);
       +//print("nthread %d\n", p->nthread);
                                free(t);
                        }
                }
       t@@ -598,7 +624,8 @@ main(int argc, char **argv)
        
                argv0 = argv[0];
        
       -        _threadsetupdaemonize();
       +        if(getenv("NOLIBTHREADDAEMONIZE") == nil)
       +                _threadsetupdaemonize();
        
                threadargc = argc;
                threadargv = argv;
 (DIR) diff --git a/src/libthread/threadimpl.h b/src/libthread/threadimpl.h
       t@@ -126,6 +126,7 @@ struct Proc
                int                        nswitch;
                _Thread                *thread;
                _Threadlist        runqueue;
       +        _Threadlist        idlequeue;
                _Threadlist        allthreads;
                uint                nthread;
                uint                sysproc;