-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleReentrantLock.java
73 lines (68 loc) · 2.26 KB
/
SimpleReentrantLock.java
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
import java.util.concurrent.locks.*;
// A lock is re-entrant if it can be acquired multiple
// times by the same thread. Simple Reentrant Lock
// uses owner thread ID and hold count fields to keep
// track of the owning thread and the number of times
// it holds the lock. A common lock is used for
// ensuring field updates are atomic, and a condition
// object is used for synchronization.
// cr
// Acquiring the lock involves holding the common
// lock, waiting until there is no other thread
// holding it, updating owner thread ID (to current)
// and incrementing hold count before releasing the
// common lock.
//
// Releasing the write lock involves holding the
// common lock, decrementing hold count, and if
// not holding anymore, signalling the others before
// releasing the common lock.
//
// Java already provides a ReentrantLock. This is
// for educational purposes only.
class SimpleReentrantLock extends AbstractLock {
final Lock lock;
final Condition noHolder;
long owner, holdCount;
// lock: common lock
// condition: indicates "no holder"
// owner: thread ID of holding thread
// holdCount: times lock was acquired by owner
// 1. create lock (need not be reentrant)
// 2. create condition
public SimpleReentrantLock() {
lock = new ReentrantLock(); // 1
noHolder = lock.newCondition(); // 2
}
// 1. Acquire common lock.
// 2. Wait until there is no other holder.
// 3. Update owner, and increment hold count.
// 4. Release common lock.
@Override
public void lock() {
long id = Thread.currentThread().getId();
lock.lock(); // 1
try {
while (owner != id && holdCount > 0) // 2
noHolder.await(); // 2
owner = id; // 3
holdCount++; // 3
}
catch (InterruptedException e) {}
finally { lock.unlock(); } // 4
}
// 1. Acquire common lock.
// 2. Throw expection, if we dont hold it.
// 3. Decrement hold count.
// 4. If not holding anymore, signal others.
// 5. Release common lock.
@Override
public void unlock() {
long id = Thread.currentThread().getId();
lock.lock(); // 1
if (owner != id || holdCount == 0) // 2
throw new IllegalMonitorStateException(); // 2
if(--holdCount == 0) noHolder.signal(); // 3, 4
lock.unlock(); // 5
}
}