add threadidle
This commit is contained in:
parent
8b9a1d4cfe
commit
615e0f9fb2
3 changed files with 31 additions and 2 deletions
|
|
@ -17,6 +17,7 @@ void threadsetname(char*, ...);
|
||||||
void threadsetstate(char*, ...);
|
void threadsetstate(char*, ...);
|
||||||
char *threadgetname(void);
|
char *threadgetname(void);
|
||||||
int threadyield(void);
|
int threadyield(void);
|
||||||
|
int threadidle(void);
|
||||||
void _threadready(_Thread*);
|
void _threadready(_Thread*);
|
||||||
void _threadswitch(void);
|
void _threadswitch(void);
|
||||||
void _threadsetsysproc(void);
|
void _threadsetsysproc(void);
|
||||||
|
|
|
||||||
|
|
@ -207,6 +207,22 @@ _threadready(_Thread *t)
|
||||||
unlock(&p->lock);
|
unlock(&p->lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
int
|
||||||
threadyield(void)
|
threadyield(void)
|
||||||
{
|
{
|
||||||
|
|
@ -255,6 +271,16 @@ procscheduler(Proc *p)
|
||||||
while((t = p->runqueue.head) == nil){
|
while((t = p->runqueue.head) == nil){
|
||||||
if(p->nthread == 0)
|
if(p->nthread == 0)
|
||||||
goto Out;
|
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;
|
p->runrend.l = &p->lock;
|
||||||
_threaddebug("scheduler sleep");
|
_threaddebug("scheduler sleep");
|
||||||
_procsleep(&p->runrend);
|
_procsleep(&p->runrend);
|
||||||
|
|
@ -272,7 +298,7 @@ procscheduler(Proc *p)
|
||||||
if(t->exiting){
|
if(t->exiting){
|
||||||
delthreadinproc(p, t);
|
delthreadinproc(p, t);
|
||||||
p->nthread--;
|
p->nthread--;
|
||||||
//print("ntrhead %d\n", p->nthread);
|
//print("nthread %d\n", p->nthread);
|
||||||
free(t);
|
free(t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -598,7 +624,8 @@ main(int argc, char **argv)
|
||||||
|
|
||||||
argv0 = argv[0];
|
argv0 = argv[0];
|
||||||
|
|
||||||
_threadsetupdaemonize();
|
if(getenv("NOLIBTHREADDAEMONIZE") == nil)
|
||||||
|
_threadsetupdaemonize();
|
||||||
|
|
||||||
threadargc = argc;
|
threadargc = argc;
|
||||||
threadargv = argv;
|
threadargv = argv;
|
||||||
|
|
|
||||||
|
|
@ -126,6 +126,7 @@ struct Proc
|
||||||
int nswitch;
|
int nswitch;
|
||||||
_Thread *thread;
|
_Thread *thread;
|
||||||
_Threadlist runqueue;
|
_Threadlist runqueue;
|
||||||
|
_Threadlist idlequeue;
|
||||||
_Threadlist allthreads;
|
_Threadlist allthreads;
|
||||||
uint nthread;
|
uint nthread;
|
||||||
uint sysproc;
|
uint sysproc;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue