4 #include "threadimpl.h"
7 static Thread *runthread(Proc*);
9 static char *_psstate[] = {
22 if(s < 0 || s >= nelem(_psstate))
35 p->pid = _tos->pid; //getpid();
36 while(setjmp(p->sched))
38 _threaddebug(DBGSCHED, "top of schedinit, _threadexitsallstatus=%p", _threadexitsallstatus);
39 if(_threadexitsallstatus)
40 exits(_threadexitsallstatus);
42 if((t=p->thread) != nil){
46 for(l=&p->threads.head; *l; l=&(*l)->nextt)
61 free(t); /* XXX how do we know there are no references? */
66 t->ret = _schedexec(&p->exec);
70 t->ret = _schedfork(p->newproc);
73 t->state = t->nextstate;
91 if((uchar*)&x - n < (uchar*)t->stk){
92 fprint(2, "%s %lud: &x=%p n=%d t->stk=%p\n",
93 argv0, _tos->pid, &x, n, t->stk);
94 fprint(2, "%s %lud: stack overflow\n", argv0, _tos->pid);
106 p = _threadgetproc();
107 if((t = p->thread) != nil){
109 _threaddebug(DBGSCHED, "pausing, state=%s", psstate(t->state));
110 if(setjmp(t->sched)==0)
111 longjmp(p->sched, 1);
116 _threaddebug(DBGSCHED, "all threads gone; exiting");
119 _threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
122 _threaddebug(DBGSCHED, "%d.%d marked to die");
126 t->nextstate = Ready;
127 longjmp(t->sched, 1);
143 _threaddebug(DBGSCHED, "sleeping for more work");
144 unlock(&p->readylock);
145 while(rendezvous(q, 0) == (void*)~0){
146 if(_threadexitsallstatus)
147 exits(_threadexitsallstatus);
149 /* lock picked up from _threadready */
153 unlock(&p->readylock);
158 _threadready(Thread *t)
162 assert(t->state == Ready);
163 _threaddebug(DBGSCHED, "readying %d.%d", t->proc->pid, t->id);
165 lock(&t->proc->readylock);
174 /* lock passes to runthread */
175 _threaddebug(DBGSCHED, "waking process %d", t->proc->pid);
176 while(rendezvous(q, 0) == (void*)~0){
177 if(_threadexitsallstatus)
178 exits(_threadexitsallstatus);
181 unlock(&t->proc->readylock);