Blob


1 #include <u.h>
2 #include <signal.h>
3 #include <errno.h>
4 #include "threadimpl.h"
6 //static Thread *runthread(Proc*);
8 static char *_psstate[] = {
9 "Dead",
10 "Running",
11 "Ready",
12 "Rendezvous",
13 };
15 static char*
16 psstate(int s)
17 {
18 if(s < 0 || s >= nelem(_psstate))
19 return "unknown";
20 return _psstate[s];
21 }
23 void
24 _schedinit(void *arg)
25 {
26 Proc *p;
27 Thread *t;
28 extern void ignusr1(void), _threaddie(int);
29 ignusr1();
30 signal(SIGTERM, _threaddie);
32 p = arg;
33 lock(&p->lock);
34 p->pid = _threadgetpid();
35 _threadsetproc(p);
36 unlock(&p->lock);
37 while(_setlabel(&p->sched))
38 ;
39 _threaddebug(DBGSCHED, "top of schedinit, _threadexitsallstatus=%p", _threadexitsallstatus);
40 if(_threadexitsallstatus)
41 exits(_threadexitsallstatus);
42 lock(&p->lock);
43 if((t=p->thread) != nil){
44 p->thread = nil;
45 if(t->moribund){
46 if(t->moribund != 1)
47 fprint(2, "moribund %d\n", t->moribund);
48 assert(t->moribund == 1);
49 t->state = Dead;
50 if(t->prevt)
51 t->prevt->nextt = t->nextt;
52 else
53 p->threads.head = t->nextt;
54 if(t->nextt)
55 t->nextt->prevt = t->prevt;
56 else
57 p->threads.tail = t->prevt;
58 unlock(&p->lock);
59 if(t->inrendez){
60 _threadflagrendez(t);
61 _threadbreakrendez();
62 }
63 _stackfree(t->stk);
64 free(t->cmdname);
65 free(t); /* XXX how do we know there are no references? */
66 p->nthreads--;
67 t = nil;
68 _sched();
69 }
70 /*
71 if(p->needexec){
72 t->ret = _schedexec(&p->exec);
73 p->needexec = 0;
74 }
75 */
76 if(p->newproc){
77 t->ret = _schedfork(p->newproc);
78 if(t->ret < 0){
79 //fprint(2, "_schedfork: %r\n");
80 abort();
81 }
82 p->newproc = nil;
83 }
84 t->state = t->nextstate;
85 if(t->state == Ready)
86 _threadready(t);
87 }
88 unlock(&p->lock);
89 _sched();
90 }
92 static Thread*
93 runthread(Proc *p)
94 {
95 Channel *c;
96 Thread *t;
97 Tqueue *q;
98 Waitmsg *w;
99 int e, sent;
101 if(p->nthreads==0 || (p->nthreads==1 && p->idle))
102 return nil;
103 q = &p->ready;
104 relock:
105 lock(&p->readylock);
106 if(q->head == nil){
107 e = errno;
108 if((c = _threadwaitchan) != nil){
109 if(c->n <= c->s){
110 sent = 0;
111 for(;;){
112 if((w = p->waitmsg) != nil)
113 p->waitmsg = nil;
114 else
115 w = waitnohang();
116 if(w == nil)
117 break;
118 if(sent == 0){
119 unlock(&p->readylock);
120 sent = 1;
122 if(nbsendp(c, w) != 1)
123 break;
125 p->waitmsg = w;
126 if(sent)
127 goto relock;
129 }else{
130 while((w = waitnohang()) != nil)
131 free(w);
133 errno = e;
134 if(p->idle){
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);
141 return p->idle;
144 _threaddebug(DBGSCHED, "sleeping for more work (%d threads)", p->nthreads);
145 q->asleep = 1;
146 unlock(&p->readylock);
147 while(rendezvous((ulong)q, 0) == ~0){
148 if(_threadexitsallstatus)
149 exits(_threadexitsallstatus);
151 /* lock picked up from _threadready */
153 t = q->head;
154 q->head = t->next;
155 unlock(&p->readylock);
156 return t;
159 void
160 _sched(void)
162 Proc *p;
163 Thread *t;
165 Resched:
166 p = _threadgetproc();
167 //fprint(2, "p %p\n", p);
168 if((t = p->thread) != nil){
169 if((ulong)&p < (ulong)t->stk+512){ /* stack overflow waiting to happen */
170 fprint(2, "stack overflow: stack at %lux, limit at %lux\n", (ulong)&p, (ulong)t->stk);
171 abort();
173 // _threaddebug(DBGSCHED, "pausing, state=%s set %p goto %p",
174 // psstate(t->state), &t->sched, &p->sched);
175 if(_setlabel(&t->sched)==0)
176 _gotolabel(&p->sched);
177 _threadstacklimit(t->stk);
178 return;
179 }else{
180 t = runthread(p);
181 if(t == nil){
182 _threaddebug(DBGSCHED, "all threads gone; exiting");
183 _threaddelproc();
184 _schedexit(p);
186 _threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
187 p->thread = t;
188 if(t->moribund){
189 _threaddebug(DBGSCHED, "%d.%d marked to die");
190 goto Resched;
192 t->state = Running;
193 t->nextstate = Ready;
194 _gotolabel(&t->sched);
198 long
199 threadstack(void)
201 Proc *p;
202 Thread *t;
204 p = _threadgetproc();
205 t = p->thread;
206 return (ulong)&p - (ulong)t->stk;
209 void
210 _threadready(Thread *t)
212 Tqueue *q;
214 if(t == t->proc->idle){
215 _threaddebug(DBGSCHED, "idle thread is ready");
216 return;
219 assert(t->state == Ready);
220 _threaddebug(DBGSCHED, "readying %d.%d", t->proc->pid, t->id);
221 q = &t->proc->ready;
222 lock(&t->proc->readylock);
223 t->next = nil;
224 if(q->head==nil)
225 q->head = t;
226 else
227 q->tail->next = t;
228 q->tail = t;
229 if(q->asleep){
230 assert(q->asleep == 1);
231 q->asleep = 0;
232 /* lock passes to runthread */
233 _threaddebug(DBGSCHED, "waking process %d", t->proc->pid);
234 while(rendezvous((ulong)q, 0) == ~0){
235 if(_threadexitsallstatus)
236 exits(_threadexitsallstatus);
238 }else
239 unlock(&t->proc->readylock);
242 void
243 _threadidle(void)
245 Tqueue *q;
246 Thread *t, *idle;
247 Proc *p;
249 p = _threadgetproc();
250 q = &p->ready;
251 lock(&p->readylock);
252 assert(q->tail);
253 idle = q->tail;
254 if(q->head == idle){
255 q->head = nil;
256 q->tail = nil;
257 }else{
258 for(t=q->head; t->next!=q->tail; t=t->next)
260 t->next = nil;
261 q->tail = t;
263 p->idle = idle;
264 _threaddebug(DBGSCHED, "p->idle is %d\n", idle->id);
265 unlock(&p->readylock);
268 void
269 yield(void)
271 _sched();
274 void
275 threadstatus(void)
277 Proc *p;
278 Thread *t;
280 p = _threadgetproc();
281 for(t=p->threads.head; t; t=t->nextt)
282 fprint(2, "[%3d] %s userpc=%lux\n",
283 t->id, psstate(t->state), t->userpc);