-
Notifications
You must be signed in to change notification settings - Fork 0
/
572_bad.cpp
44 lines (40 loc) · 870 Bytes
/
572_bad.cpp
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
class Lock {
private:
int value = FREE;
Queue waiting;
public:
void acquire();
void release();
}
Lock::acquire() {
TCB *chosenTCB;
disableInterrupts();
if (value == BUSY) {
waiting.add(runningThread);
runningThread->state = WAITING;
chosenTCB = readyList.remove();
thread_switch(runningThread,
chosenTCB);
runningThread->state = RUNNING;
value = BUSY;
} else {
value = BUSY;
}
enableInterrupts();
}
Lock::release() {
// next thread to hold lock
TCB *next;
disableInterrupts();
if (waiting.notEmpty()) {
// move one TCB from waiting
// to ready
next = waiting.remove();
next->state = READY;
readyList.add(next);
value = FREE;
} else {
value = FREE;
}
enableInterrupts();
}