Blob


1 #include <signal.h>
2 #include "threadimpl.h"
4 //static Thread *runthread(Proc*);
6 #if 0
7 static char *_psstate[] = {
8 "Dead",
9 "Running",
10 "Ready",
11 "Rendezvous",
12 };
14 static char*
15 psstate(int s)
16 {
17 if(s < 0 || s >= nelem(_psstate))
18 return "unknown";
19 return _psstate[s];
20 }
21 #endif
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 if(p->needexec){
71 t->ret = _schedexec(&p->exec);
72 p->needexec = 0;
73 }
74 if(p->newproc){
75 t->ret = _schedfork(p->newproc);
76 if(t->ret < 0){
77 //fprint(2, "_schedfork: %r\n");
78 abort();
79 }
80 p->newproc = nil;
81 }
82 t->state = t->nextstate;
83 if(t->state == Ready)
84 _threadready(t);
85 }
86 unlock(&p->lock);
87 _sched();
88 }
90 static Thread*
91 runthread(Proc *p)
92 {
93 Thread *t;
94 Tqueue *q;
96 if(p->nthreads==0 || (p->nthreads==1 && p->idle))
97 return nil;
98 q = &p->ready;
99 lock(&p->readylock);
100 if(q->head == nil){
101 if(p->idle){
102 if(p->idle->state != Ready){
103 fprint(2, "everyone is asleep\n");
104 exits("everyone is asleep");
106 unlock(&p->readylock);
107 _threaddebug(DBGSCHED, "running idle thread", p->nthreads);
108 return p->idle;
111 _threaddebug(DBGSCHED, "sleeping for more work (%d threads)", p->nthreads);
112 q->asleep = 1;
113 unlock(&p->readylock);
114 while(rendezvous((ulong)q, 0) == ~0){
115 if(_threadexitsallstatus)
116 exits(_threadexitsallstatus);
118 /* lock picked up from _threadready */
120 t = q->head;
121 q->head = t->next;
122 unlock(&p->readylock);
123 return t;
126 void
127 _sched(void)
129 Proc *p;
130 Thread *t;
132 Resched:
133 p = _threadgetproc();
134 //fprint(2, "p %p\n", p);
135 if((t = p->thread) != nil){
136 if((ulong)&p < (ulong)t->stk){ /* stack overflow */
137 fprint(2, "stack overflow %lux %lux\n", (ulong)&p, (ulong)t->stk);
138 abort();
140 // _threaddebug(DBGSCHED, "pausing, state=%s set %p goto %p",
141 // psstate(t->state), &t->sched, &p->sched);
142 if(_setlabel(&t->sched)==0)
143 _gotolabel(&p->sched);
144 return;
145 }else{
146 t = runthread(p);
147 if(t == nil){
148 _threaddebug(DBGSCHED, "all threads gone; exiting");
149 _threaddelproc();
150 _schedexit(p);
152 _threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
153 p->thread = t;
154 if(t->moribund){
155 _threaddebug(DBGSCHED, "%d.%d marked to die");
156 goto Resched;
158 t->state = Running;
159 t->nextstate = Ready;
160 _gotolabel(&t->sched);
164 long
165 threadstack(void)
167 Proc *p;
168 Thread *t;
170 p = _threadgetproc();
171 t = p->thread;
172 return (ulong)&p - (ulong)t->stk;
175 void
176 _threadready(Thread *t)
178 Tqueue *q;
180 if(t == t->proc->idle){
181 _threaddebug(DBGSCHED, "idle thread is ready");
182 return;
185 assert(t->state == Ready);
186 _threaddebug(DBGSCHED, "readying %d.%d", t->proc->pid, t->id);
187 q = &t->proc->ready;
188 lock(&t->proc->readylock);
189 t->next = nil;
190 if(q->head==nil)
191 q->head = t;
192 else
193 q->tail->next = t;
194 q->tail = t;
195 if(q->asleep){
196 assert(q->asleep == 1);
197 q->asleep = 0;
198 /* lock passes to runthread */
199 _threaddebug(DBGSCHED, "waking process %d", t->proc->pid);
200 while(rendezvous((ulong)q, 0) == ~0){
201 if(_threadexitsallstatus)
202 exits(_threadexitsallstatus);
204 }else
205 unlock(&t->proc->readylock);
208 void
209 _threadidle(void)
211 Tqueue *q;
212 Thread *t, *idle;
213 Proc *p;
215 p = _threadgetproc();
216 q = &p->ready;
217 lock(&p->readylock);
218 assert(q->tail);
219 idle = q->tail;
220 if(q->head == idle){
221 q->head = nil;
222 q->tail = nil;
223 }else{
224 for(t=q->head; t->next!=q->tail; t=t->next)
226 t->next = nil;
227 q->tail = t;
229 p->idle = idle;
230 _threaddebug(DBGSCHED, "p->idle is %d\n", idle->id);
231 unlock(&p->readylock);
234 void
235 yield(void)
237 _sched();