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 needstack(int howmuch)
162 Proc *p;
163 Thread *t;
165 p = _threadgetproc();
166 if(p == nil || (t=p->thread) == nil)
167 return;
168 if((ulong)&howmuch < (ulong)t->stk+howmuch){ /* stack overflow waiting to happen */
169 fprint(2, "stack overflow: stack at 0x%lux, limit at 0x%lux, need 0x%lux\n", (ulong)&p, (ulong)t->stk, howmuch);
170 abort();
174 void
175 _sched(void)
177 Proc *p;
178 Thread *t;
180 Resched:
181 p = _threadgetproc();
182 //fprint(2, "p %p\n", p);
183 if((t = p->thread) != nil){
184 needstack(512);
185 // _threaddebug(DBGSCHED, "pausing, state=%s set %p goto %p",
186 // psstate(t->state), &t->sched, &p->sched);
187 if(_setlabel(&t->sched)==0)
188 _gotolabel(&p->sched);
189 _threadstacklimit(t->stk);
190 return;
191 }else{
192 t = runthread(p);
193 if(t == nil){
194 _threaddebug(DBGSCHED, "all threads gone; exiting");
195 _threaddelproc();
196 _schedexit(p);
198 _threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
199 p->thread = t;
200 if(t->moribund){
201 _threaddebug(DBGSCHED, "%d.%d marked to die");
202 goto Resched;
204 t->state = Running;
205 t->nextstate = Ready;
206 _gotolabel(&t->sched);
210 long
211 threadstack(void)
213 Proc *p;
214 Thread *t;
216 p = _threadgetproc();
217 t = p->thread;
218 return (ulong)&p - (ulong)t->stk;
221 void
222 _threadready(Thread *t)
224 Tqueue *q;
226 if(t == t->proc->idle){
227 _threaddebug(DBGSCHED, "idle thread is ready");
228 return;
231 assert(t->state == Ready);
232 _threaddebug(DBGSCHED, "readying %d.%d", t->proc->pid, t->id);
233 q = &t->proc->ready;
234 lock(&t->proc->readylock);
235 t->next = nil;
236 if(q->head==nil)
237 q->head = t;
238 else
239 q->tail->next = t;
240 q->tail = t;
241 if(q->asleep){
242 assert(q->asleep == 1);
243 q->asleep = 0;
244 /* lock passes to runthread */
245 _threaddebug(DBGSCHED, "waking process %d", t->proc->pid);
246 while(rendezvous((ulong)q, 0) == ~0){
247 if(_threadexitsallstatus)
248 exits(_threadexitsallstatus);
250 }else
251 unlock(&t->proc->readylock);
254 void
255 _threadidle(void)
257 Tqueue *q;
258 Thread *t, *idle;
259 Proc *p;
261 p = _threadgetproc();
262 q = &p->ready;
263 lock(&p->readylock);
264 assert(q->tail);
265 idle = q->tail;
266 if(q->head == idle){
267 q->head = nil;
268 q->tail = nil;
269 }else{
270 for(t=q->head; t->next!=q->tail; t=t->next)
272 t->next = nil;
273 q->tail = t;
275 p->idle = idle;
276 _threaddebug(DBGSCHED, "p->idle is %d\n", idle->id);
277 unlock(&p->readylock);
280 void
281 yield(void)
283 _sched();
286 void
287 threadstatus(void)
289 Proc *p;
290 Thread *t;
292 p = _threadgetproc();
293 for(t=p->threads.head; t; t=t->nextt)
294 fprint(2, "[%3d] %s userpc=%lux\n",
295 t->id, psstate(t->state), t->userpc);