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
|
#include <signal.h>
#include "threadimpl.h"
//static Thread *runthread(Proc*);
#if 0
static char *_psstate[] = {
"Dead",
"Running",
"Ready",
"Rendezvous",
};
static char*
psstate(int s)
{
if(s < 0 || s >= nelem(_psstate))
return "unknown";
return _psstate[s];
}
#endif
void
_schedinit(void *arg)
{
Proc *p;
Thread *t;
extern void ignusr1(void), _threaddie(int);
ignusr1();
signal(SIGTERM, _threaddie);
p = arg;
lock(&p->lock);
p->pid = _threadgetpid();
_threadsetproc(p);
unlock(&p->lock);
while(_setlabel(&p->sched))
;
_threaddebug(DBGSCHED, "top of schedinit, _threadexitsallstatus=%p", _threadexitsallstatus);
if(_threadexitsallstatus)
exits(_threadexitsallstatus);
lock(&p->lock);
if((t=p->thread) != nil){
p->thread = nil;
if(t->moribund){
if(t->moribund != 1)
fprint(2, "moribund %d\n", t->moribund);
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);
if(t->inrendez){
_threadflagrendez(t);
_threadbreakrendez();
}
_stackfree(t->stk);
free(t->cmdname);
free(t); /* XXX how do we know there are no references? */
p->nthreads--;
t = nil;
_sched();
}
if(p->needexec){
t->ret = _schedexec(&p->exec);
p->needexec = 0;
}
if(p->newproc){
t->ret = _schedfork(p->newproc);
if(t->ret < 0){
//fprint(2, "_schedfork: %r\n");
abort();
}
p->newproc = nil;
}
t->state = t->nextstate;
if(t->state == Ready)
_threadready(t);
}
unlock(&p->lock);
_sched();
}
static Thread*
runthread(Proc *p)
{
Thread *t;
Tqueue *q;
if(p->nthreads==0 || (p->nthreads==1 && p->idle))
return nil;
q = &p->ready;
lock(&p->readylock);
if(q->head == nil){
if(p->idle){
if(p->idle->state != Ready){
fprint(2, "everyone is asleep\n");
exits("everyone is asleep");
}
unlock(&p->readylock);
_threaddebug(DBGSCHED, "running idle thread", p->nthreads);
return p->idle;
}
_threaddebug(DBGSCHED, "sleeping for more work (%d threads)", p->nthreads);
q->asleep = 1;
unlock(&p->readylock);
while(rendezvous((ulong)q, 0) == ~0){
if(_threadexitsallstatus)
exits(_threadexitsallstatus);
}
/* lock picked up from _threadready */
}
t = q->head;
q->head = t->next;
unlock(&p->readylock);
return t;
}
void
_sched(void)
{
Proc *p;
Thread *t;
Resched:
p = _threadgetproc();
//fprint(2, "p %p\n", p);
if((t = p->thread) != nil){
if((ulong)&p < (ulong)t->stk){ /* stack overflow */
fprint(2, "stack overflow %lux %lux\n", (ulong)&p, (ulong)t->stk);
abort();
}
// _threaddebug(DBGSCHED, "pausing, state=%s set %p goto %p",
// psstate(t->state), &t->sched, &p->sched);
if(_setlabel(&t->sched)==0)
_gotolabel(&p->sched);
return;
}else{
t = runthread(p);
if(t == nil){
_threaddebug(DBGSCHED, "all threads gone; exiting");
_threaddelproc();
_schedexit(p);
}
_threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
p->thread = t;
if(t->moribund){
_threaddebug(DBGSCHED, "%d.%d marked to die");
goto Resched;
}
t->state = Running;
t->nextstate = Ready;
_gotolabel(&t->sched);
}
}
long
threadstack(void)
{
Proc *p;
Thread *t;
p = _threadgetproc();
t = p->thread;
return (ulong)&p - (ulong)t->stk;
}
void
_threadready(Thread *t)
{
Tqueue *q;
if(t == t->proc->idle){
_threaddebug(DBGSCHED, "idle thread is ready");
return;
}
assert(t->state == Ready);
_threaddebug(DBGSCHED, "readying %d.%d", t->proc->pid, t->id);
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;
if(q->asleep){
assert(q->asleep == 1);
q->asleep = 0;
/* lock passes to runthread */
_threaddebug(DBGSCHED, "waking process %d", t->proc->pid);
while(rendezvous((ulong)q, 0) == ~0){
if(_threadexitsallstatus)
exits(_threadexitsallstatus);
}
}else
unlock(&t->proc->readylock);
}
void
_threadidle(void)
{
Tqueue *q;
Thread *t, *idle;
Proc *p;
p = _threadgetproc();
q = &p->ready;
lock(&p->readylock);
assert(q->tail);
idle = q->tail;
if(q->head == idle){
q->head = nil;
q->tail = nil;
}else{
for(t=q->head; t->next!=q->tail; t=t->next)
;
t->next = nil;
q->tail = t;
}
p->idle = idle;
_threaddebug(DBGSCHED, "p->idle is %d\n", idle->id);
unlock(&p->readylock);
}
void
yield(void)
{
_sched();
}
|