3 #include "threadimpl.h"
5 //static Thread *runthread(Proc*);
7 static char *_psstate[] = {
17 if(s < 0 || s >= nelem(_psstate))
27 extern void ignusr1(void), _threaddie(int);
29 signal(SIGTERM, _threaddie);
33 p->pid = _threadgetpid();
36 while(_setlabel(&p->sched))
38 _threaddebug(DBGSCHED, "top of schedinit, _threadexitsallstatus=%p", _threadexitsallstatus);
39 if(_threadexitsallstatus)
40 exits(_threadexitsallstatus);
42 if((t=p->thread) != nil){
46 fprint(2, "moribund %d\n", t->moribund);
47 assert(t->moribund == 1);
50 t->prevt->nextt = t->nextt;
52 p->threads.head = t->nextt;
54 t->nextt->prevt = t->prevt;
56 p->threads.tail = t->prevt;
64 free(t); /* XXX how do we know there are no references? */
71 t->ret = _schedexec(&p->exec);
76 t->ret = _schedfork(p->newproc);
78 //fprint(2, "_schedfork: %r\n");
83 t->state = t->nextstate;
100 if(p->nthreads==0 || (p->nthreads==1 && p->idle))
107 if((c = _threadwaitchan) != nil){
111 if((w = p->waitmsg) != nil)
118 unlock(&p->readylock);
121 if(nbsendp(c, w) != 1)
129 while((w = waitnohang()) != nil)
134 if(p->idle->state != Ready){
135 fprint(2, "everyone is asleep\n");
136 exits("everyone is asleep");
138 unlock(&p->readylock);
139 _threaddebug(DBGSCHED, "running idle thread", p->nthreads);
143 _threaddebug(DBGSCHED, "sleeping for more work (%d threads)", p->nthreads);
145 unlock(&p->readylock);
146 while(rendezvous((ulong)q, 0) == ~0){
147 if(_threadexitsallstatus)
148 exits(_threadexitsallstatus);
150 /* lock picked up from _threadready */
154 unlock(&p->readylock);
165 p = _threadgetproc();
166 //fprint(2, "p %p\n", p);
167 if((t = p->thread) != nil){
168 if((ulong)&p < (ulong)t->stk){ /* stack overflow */
169 fprint(2, "stack overflow %lux %lux\n", (ulong)&p, (ulong)t->stk);
172 // _threaddebug(DBGSCHED, "pausing, state=%s set %p goto %p",
173 // psstate(t->state), &t->sched, &p->sched);
174 if(_setlabel(&t->sched)==0)
175 _gotolabel(&p->sched);
180 _threaddebug(DBGSCHED, "all threads gone; exiting");
184 _threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
187 _threaddebug(DBGSCHED, "%d.%d marked to die");
191 t->nextstate = Ready;
192 _gotolabel(&t->sched);
202 p = _threadgetproc();
204 return (ulong)&p - (ulong)t->stk;
208 _threadready(Thread *t)
212 if(t == t->proc->idle){
213 _threaddebug(DBGSCHED, "idle thread is ready");
217 assert(t->state == Ready);
218 _threaddebug(DBGSCHED, "readying %d.%d", t->proc->pid, t->id);
220 lock(&t->proc->readylock);
228 assert(q->asleep == 1);
230 /* lock passes to runthread */
231 _threaddebug(DBGSCHED, "waking process %d", t->proc->pid);
232 while(rendezvous((ulong)q, 0) == ~0){
233 if(_threadexitsallstatus)
234 exits(_threadexitsallstatus);
237 unlock(&t->proc->readylock);
247 p = _threadgetproc();
256 for(t=q->head; t->next!=q->tail; t=t->next)
262 _threaddebug(DBGSCHED, "p->idle is %d\n", idle->id);
263 unlock(&p->readylock);
278 p = _threadgetproc();
279 for(t=p->threads.head; t; t=t->nextt)
280 fprint(2, "[%3d] %s userpc=%lux\n",
281 t->id, psstate(t->state), t->userpc);