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(int), _threaddie(int);
29 ignusr1(1);
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(p->nsched%128 == 0){
107 /* clean up children */
108 e = errno;
109 if((c = _threadwaitchan) != nil){
110 if(c->n <= c->s){
111 sent = 0;
112 for(;;){
113 if((w = p->waitmsg) != nil)
114 p->waitmsg = nil;
115 else
116 w = waitnohang();
117 if(w == nil)
118 break;
119 if(sent == 0){
120 unlock(&p->readylock);
121 sent = 1;
123 if(nbsendp(c, w) != 1)
124 break;
126 p->waitmsg = w;
127 if(sent)
128 goto relock;
130 }else{
131 while((w = waitnohang()) != nil)
132 free(w);
134 errno = e;
136 if(q->head == nil){
137 if(p->idle){
138 if(p->idle->state != Ready){
139 fprint(2, "everyone is asleep\n");
140 exits("everyone is asleep");
142 unlock(&p->readylock);
143 _threaddebug(DBGSCHED, "running idle thread", p->nthreads);
144 return p->idle;
147 _threaddebug(DBGSCHED, "sleeping for more work (%d threads)", p->nthreads);
148 q->asleep = 1;
149 unlock(&p->readylock);
150 while(rendezvous((ulong)q, 0) == ~0){
151 if(_threadexitsallstatus)
152 exits(_threadexitsallstatus);
154 /* lock picked up from _threadready */
156 t = q->head;
157 q->head = t->next;
158 unlock(&p->readylock);
159 return t;
162 void
163 needstack(int howmuch)
165 Proc *p;
166 Thread *t;
168 p = _threadgetproc();
169 if(p == nil || (t=p->thread) == nil)
170 return;
171 if((ulong)&howmuch < (ulong)t->stk+howmuch){ /* stack overflow waiting to happen */
172 fprint(2, "stack overflow: stack at 0x%lux, limit at 0x%lux, need 0x%lux\n", (ulong)&p, (ulong)t->stk, howmuch);
173 abort();
177 int
178 _sched(void)
180 Proc *p;
181 Thread *t;
183 Resched:
184 p = _threadgetproc();
185 //fprint(2, "p %p\n", p);
186 if((t = p->thread) != nil){
187 needstack(512);
188 // _threaddebug(DBGSCHED, "pausing, state=%s set %p goto %p",
189 // psstate(t->state), &t->sched, &p->sched);
190 if(_setlabel(&t->sched)==0)
191 _gotolabel(&p->sched);
192 _threadstacklimit(t->stk, t->stk+t->stksize);
193 return p->nsched++;
194 }else{
195 t = runthread(p);
196 if(t == nil){
197 _threaddebug(DBGSCHED, "all threads gone; exiting");
198 _threaddelproc();
199 _schedexit(p);
201 _threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
202 p->thread = t;
203 if(t->moribund){
204 _threaddebug(DBGSCHED, "%d.%d marked to die");
205 goto Resched;
207 t->state = Running;
208 t->nextstate = Ready;
209 _gotolabel(&t->sched);
210 for(;;);
214 long
215 threadstack(void)
217 Proc *p;
218 Thread *t;
220 p = _threadgetproc();
221 t = p->thread;
222 return (ulong)&p - (ulong)t->stk;
225 void
226 _threadready(Thread *t)
228 Tqueue *q;
230 if(t == t->proc->idle){
231 _threaddebug(DBGSCHED, "idle thread is ready");
232 return;
235 assert(t->state == Ready);
236 _threaddebug(DBGSCHED, "readying %d.%d", t->proc->pid, t->id);
237 q = &t->proc->ready;
238 lock(&t->proc->readylock);
239 t->next = nil;
240 if(q->head==nil)
241 q->head = t;
242 else
243 q->tail->next = t;
244 q->tail = t;
245 if(q->asleep){
246 assert(q->asleep == 1);
247 q->asleep = 0;
248 /* lock passes to runthread */
249 _threaddebug(DBGSCHED, "waking process %d", t->proc->pid);
250 while(rendezvous((ulong)q, 0) == ~0){
251 if(_threadexitsallstatus)
252 exits(_threadexitsallstatus);
254 }else
255 unlock(&t->proc->readylock);
258 void
259 _threadidle(void)
261 Tqueue *q;
262 Thread *t, *idle;
263 Proc *p;
265 p = _threadgetproc();
266 q = &p->ready;
267 lock(&p->readylock);
268 assert(q->tail);
269 idle = q->tail;
270 if(q->head == idle){
271 q->head = nil;
272 q->tail = nil;
273 }else{
274 for(t=q->head; t->next!=q->tail; t=t->next)
276 t->next = nil;
277 q->tail = t;
279 p->idle = idle;
280 _threaddebug(DBGSCHED, "p->idle is %d\n", idle->id);
281 unlock(&p->readylock);
284 int
285 yield(void)
287 Proc *p;
288 int nsched;
290 p = _threadgetproc();
291 nsched = p->nsched;
292 return _sched() - nsched;
295 void
296 threadstatus(void)
298 Proc *p;
299 Thread *t;
301 p = _threadgetproc();
302 for(t=p->threads.head; t; t=t->nextt)
303 fprint(2, "[%3d] %s userpc=%lux\n",
304 t->id, psstate(t->state), t->userpc);