|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectpt.ua.concurrent.CObject
pt.ua.concurrent.RWEx
public class RWEx
Readers-writer exclusion class.
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.
Nested Class Summary | |
---|---|
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 Summary | |
---|---|
RWEx()
Constructs a new non-recursive RWEx with WRITER priority and registering waiting threads. |
|
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. |
Method Summary | |
---|---|
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 |
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. |
boolean |
writerLockIsMine()
Is writer lock owned by me? |
Methods inherited from class pt.ua.concurrent.CObject |
---|
await, await, await, broadcast, interruptWaitingThreads, registerAwaitingThreads, signal, syncronizedLockIsMine |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface pt.ua.concurrent.InterruptibleAwaitingThreads |
---|
interruptWaitingThreads, registerAwaitingThreads |
Constructor Detail |
---|
public RWEx()
public 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 waitingMethod Detail |
---|
public 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 RWExCV newCV()
newCV
in interface Sync
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()
readerLockIsMine()
public boolean tryToChangeToWriter()
readerLockIsMine()
result && writerLockIsMine() || !result && readerLockIsMine()
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |