1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
/*
* Thread scheduler.
*/
#include "threadimpl.h"
static Thread *runthread(Proc*);
static void schedexit(Proc*);
/*
* Main scheduling loop.
*/
void
_threadscheduler(void *arg)
{
Proc *p;
Thread *t;
p = arg;
_threadlinkmain();
_threadinitproc(p);
for(;;){
/*
* Clean up zombie children.
*/
_threadwaitkids(p);
/*
* Find next thread to run.
*/
_threaddebug(DBGSCHED, "runthread");
t = runthread(p);
if(t == nil)
schedexit(p);
/*
* If it's ready, run it (might instead be marked to die).
*/
lock(&p->lock);
if(t->state == Ready){
_threaddebug(DBGSCHED, "running %d.%d", p->id, t->id);
t->state = Running;
t->nextstate = Ready;
p->thread = t;
unlock(&p->lock);
_swaplabel(&p->context, &t->context);
lock(&p->lock);
p->thread = nil;
}
/*
* If thread needs to die, kill it.
*/
if(t->moribund){
_threaddebug(DBGSCHED, "moribund %d.%d", p->id, t->id);
assert(t->moribund == 1);
t->state = Dead;
if(t->prevt)
t->prevt->nextt = t->nextt;
else
p->threads.head = t->nextt;
if(t->nextt)
t->nextt->prevt = t->prevt;
else
p->threads.tail = t->prevt;
unlock(&p->lock);
_threadfree(t);
p->nthreads--;
t = nil;
continue;
}
unlock(&p->lock);
/*
* If there is a request to run a function on the
* scheduling stack, do so.
*/
if(p->schedfn){
_threaddebug(DBGSCHED, "schedfn");
p->schedfn(p);
p->schedfn = nil;
_threaddebug(DBGSCHED, "schedfn ended");
}
/*
* Move the thread along.
*/
t->state = t->nextstate;
_threaddebug(DBGSCHED, "moveon %d.%d", p->id, t->id);
if(t->state == Ready)
_threadready(t);
}
}
/*
* Called by thread to give up control of processor to scheduler.
*/
int
_sched(void)
{
Proc *p;
Thread *t;
p = _threadgetproc();
t = p->thread;
assert(t != nil);
_swaplabel(&t->context, &p->context);
return p->nsched++;
}
/*
* Called by thread to yield the processor to other threads.
* Returns number of other threads run between call and return.
*/
int
yield(void)
{
Proc *p;
int nsched;
p = _threadgetproc();
nsched = p->nsched;
return _sched() - nsched;
}
/*
* Choose the next thread to run.
*/
static Thread*
runthread(Proc *p)
{
Thread *t;
Tqueue *q;
/*
* No threads left?
*/
if(p->nthreads==0 || (p->nthreads==1 && p->idle))
return nil;
lock(&p->readylock);
q = &p->ready;
if(q->head == nil){
/*
* Is this a single-process program with an idle thread?
*/
if(p->idle){
/*
* The idle thread had better be ready!
*/
if(p->idle->state != Ready)
sysfatal("all threads are asleep");
/*
* Run the idle thread.
*/
unlock(&p->readylock);
_threaddebug(DBGSCHED, "running idle thread", p->nthreads);
return p->idle;
}
/*
* Wait until one of our threads is readied (by another proc!).
*/
q->asleep = 1;
p->rend.l = &p->readylock;
_procsleep(&p->rend);
/*
* Maybe we were awakened to exit?
*/
if(_threadexitsallstatus)
_exits(_threadexitsallstatus);
assert(q->head != nil);
}
t = q->head;
q->head = t->next;
unlock(&p->readylock);
return t;
}
/*
* Add a newly-ready thread to its proc's run queue.
*/
void
_threadready(Thread *t)
{
Tqueue *q;
/*
* The idle thread does not go on the run queue.
*/
if(t == t->proc->idle){
_threaddebug(DBGSCHED, "idle thread is ready");
return;
}
assert(t->state == Ready);
_threaddebug(DBGSCHED, "readying %d.%d", t->proc->id, t->id);
/*
* Add thread to run queue.
*/
q = &t->proc->ready;
lock(&t->proc->readylock);
t->next = nil;
if(q->head == nil)
q->head = t;
else
q->tail->next = t;
q->tail = t;
/*
* Wake proc scheduler if it is sleeping.
*/
if(q->asleep){
assert(q->asleep == 1);
q->asleep = 0;
_procwakeup(&t->proc->rend);
}
unlock(&t->proc->readylock);
}
/*
* Mark the given thread as the idle thread.
* Since the idle thread was just created, it is sitting
* somewhere on the ready queue.
*/
void
_threadsetidle(int id)
{
Tqueue *q;
Thread *t, **l, *last;
Proc *p;
p = _threadgetproc();
lock(&p->readylock);
/*
* Find thread on ready queue.
*/
q = &p->ready;
for(l=&q->head, last=nil; (t=*l) != nil; l=&t->next, last=t)
if(t->id == id)
break;
assert(t != nil);
/*
* Remove it from ready queue.
*/
*l = t->next;
if(t == q->head)
q->head = t->next;
if(t->next == nil)
q->tail = last;
/*
* Set as idle thread.
*/
p->idle = t;
_threaddebug(DBGSCHED, "p->idle is %d\n", t->id);
unlock(&p->readylock);
}
static void
schedexit(Proc *p)
{
char ex[ERRMAX];
int n;
Proc **l;
_threaddebug(DBGSCHED, "exiting proc %d", p->id);
lock(&_threadpq.lock);
for(l=&_threadpq.head; *l; l=&(*l)->next){
if(*l == p){
*l = p->next;
if(*l == nil)
_threadpq.tail = l;
break;
}
}
n = --_threadnprocs;
unlock(&_threadpq.lock);
strncpy(ex, p->exitstr, sizeof ex);
ex[sizeof ex-1] = '\0';
free(p);
if(n == 0)
_threadexitallproc(ex);
else
_threadexitproc(ex);
}
|