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);
160 needstack(int howmuch)
165 p = _threadgetproc();
166 if(p == nil || (t=p->thread) == nil)
168 if((ulong)&howmuch < (ulong)t->stk+howmuch){ /* stack overflow waiting to happen */
169 fprint(2, "stack overflow: stack at 0x%lux, limit at 0x%lux, need 0x%lux\n", (ulong)&p, (ulong)t->stk, howmuch);
181 p = _threadgetproc();
182 //fprint(2, "p %p\n", p);
183 if((t = p->thread) != nil){
185 // _threaddebug(DBGSCHED, "pausing, state=%s set %p goto %p",
186 // psstate(t->state), &t->sched, &p->sched);
187 if(_setlabel(&t->sched)==0)
188 _gotolabel(&p->sched);
189 _threadstacklimit(t->stk, t->stk+t->stksize);
194 _threaddebug(DBGSCHED, "all threads gone; exiting");
198 _threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
201 _threaddebug(DBGSCHED, "%d.%d marked to die");
205 t->nextstate = Ready;
206 _gotolabel(&t->sched);
216 p = _threadgetproc();
218 return (ulong)&p - (ulong)t->stk;
222 _threadready(Thread *t)
226 if(t == t->proc->idle){
227 _threaddebug(DBGSCHED, "idle thread is ready");
231 assert(t->state == Ready);
232 _threaddebug(DBGSCHED, "readying %d.%d", t->proc->pid, t->id);
234 lock(&t->proc->readylock);
242 assert(q->asleep == 1);
244 /* lock passes to runthread */
245 _threaddebug(DBGSCHED, "waking process %d", t->proc->pid);
246 while(rendezvous((ulong)q, 0) == ~0){
247 if(_threadexitsallstatus)
248 exits(_threadexitsallstatus);
251 unlock(&t->proc->readylock);
261 p = _threadgetproc();
270 for(t=q->head; t->next!=q->tail; t=t->next)
276 _threaddebug(DBGSCHED, "p->idle is %d\n", idle->id);
277 unlock(&p->readylock);
286 p = _threadgetproc();
288 return _sched() - nsched;
297 p = _threadgetproc();
298 for(t=p->threads.head; t; t=t->nextt)
299 fprint(2, "[%3d] %s userpc=%lux\n",
300 t->id, psstate(t->state), t->userpc);