4 #include "threadimpl.h"
6 //static Thread *runthread(Proc*);
8 static char *_psstate[] = {
18 if(s < 0 || s >= nelem(_psstate))
28 extern void ignusr1(void), _threaddie(int);
30 signal(SIGTERM, _threaddie);
34 p->pid = _threadgetpid();
37 while(_setlabel(&p->sched))
39 _threaddebug(DBGSCHED, "top of schedinit, _threadexitsallstatus=%p", _threadexitsallstatus);
40 if(_threadexitsallstatus)
41 exits(_threadexitsallstatus);
43 if((t=p->thread) != nil){
47 fprint(2, "moribund %d\n", t->moribund);
48 assert(t->moribund == 1);
51 t->prevt->nextt = t->nextt;
53 p->threads.head = t->nextt;
55 t->nextt->prevt = t->prevt;
57 p->threads.tail = t->prevt;
65 free(t); /* XXX how do we know there are no references? */
72 t->ret = _schedexec(&p->exec);
77 t->ret = _schedfork(p->newproc);
79 //fprint(2, "_schedfork: %r\n");
84 t->state = t->nextstate;
101 if(p->nthreads==0 || (p->nthreads==1 && p->idle))
108 if((c = _threadwaitchan) != nil){
112 if((w = p->waitmsg) != nil)
119 unlock(&p->readylock);
122 if(nbsendp(c, w) != 1)
130 while((w = waitnohang()) != nil)
135 if(p->idle->state != Ready){
136 fprint(2, "everyone is asleep\n");
137 exits("everyone is asleep");
139 unlock(&p->readylock);
140 _threaddebug(DBGSCHED, "running idle thread", p->nthreads);
144 _threaddebug(DBGSCHED, "sleeping for more work (%d threads)", p->nthreads);
146 unlock(&p->readylock);
147 while(rendezvous((ulong)q, 0) == ~0){
148 if(_threadexitsallstatus)
149 exits(_threadexitsallstatus);
151 /* lock picked up from _threadready */
155 unlock(&p->readylock);
166 p = _threadgetproc();
167 //fprint(2, "p %p\n", p);
168 if((t = p->thread) != nil){
169 if((ulong)&p < (ulong)t->stk+512){ /* stack overflow waiting to happen */
170 fprint(2, "stack overflow: stack at %lux, limit at %lux\n", (ulong)&p, (ulong)t->stk);
173 // _threaddebug(DBGSCHED, "pausing, state=%s set %p goto %p",
174 // psstate(t->state), &t->sched, &p->sched);
175 if(_setlabel(&t->sched)==0)
176 _gotolabel(&p->sched);
177 _threadstacklimit(t->stk);
182 _threaddebug(DBGSCHED, "all threads gone; exiting");
186 _threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
189 _threaddebug(DBGSCHED, "%d.%d marked to die");
193 t->nextstate = Ready;
194 _gotolabel(&t->sched);
204 p = _threadgetproc();
206 return (ulong)&p - (ulong)t->stk;
210 _threadready(Thread *t)
214 if(t == t->proc->idle){
215 _threaddebug(DBGSCHED, "idle thread is ready");
219 assert(t->state == Ready);
220 _threaddebug(DBGSCHED, "readying %d.%d", t->proc->pid, t->id);
222 lock(&t->proc->readylock);
230 assert(q->asleep == 1);
232 /* lock passes to runthread */
233 _threaddebug(DBGSCHED, "waking process %d", t->proc->pid);
234 while(rendezvous((ulong)q, 0) == ~0){
235 if(_threadexitsallstatus)
236 exits(_threadexitsallstatus);
239 unlock(&t->proc->readylock);
249 p = _threadgetproc();
258 for(t=q->head; t->next!=q->tail; t=t->next)
264 _threaddebug(DBGSCHED, "p->idle is %d\n", idle->id);
265 unlock(&p->readylock);
280 p = _threadgetproc();
281 for(t=p->threads.head; t; t=t->nextt)
282 fprint(2, "[%3d] %s userpc=%lux\n",
283 t->id, psstate(t->state), t->userpc);