4 #include "threadimpl.h"
6 //static Thread *runthread(Proc*);
8 static char *_psstate[] = {
18 if(s < 0 || s >= nelem(_psstate))
28 extern void ignusr1(int), _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))
106 if(p->nsched%128 == 0){
107 /* clean up children */
109 if((c = _threadwaitchan) != nil){
113 if((w = p->waitmsg) != nil)
120 unlock(&p->readylock);
123 if(nbsendp(c, w) != 1)
131 while((w = waitnohang()) != nil)
138 if(p->idle->state != Ready){
139 fprint(2, "everyone is asleep\n");
140 exits("everyone is asleep");
142 unlock(&p->readylock);
143 _threaddebug(DBGSCHED, "running idle thread", p->nthreads);
147 _threaddebug(DBGSCHED, "sleeping for more work (%d threads)", p->nthreads);
149 unlock(&p->readylock);
150 while(rendezvous((ulong)q, 0) == ~0){
151 if(_threadexitsallstatus)
152 exits(_threadexitsallstatus);
154 /* lock picked up from _threadready */
158 unlock(&p->readylock);
163 needstack(int howmuch)
168 p = _threadgetproc();
169 if(p == nil || (t=p->thread) == nil)
171 if((ulong)&howmuch < (ulong)t->stk+howmuch){ /* stack overflow waiting to happen */
172 fprint(2, "stack overflow: stack at 0x%lux, limit at 0x%lux, need 0x%lux\n", (ulong)&p, (ulong)t->stk, howmuch);
184 p = _threadgetproc();
185 //fprint(2, "p %p\n", p);
186 if((t = p->thread) != nil){
188 // _threaddebug(DBGSCHED, "pausing, state=%s set %p goto %p",
189 // psstate(t->state), &t->sched, &p->sched);
190 if(_setlabel(&t->sched)==0)
191 _gotolabel(&p->sched);
192 _threadstacklimit(t->stk, t->stk+t->stksize);
197 _threaddebug(DBGSCHED, "all threads gone; exiting");
201 _threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
204 _threaddebug(DBGSCHED, "%d.%d marked to die");
208 t->nextstate = Ready;
209 _gotolabel(&t->sched);
220 p = _threadgetproc();
222 return (ulong)&p - (ulong)t->stk;
226 _threadready(Thread *t)
230 if(t == t->proc->idle){
231 _threaddebug(DBGSCHED, "idle thread is ready");
235 assert(t->state == Ready);
236 _threaddebug(DBGSCHED, "readying %d.%d", t->proc->pid, t->id);
238 lock(&t->proc->readylock);
246 assert(q->asleep == 1);
248 /* lock passes to runthread */
249 _threaddebug(DBGSCHED, "waking process %d", t->proc->pid);
250 while(rendezvous((ulong)q, 0) == ~0){
251 if(_threadexitsallstatus)
252 exits(_threadexitsallstatus);
255 unlock(&t->proc->readylock);
265 p = _threadgetproc();
274 for(t=q->head; t->next!=q->tail; t=t->next)
280 _threaddebug(DBGSCHED, "p->idle is %d\n", idle->id);
281 unlock(&p->readylock);
290 p = _threadgetproc();
292 return _sched() - nsched;
301 p = _threadgetproc();
302 for(t=p->threads.head; t; t=t->nextt)
303 fprint(2, "[%3d] %s userpc=%lux\n",
304 t->id, psstate(t->state), t->userpc);