-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
TwoWayReentrantReadWriteLock.java
174 lines (149 loc) · 4.68 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
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
package me.nallar.tickthreading.util;
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;
/**
* Derived from http://tutorials.jenkov.com/java-concurrency/read-write-locks.html#full
*/
public class TwoWayReentrantReadWriteLock implements ReadWriteLock {
private final Map<Thread, Integer> readingThreads = new HashMap<Thread, Integer>();
private volatile int writeAccesses = 0;
private volatile int writeRequests = 0;
protected boolean fair = true;
private volatile 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 synchronized void lockRead() {
Thread callingThread = Thread.currentThread();
while (cantGrantReadAccess(callingThread)) {
try {
wait(0, 100);
} catch (InterruptedException ignored) {
}
}
readingThreads.put(callingThread,
(getReadAccessCount(callingThread) + 1));
}
private boolean cantGrantReadAccess(Thread callingThread) {
return isNotWriter(callingThread) && (hasWriter() || (isNotReader(callingThread) && (fair && hasWriteRequests())));
}
public synchronized void unlockRead() {
Thread callingThread = Thread.currentThread();
if (isNotReader(callingThread)) {
throw new IllegalMonitorStateException("Calling Thread does not" +
" hold a read lock on this ReadWriteLock");
}
int accessCount = getReadAccessCount(callingThread);
if (accessCount == 1) {
readingThreads.remove(callingThread);
} else {
readingThreads.put(callingThread, (accessCount - 1));
}
notifyAll();
}
public synchronized void lockWrite() {
writeRequests++;
Thread callingThread = Thread.currentThread();
while (cantGrantWriteAccess(callingThread)) {
try {
wait(0, 100);
} catch (InterruptedException ignored) {
}
}
writeRequests--;
writeAccesses++;
if (writingThread != null) {
throw new IllegalStateException("Writing thread was already set when granting write access");
}
writingThread = callingThread;
}
public synchronized void unlockWrite() {
if (isNotWriter(Thread.currentThread())) {
throw new IllegalMonitorStateException("Calling Thread does not" +
" hold the write lock on this ReadWriteLock");
}
writeAccesses--;
if (writeAccesses == 0) {
writingThread = null;
}
notifyAll();
}
private boolean cantGrantWriteAccess(Thread callingThread) {
return isNotOnlyReader(callingThread) && (hasReaders() || (writingThread != null && isNotWriter(callingThread)));
}
private int getReadAccessCount(Thread callingThread) {
Integer accessCount = readingThreads.get(callingThread);
if (accessCount == null) {
return 0;
}
return accessCount;
}
private boolean hasReaders() {
return readingThreads.size() > 0;
}
private boolean isNotReader(Thread callingThread) {
return readingThreads.get(callingThread) == null;
}
private boolean isNotOnlyReader(Thread callingThread) {
return readingThreads.size() != 1 ||
readingThreads.get(callingThread) == null;
}
private boolean hasWriter() {
return writingThread != null;
}
private boolean isNotWriter(Thread callingThread) {
return writingThread != callingThread;
}
private boolean hasWriteRequests() {
return this.writeRequests > 0;
}
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.");
}
}
}