-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
TwoWayReentrantReadWriteLock.java
135 lines (119 loc) · 3.71 KB
/
TwoWayReentrantReadWriteLock.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
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
package me.nallar.tickthreading.util.concurrent;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
public class TwoWayReentrantReadWriteLock implements ReadWriteLock {
private final Map<Thread, Integer> readingThreads = new HashMap<Thread, Integer>();
private int writeAccesses = 0;
private int writeRequests = 0;
private int readRequests = 0;
private Thread writingThread = null;
private final Lock readLock = new SimpleLock() {
@Override
public void lock() {
TwoWayReentrantReadWriteLock.this.lockRead();
}
@Override
public void unlock() {
TwoWayReentrantReadWriteLock.this.unlockRead();
}
};
private final Lock writeLock = new SimpleLock() {
@Override
public void lock() {
TwoWayReentrantReadWriteLock.this.lockWrite();
}
@Override
public void unlock() {
TwoWayReentrantReadWriteLock.this.unlockWrite();
}
};
@Override
public Lock readLock() {
return readLock;
}
@Override
public Lock writeLock() {
return writeLock;
}
public final synchronized void lockRead() {
Thread callingThread = Thread.currentThread();
readRequests++;
while (writingThread != callingThread && writingThread != null) {
try {
wait();
} catch (InterruptedException ignored) {
}
}
readRequests--;
Integer count = readingThreads.get(callingThread);
readingThreads.put(callingThread, count == null ? 1 : count + 1);
}
public final synchronized void unlockRead() {
Thread callingThread = Thread.currentThread();
Integer accessCount_ = readingThreads.get(callingThread);
if (accessCount_ == null) {
throw new IllegalMonitorStateException("Calling Thread does not" +
" hold a read lock on this ReadWriteLock");
}
if (accessCount_ == 1) {
readingThreads.remove(callingThread);
if (writeRequests > 0 && readingThreads.isEmpty()) {
notify();
}
} else {
readingThreads.put(callingThread, (accessCount_ - 1));
}
}
@SuppressWarnings ("FieldRepeatedlyAccessedInMethod")
public final synchronized void lockWrite() {
writeRequests++;
Thread callingThread = Thread.currentThread();
int size;
while ((writingThread != callingThread && writingThread != null) || ((size = readingThreads.size()) > 1 || (size == 1 && readingThreads.get(callingThread) == null))) {
try {
wait();
} catch (InterruptedException ignored) {
}
}
writeRequests--;
writeAccesses++;
writingThread = callingThread;
}
public final synchronized void unlockWrite() {
if (writingThread != Thread.currentThread()) {
throw new IllegalMonitorStateException("Calling Thread does not" +
" hold the write lock on this ReadWriteLock");
}
writeAccesses--;
if (writeAccesses == 0) {
writingThread = null;
}
if (writeRequests > 0 || readRequests > 0) {
notifyAll();
}
}
private abstract static class SimpleLock implements Lock {
SimpleLock() {
}
@Override
public void lockInterruptibly() throws InterruptedException {
lock();
}
@Override
public boolean tryLock() {
throw new UnsupportedOperationException("You dun goofed! TwoWayReentrantReadWriteLock doesn't support this.");
}
@Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
throw new UnsupportedOperationException("You dun goofed! TwoWayReentrantReadWriteLock doesn't support this.");
}
@Override
public Condition newCondition() {
throw new UnsupportedOperationException("You dun goofed! TwoWayReentrantReadWriteLock doesn't support this.");
}
}
}