2 #include "threadimpl.h"
4 //static Thread *runthread(Proc*);
7 static char *_psstate[] = {
17 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? */
71 t->ret = _schedexec(&p->exec);
75 t->ret = _schedfork(p->newproc);
77 //fprint(2, "_schedfork: %r\n");
82 t->state = t->nextstate;
102 _threaddebug(DBGSCHED, "sleeping for more work (%d threads)", p->nthreads);
103 unlock(&p->readylock);
104 while(rendezvous((ulong)q, 0) == ~0){
105 if(_threadexitsallstatus)
106 exits(_threadexitsallstatus);
108 /* lock picked up from _threadready */
112 unlock(&p->readylock);
123 p = _threadgetproc();
124 //fprint(2, "p %p\n", p);
125 if((t = p->thread) != nil){
126 if((ulong)&p < (ulong)t->stk){ /* stack overflow */
127 fprint(2, "stack overflow %lux %lux\n", (ulong)&p, (ulong)t->stk);
130 // _threaddebug(DBGSCHED, "pausing, state=%s set %p goto %p",
131 // psstate(t->state), &t->sched, &p->sched);
132 if(_setlabel(&t->sched)==0)
133 _gotolabel(&p->sched);
138 _threaddebug(DBGSCHED, "all threads gone; exiting");
142 // _threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
145 _threaddebug(DBGSCHED, "%d.%d marked to die");
149 t->nextstate = Ready;
150 _gotolabel(&t->sched);
160 p = _threadgetproc();
162 return (ulong)&p - (ulong)t->stk;
166 _threadready(Thread *t)
170 assert(t->state == Ready);
171 _threaddebug(DBGSCHED, "readying %d.%d", t->proc->pid, t->id);
173 lock(&t->proc->readylock);
181 assert(q->asleep == 1);
183 /* lock passes to runthread */
184 _threaddebug(DBGSCHED, "waking process %d", t->proc->pid);
185 while(rendezvous((ulong)q, 0) == ~0){
186 if(_threadexitsallstatus)
187 exits(_threadexitsallstatus);
190 unlock(&t->proc->readylock);