2 #include "threadimpl.h"
4 //static Thread *runthread(Proc*);
6 static char *_psstate[] = {
16 if(s < 0 || s >= nelem(_psstate))
26 extern void ignusr1(void), _threaddie(int);
28 signal(SIGTERM, _threaddie);
32 p->pid = _threadgetpid();
35 while(_setlabel(&p->sched))
37 _threaddebug(DBGSCHED, "top of schedinit, _threadexitsallstatus=%p", _threadexitsallstatus);
38 if(_threadexitsallstatus)
39 exits(_threadexitsallstatus);
41 if((t=p->thread) != nil){
45 fprint(2, "moribund %d\n", t->moribund);
46 assert(t->moribund == 1);
49 t->prevt->nextt = t->nextt;
51 p->threads.head = t->nextt;
53 t->nextt->prevt = t->prevt;
55 p->threads.tail = t->prevt;
63 free(t); /* XXX how do we know there are no references? */
69 t->ret = _schedexec(&p->exec);
73 t->ret = _schedfork(p->newproc);
75 //fprint(2, "_schedfork: %r\n");
80 t->state = t->nextstate;
100 _threaddebug(DBGSCHED, "sleeping for more work (%d threads)", p->nthreads);
101 unlock(&p->readylock);
102 while(rendezvous((ulong)q, 0) == ~0){
103 if(_threadexitsallstatus)
104 exits(_threadexitsallstatus);
106 /* lock picked up from _threadready */
110 unlock(&p->readylock);
121 p = _threadgetproc();
122 //fprint(2, "p %p\n", p);
123 if((t = p->thread) != nil){
124 if((ulong)&p < (ulong)t->stk){ /* stack overflow */
125 fprint(2, "stack overflow %lux %lux\n", (ulong)&p, (ulong)t->stk);
128 // _threaddebug(DBGSCHED, "pausing, state=%s set %p goto %p",
129 // psstate(t->state), &t->sched, &p->sched);
130 if(_setlabel(&t->sched)==0)
131 _gotolabel(&p->sched);
136 _threaddebug(DBGSCHED, "all threads gone; exiting");
140 // _threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
143 _threaddebug(DBGSCHED, "%d.%d marked to die");
147 t->nextstate = Ready;
148 _gotolabel(&t->sched);
158 p = _threadgetproc();
160 return (ulong)&p - (ulong)t->stk;
164 _threadready(Thread *t)
168 assert(t->state == Ready);
169 _threaddebug(DBGSCHED, "readying %d.%d", t->proc->pid, t->id);
171 lock(&t->proc->readylock);
179 assert(q->asleep == 1);
181 /* lock passes to runthread */
182 _threaddebug(DBGSCHED, "waking process %d", t->proc->pid);
183 while(rendezvous((ulong)q, 0) == ~0){
184 if(_threadexitsallstatus)
185 exits(_threadexitsallstatus);
188 unlock(&t->proc->readylock);