public class AtomicLockingMutexSet extends CObject implements Sync
Locking on a set of mutexes in which waiting *without* locking on any of the mutexes. A lock operation is applied only to a first mutex, and the remaining mutexe are locked with trylock. A failure in one of these, implies that that mutex is putted in the head of the mutex list, and a new attempt is tried (until it succeeds). The advantage of this compound locking module is that waiting (lock operation) is done in the first mutex (hence there is no waiting with a locked mutex), and the mutex list elements are permuted automatically to ensure this compound atomic locking).
The correct algorithm pattern to use AtomicLockingMutexSet objects should use try/finally
blocks:
mstx.lock(); try { ... } finally { mstx.unlock(); }
This class follows DbC(tm) methodology
(Wikipedia).
Where possible, contracts are implement with native's Java
assert.
Constructor and Description |
---|
AtomicLockingMutexSet(Mutex[] array)
Constructs a AtomicLockingMutexSet with array elements.
|
Modifier and Type | Method and Description |
---|---|
static boolean |
allDefined(Mutex[] array)
All the elements of the array are reference to objects.
|
SyncState |
getStateAndUnlock()
Get current Sync state, and unlock it (if applicable).
|
void |
lock()
Locks mutex.
|
boolean |
lockIsMine()
Is lock owned by me?
|
MutexCV |
newCV()
Create and return a new condition variable attached to current AtomicLockingMutexSet.
|
void |
recoverState(SyncState state)
Recover Sync state.
|
boolean |
trylock()
Try to lock mutex (without waiting).
|
void |
unlock()
Unlocks mutex.
|
await, await, await, broadcast, interruptWaitingThreads, registerAwaitingThreads, signal, syncronizedLockIsMine
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
interruptWaitingThreads, registerAwaitingThreads
public AtomicLockingMutexSet(Mutex[] array)
array != null && array.length > 0
allDefined(array)
array
- the arraypublic static boolean allDefined(Mutex[] array)
array != null && array.length > 0
array
- the arraypublic void lock()
!lockIsMine()
lockIsMine()
public boolean trylock()
!lockIsMine()
result && lockIsMine() || !result && !lockIsMine()
public void unlock()
lockIsMine()
!lockIsMine()
public boolean lockIsMine()
lockIsMine
in interface Sync
lockIsMine
in interface SyncCV
lockIsMine
in class CObject
public MutexCV 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