1 #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);
33 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){
44 assert(t->moribund == 1);
47 t->prevt->nextt = t->nextt;
49 p->threads.head = t->nextt;
51 t->nextt->prevt = t->prevt;
53 p->threads.tail = t->prevt;
61 free(t); /* XXX how do we know there are no references? */
66 t->ret = _schedexec(&p->exec);
70 t->ret = _schedfork(p->newproc);
72 //fprint(2, "_schedfork: %r\n");
77 t->state = t->nextstate;
97 _threaddebug(DBGSCHED, "sleeping for more work");
98 unlock(&p->readylock);
99 while(rendezvous((ulong)q, 0) == ~0){
100 if(_threadexitsallstatus)
101 exits(_threadexitsallstatus);
103 /* lock picked up from _threadready */
107 unlock(&p->readylock);
118 p = _threadgetproc();
119 //fprint(2, "p %p\n", p);
120 if((t = p->thread) != nil){
121 if((ulong)&p < (ulong)t->stk){ /* stack overflow */
122 fprint(2, "stack overflow %lux %lux\n", (ulong)&p, (ulong)t->stk);
125 // _threaddebug(DBGSCHED, "pausing, state=%s set %p goto %p",
126 // psstate(t->state), &t->sched, &p->sched);
127 if(_setlabel(&t->sched)==0)
128 _gotolabel(&p->sched);
133 _threaddebug(DBGSCHED, "all threads gone; exiting");
137 // _threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
140 _threaddebug(DBGSCHED, "%d.%d marked to die");
144 t->nextstate = Ready;
145 _gotolabel(&t->sched);
155 p = _threadgetproc();
157 return (ulong)&p - (ulong)t->stk;
161 _threadready(Thread *t)
165 assert(t->state == Ready);
166 _threaddebug(DBGSCHED, "readying %d.%d", t->proc->pid, t->id);
168 lock(&t->proc->readylock);
177 /* lock passes to runthread */
178 _threaddebug(DBGSCHED, "waking process %d", t->proc->pid);
179 while(rendezvous((ulong)q, 0) == ~0){
180 if(_threadexitsallstatus)
181 exits(_threadexitsallstatus);
184 unlock(&t->proc->readylock);