public class RWEx extends CObject implements Sync
The correct algorithm pattern to use RWEx objects should use try/finally
blocks:
rwex.lockWriter(); // rwex.lockReader(); try { ... } finally { rwex.unlockWriter(); // rwex.unlockReader(); // rwex.unlock(); }
This class follows DbC(tm) methodology
(Wikipedia).
Where possible, contracts are implement with native's Java
assert.
Modifier and Type | Class and Description |
---|---|
static class |
RWEx.Priority
Locking priority choices:
WRITER: priority to writers (a waiting writer prevents new readers from getting the lock); READERS: priority to readers (a waiting reader prevents new writers from getting the lock); TIME_OF_ARRIVAL: priority ordered by decreasing waiting time (not yet implemented!). |
Constructor and Description |
---|
RWEx()
Constructs a new non-recursive RWEx with WRITER priority and registering waiting threads.
|
RWEx(boolean recursive,
boolean registerAwaitingThreads)
Constructs a new RWEx with WRITER priority.
|
RWEx(RWEx.Priority priority)
Constructs a new non-recursive RWEx registering waiting threads.
|
RWEx(RWEx.Priority priority,
boolean registerAwaitingThreads)
Constructs a new non-recursive RWEx.
|
RWEx(RWEx.Priority priority,
boolean recursive,
boolean registerAwaitingThreads)
Constructs a new RWEx.
|
Modifier and Type | Method and Description |
---|---|
void |
changePriority(RWEx.Priority priority)
Change current priority.
|
void |
changeToReader()
Change from a writer lock to a reader lock.
|
SyncState |
getStateAndUnlock()
Get current Sync state, and unlock it (if applicable).
|
boolean |
lockIsMine()
Is lock owned by me?
|
void |
lockReader()
Reader lock.
|
void |
lockWriter()
Writer lock.
|
RWExCV |
newCV()
Create and return a new condition variable attached to current RWEx.
|
RWEx.Priority |
priority()
Current priority.
|
boolean |
readerLockIsMine()
Is reader lock owned by me?
|
void |
recoverState(SyncState state)
Recover Sync state.
|
boolean |
recursive()
Is current rwex recursive?
|
boolean |
trylockReader()
Try to lock as a reader (without waiting).
|
boolean |
trylockWriter()
Try to lock as a writer (without waiting).
|
boolean |
tryToChangeToWriter()
Attempts to change from a reader lock to a writer lock.
|
void |
unlock()
Unlocks active lock (which might be either a reader or a writer lock).
|
void |
unlockReader()
Unlocks reader.
|
void |
unlockWriter()
Unlocks writer.
|
int |
writerLockCount()
Writer lock count of current lock.
|
boolean |
writerLockIsMine()
Is writer lock owned by me?
|
await, await, await, broadcast, interruptWaitingThreads, registerAwaitingThreads, signal, syncronizedLockIsMine
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
interruptWaitingThreads, registerAwaitingThreads
public RWEx()
public RWEx(boolean recursive, boolean registerAwaitingThreads)
recursive
- if true, the created rwex will be recursive (non-recursive, if false)registerAwaitingThreads
- if true, threads are registered when waitingpublic RWEx(RWEx.Priority priority)
priority
- waiting priority (WRITER or READERS)public RWEx(RWEx.Priority priority, boolean registerAwaitingThreads)
priority
- waiting priority (WRITER or READERS)registerAwaitingThreads
- if true, threads are registered when waitingpublic RWEx(RWEx.Priority priority, boolean recursive, boolean registerAwaitingThreads)
priority
- waiting priority (WRITER or READERS)recursive
- if true, the created rwex will be recursive (non-recursive, if false)registerAwaitingThreads
- if true, threads are registered when waitingpublic RWEx.Priority priority()
public void changePriority(RWEx.Priority priority)
priority
- waiting priority (WRITER or READERS)public void lockReader()
!lockIsMine()
readerLockIsMine()
public void lockWriter()
!lockIsMine()
writerLockIsMine()
public boolean trylockReader()
!lockIsMine()
result && readerLockIsMine() || !result && !readerLockIsMine()
public boolean trylockWriter()
!lockIsMine()
result && writerLockIsMine() || !result && !writerLockIsMine()
public void unlock()
lockIsMine()
!lockIsMine()
public void unlockReader()
readerLockIsMine()
!lockIsMine()
public void unlockWriter()
writerLockIsMine()
!lockIsMine()
public boolean lockIsMine()
CObject
lockIsMine
in interface Sync
lockIsMine
in interface SyncCV
lockIsMine
in class CObject
public boolean readerLockIsMine()
public boolean writerLockIsMine()
public boolean recursive()
public int writerLockCount()
writerLockIsMine()
public RWExCV newCV()
public SyncState getStateAndUnlock()
Sync
lockIsMine()
getStateAndUnlock
in interface Sync
public void recoverState(SyncState state) throws ThreadInterruptedException
Sync
state != null && state.obj() == this
- my state object required!lockIsMine()
recoverState
in interface Sync
state
- Sync state object (returned by getStateAndUnlock)ThreadInterruptedException
public void changeToReader()
writerLockIsMine()
!recursive() || writerLockCount() == 1
readerLockIsMine()
public boolean tryToChangeToWriter()
readerLockIsMine()
!recursive()
result && writerLockIsMine() || !result && readerLockIsMine()