103 |
|
* @spec JSR-166 |
104 |
|
* @revised $Date$ |
105 |
|
* @editor $Author$ |
106 |
+ |
* @author Doug Lea |
107 |
|
* |
108 |
|
*/ |
109 |
|
public class ReentrantReadWriteLock implements ReadWriteLock, java.io.Serializable { |
114 |
|
*/ |
115 |
|
|
116 |
|
/** Inner class providing readlock */ |
117 |
< |
private transient final Lock readerLock = new ReaderLock(); |
117 |
> |
private final Lock readerLock = new ReaderLock(); |
118 |
|
/** Inner class providing writelock */ |
119 |
< |
private transient final Lock writerLock = new WriterLock(); |
119 |
> |
private final Lock writerLock = new WriterLock(); |
120 |
|
|
121 |
|
public Lock writeLock() { return writerLock; } |
122 |
+ |
|
123 |
|
public Lock readLock() { return readerLock; } |
124 |
|
|
125 |
|
/** |
130 |
|
* subclass defined below that surrounds WriteLock condition waits |
131 |
|
* with bookeeping to save and restore writer state. |
132 |
|
**/ |
133 |
< |
private transient final RRWLock entryLock = new RRWLock(); |
133 |
> |
private final RRWLock entryLock = new RRWLock(); |
134 |
|
|
135 |
|
/** |
136 |
|
* Number of threads that have entered read lock. This is never |
162 |
|
* Lock used by exiting readers and entering writers |
163 |
|
*/ |
164 |
|
|
165 |
< |
private transient final ReentrantLock writeCheckLock = new ReentrantLock(); |
165 |
> |
private final ReentrantLock writeCheckLock = new ReentrantLock(); |
166 |
|
|
167 |
|
/** |
168 |
|
* Condition waited on by blocked entering writers. |
169 |
|
*/ |
170 |
< |
private transient final Condition writeEnabled = writeCheckLock.newCondition(); |
170 |
> |
private final Condition writeEnabled = writeCheckLock.newCondition(); |
171 |
|
|
172 |
|
/** |
173 |
|
* Reader exit protocol, called from unlock. if this is the last |
195 |
|
/** |
196 |
|
* Writer enter protocol, called after acquiring entry lock. |
197 |
|
* Wait until all readers have exited. |
198 |
+ |
* @throws InterruptedException if interrupted while waiting |
199 |
|
*/ |
200 |
|
private void writerEnter() throws InterruptedException { |
201 |
|
writeCheckLock.lock(); |
209 |
|
|
210 |
|
} |
211 |
|
|
212 |
+ |
/** |
213 |
+ |
* Trylock version of Writer enter protocol |
214 |
+ |
* @return true if successful |
215 |
+ |
*/ |
216 |
|
private boolean tryWriterEnter() { |
217 |
|
writeCheckLock.lock(); |
218 |
|
boolean ok = (exreaders == readers); |
220 |
|
return ok; |
221 |
|
} |
222 |
|
|
223 |
+ |
/** |
224 |
+ |
* Timed version of writer enter protocol, called after acquiring |
225 |
+ |
* entry lock. Wait until all readers have exited. |
226 |
+ |
* @param nanos the wait time |
227 |
+ |
* @return true if successful |
228 |
+ |
* @throws InterruptedException if interrupted while waiting |
229 |
+ |
*/ |
230 |
|
private boolean tryWriterEnter(long nanos) throws InterruptedException { |
231 |
|
writeCheckLock.lock(); |
232 |
|
try { |
243 |
|
} |
244 |
|
|
245 |
|
|
246 |
+ |
/** |
247 |
+ |
* The Reader lock |
248 |
+ |
*/ |
249 |
|
private class ReaderLock implements Lock { |
250 |
|
|
251 |
|
public void lock() { |
292 |
|
} |
293 |
|
} |
294 |
|
|
295 |
+ |
/** |
296 |
+ |
* The writer lock |
297 |
+ |
*/ |
298 |
|
private class WriterLock implements Lock { |
299 |
|
public void lock() { |
300 |
|
entryLock.lock(); |
375 |
|
|
376 |
|
public void unlock() { |
377 |
|
// must perform owner check before clearing "writing" |
378 |
< |
entryLock.checkOwner(Thread.currentThread()); |
378 |
> |
if (!entryLock.isHeldByCurrentThread()) |
379 |
> |
throw new IllegalMonitorStateException(); |
380 |
|
writing = false; |
381 |
|
entryLock.unlock(); |
382 |
|
} |
415 |
|
} |
416 |
|
} |
417 |
|
} |
418 |
+ |
|
419 |
|
} |
420 |
|
|