Package EDU.oswego.cs.dl.util.concurrent
Class FIFOReadWriteLock
- java.lang.Object
-
- EDU.oswego.cs.dl.util.concurrent.FIFOReadWriteLock
-
- All Implemented Interfaces:
ReadWriteLock
public class FIFOReadWriteLock extends java.lang.Object implements ReadWriteLock
This class implements a policy for reader/writer locks in which threads contend in a First-in/First-out manner for access (modulo the limitations of FIFOSemaphore, which is used for queuing). This policy does not particularly favor readers or writers. As a byproduct of the FIFO policy, the attempt methods may return false even when the lock might logically be available, but, due to contention, cannot be accessed within the given time bound.This lock is NOT reentrant. Current readers and writers should not try to re-obtain locks while holding them.
- See Also:
FIFOSemaphore
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
FIFOReadWriteLock.ReaderSync
protected class
FIFOReadWriteLock.WriterSync
-
Field Summary
Fields Modifier and Type Field Description protected FIFOSemaphore
entryLock
Fair Semaphore serving as a kind of mutual exclusion lock.protected int
exreaders
Number of threads that have exited read lock.protected int
readers
Number of threads that have entered read lock.protected Sync
readerSync
protected Sync
writerSync
-
Constructor Summary
Constructors Constructor Description FIFOReadWriteLock()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
acquireRead()
protected void
acquireWrite()
protected boolean
attemptRead(long msecs)
protected boolean
attemptWrite(long msecs)
Sync
readLock()
get the readLockprotected void
releaseRead()
protected void
releaseWrite()
Sync
writeLock()
get the writeLock
-
-
-
Field Detail
-
entryLock
protected final FIFOSemaphore entryLock
Fair Semaphore serving as a kind of mutual exclusion lock. Writers acquire on entry, and hold until rwlock exit. Readers acquire and release only during entry (but are blocked from doing so if there is an active writer).
-
readers
protected volatile int readers
Number of threads that have entered read lock. Note that this is never reset to zero. Incremented only during acquisition of read lock while the "entryLock" is held, but read elsewhere, so is declared volatile.
-
exreaders
protected int exreaders
Number of threads that have exited read lock. Note that this is never reset to zero. Accessed only in code protected by synchronized(this). When exreaders != readers, the rwlock is being used for reading. Else if the entry lock is held, it is being used for writing (or in transition). Else it is free. Note: To distinguish these states, we assume that fewer than 2^32 reader threads can simultaneously execute.
-
readerSync
protected final Sync readerSync
-
writerSync
protected final Sync writerSync
-
-
Method Detail
-
acquireRead
protected void acquireRead() throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
releaseRead
protected void releaseRead()
-
acquireWrite
protected void acquireWrite() throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
releaseWrite
protected void releaseWrite()
-
attemptRead
protected boolean attemptRead(long msecs) throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
attemptWrite
protected boolean attemptWrite(long msecs) throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
writeLock
public Sync writeLock()
Description copied from interface:ReadWriteLock
get the writeLock- Specified by:
writeLock
in interfaceReadWriteLock
-
readLock
public Sync readLock()
Description copied from interface:ReadWriteLock
get the readLock- Specified by:
readLock
in interfaceReadWriteLock
-
-