-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutex.c
288 lines (259 loc) · 5.24 KB
/
mutex.c
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
/*
* mutex.c
* Uniprocessor i386 implementation of mutual exclusion
*
* Mutual exclusion's not very hard on a uniprocessor, eh?
*/
#include <sys/assert.h>
#include <sys/thread.h>
#include <sys/percpu.h>
#include <sys/sched.h>
#include "../mach/mutex.h"
#include "../mach/locore.h"
extern lock_t runq_lock;
extern void lsetrun(), swtch();
#ifdef DEBUG
char msg_deadlock[] = "deadlock", msg_notheld[] = "not held";
#endif
/*
* q_sema()
* Queue a thread under a semaphore
*
* Assumes semaphore is locked.
*/
inline static void
q_sema(struct sema *s, struct thread *t)
{
if (!s->s_sleepq) {
s->s_sleepq = t;
t->t_hd = t->t_tl = t;
} else {
struct thread *t2 = s->s_sleepq;
t->t_hd = t2;
t->t_tl = t2->t_tl;
t->t_tl->t_hd = t;
t2->t_tl = t;
}
}
/*
* dq_sema()
* Remove a thread from a semaphore sleep list
*
* Assumes semaphore is locked.
*/
inline static void
dq_sema(struct sema *s, struct thread *t)
{
ASSERT_DEBUG(s->s_count < 0, "dq_sema: bad count");
s->s_count += 1;
if (t->t_hd == t) {
s->s_sleepq = 0;
} else {
struct thread *t_hd = t->t_hd;
struct thread *t_tl = t->t_tl;
t_hd->t_tl = t_tl;
t_tl->t_hd = t_hd;
if (s->s_sleepq == t) {
s->s_sleepq = t_hd;
}
}
#ifdef DEBUG
t->t_hd = t->t_tl = 0;
#endif
}
/*
* p_sema()
* Take semaphore, sleep if can't
*/
int
p_sema(sema_t *s, pri_t p)
{
struct thread *t;
ASSERT_DEBUG(cpu.pc_locks == 0, "p_sema: locks held");
ASSERT_DEBUG(s->s_lock.l_lock == 0, "p_sema: deadlock");
/*
* Counts > 0, just decrement the count and go
*/
p_lock_void(&s->s_lock, SPLHI);
s->s_count -= 1;
if (s->s_count >= 0) {
v_lock(&s->s_lock, SPL0);
return(0);
}
/*
* We're going to sleep. Add us at the tail of the
* queue, and relinquish the processor.
*/
t = curthread;
t->t_wchan = s;
t->t_nointr = (p == PRIHI);
t->t_intr = 0; /* XXX this can race with notify */
q_sema(s, t);
p_lock_void(&runq_lock, SPLHI_SAME);
v_lock(&s->s_lock, SPLHI_SAME);
t->t_state = TS_SLEEP;
ATOMIC_DEC(&num_run);
swtch();
sti();
/*
* We're back. If we have an event pending, give up on the
* semaphore and return the right result.
*/
t->t_nointr = 0;
if (t->t_intr) {
ASSERT_DEBUG(t->t_wchan == 0, "p_sema: intr w. wchan");
ASSERT_DEBUG(p != PRIHI, "p_sema: intr w. PRIHI");
return(1);
}
/*
* We were woken up to be next with the semaphore. Our waker
* did the reference count update, so we just return.
*/
return(0);
}
/*
* cp_sema()
* Conditional p_sema()
*/
int
cp_sema(sema_t *s)
{
spl_t spl;
int x;
ASSERT_DEBUG(s->s_lock.l_lock == 0, "p_sema: deadlock");
spl = p_lock(&s->s_lock, SPLHI);
if (s->s_count > 0) {
s->s_count -= 1;
x = 0;
} else {
x = -1;
}
v_lock(&s->s_lock, spl);
return(x);
}
/*
* v_sema()
* Release a semaphore
*/
void
v_sema(sema_t *s)
{
struct thread *t;
spl_t spl;
spl = p_lock(&s->s_lock, SPLHI);
if (s->s_sleepq) {
ASSERT_DEBUG(s->s_count < 0, "v_sema: too many sleepers");
t = s->s_sleepq;
ASSERT_DEBUG(t->t_wchan == s, "v_sema: mismatch");
dq_sema(s, t);
t->t_wchan = 0;
p_lock_void(&runq_lock, SPLHI_SAME);
lsetrun(t);
v_lock(&runq_lock, SPLHI_SAME);
} else {
/* dq_sema() does it otherwise */
s->s_count += 1;
}
v_lock(&s->s_lock, spl);
}
/*
* vall_sema()
* Kick everyone loose who's sleeping on the semaphore
*/
void
vall_sema(sema_t *s)
{
while (s->s_count < 0) {
/* XXX races on MP; have to expand v_sema here */
v_sema(s);
}
}
/*
* p_sema_v_lock()
* Atomically transfer from a spinlock to a semaphore
*/
int
p_sema_v_lock(sema_t *s, pri_t p, lock_t *l)
{
struct thread *t;
ASSERT_DEBUG(cpu.pc_locks == 1, "p_sema_v: bad lock count");
ASSERT_DEBUG(s->s_lock.l_lock == 0, "p_sema_v: deadlock");
/*
* Take semaphore lock. If count is high enough, release
* semaphore and lock now.
*/
p_lock_void(&s->s_lock, SPLHI);
s->s_count -= 1;
if (s->s_count >= 0) {
v_lock(&s->s_lock, SPLHI_SAME);
v_lock(l, SPL0);
return(0);
}
/*
* We're going to sleep. Add us at the tail of the
* queue, and relinquish the processor.
*/
t = curthread;
t->t_wchan = s;
t->t_intr = 0;
t->t_nointr = (p == PRIHI);
q_sema(s, t);
p_lock_void(&runq_lock, SPLHI_SAME);
v_lock(&s->s_lock, SPLHI_SAME);
v_lock(l, SPLHI_SAME);
t->t_state = TS_SLEEP;
ATOMIC_DEC(&num_run);
swtch();
sti();
/*
* We're back. If we have an event pending, give up on the
* semaphore and return the right result.
*/
t->t_nointr = 0;
if (t->t_intr) {
ASSERT_DEBUG(t->t_wchan == 0, "p_sema: intr w. wchan");
ASSERT_DEBUG(p != PRIHI, "p_sema: intr w. PRIHI");
return(1);
}
/*
* We were woken up to be next with the semaphore. Our waker
* did the reference count update, so we just return.
*/
return(0);
}
/*
* cunsleep()
* Try to remove a process from a sema queue
*
* Returns 1 on busy mutex; 0 for success
*/
int
cunsleep(struct thread *t)
{
spl_t s;
sema_t *sp;
ASSERT_DEBUG(t->t_wchan, "cunsleep: zero wchan");
/*
* Try for mutex on semaphore
*/
sp = t->t_wchan;
s = cp_lock(&sp->s_lock, SPLHI);
if (s == -1) {
return(1);
}
/*
* Get thread off queue
*/
dq_sema(sp, t);
/*
* Null out wchan to be safe. Sema count was updated
* by dq_sema(). Flag that he didn't get the semaphore.
*/
t->t_wchan = 0;
t->t_intr = 1;
/*
* Success
*/
v_lock(&sp->s_lock, s);
return(0);
}