public class GroupMutex extends CObject implements Sync
This class implements a group mutual exclusion scheme in which threads within the same (active) group can proceed concurrently, and threads from other groups are required to wait until their group becomes active.
The correct algorithm pattern to use GroupMutex objects should use try/finally blocks:
gmtx.lock(group);
try
{
...
}
finally
{
gmtx.unlock(group);
}
This class follows DbC(tm) methodology
(@see Wikipedia).
Where possible, contracts are implement with native's Java assert.
| Modifier and Type | Class and Description |
|---|---|
static class |
GroupMutex.Priority
Locking priority choices:
CURRENT_ACTIVE_GROUP: active group is always preferred; GROUP_NUMBER_INCREASING: the higher the group number the higher its priority; GROUP_NUMBER_DECREASING: the lower the group number the higher its priority; TIME_OF_ARRIVAL: priority ordered by decreasing waiting time (not yet implemented!). |
| Constructor and Description |
|---|
GroupMutex(int numberOfGroups)
Constructs a new GroupMutex with
numberOfGroups groups and
CURRENT_ACTIVE_GROUP priority. |
GroupMutex(int numberOfGroups,
GroupMutex.Priority priority)
Constructs a new GroupMutex with
numberOfGroups groups and
with a selected priority. |
GroupMutex(int numberOfGroups,
GroupMutex.Priority priority,
boolean registerAwaitingThreads)
Constructs a new GroupMutex with
numberOfGroups groups and
with a selected priority. |
| Modifier and Type | Method and Description |
|---|---|
int |
activeGroup()
Current active group (unsafe operation if lock is owned
by the caller thread).
|
void |
changePriority(GroupMutex.Priority priority)
Change current lock priority.
|
SyncState |
getStateAndUnlock()
Get current Sync state, and unlock it (if applicable).
|
void |
lock(int group)
Lock to a group waiting until succeeds, or is interrupted.
|
boolean |
lockIsMine()
Is lock owned by me?
|
GroupMutexCV |
newCV()
Create and return a new condition variable attached to current GroupMutex.
|
GroupMutexComposedCV |
newCV(Sync[] list)
Create and return a new composed condition variable attached to current GroupMutex and to an array of Sync's.
|
int |
numberOfGroups()
Number of groups.
|
GroupMutex.Priority |
priority()
Current lock priority.
|
void |
recoverState(SyncState state)
Recover Sync state.
|
boolean |
tryLock(int group)
Attempt to lock a group (no waiting involved).
|
void |
unlock(int group)
Unlocks from a group.
|
await, await, await, broadcast, interruptWaitingThreads, registerAwaitingThreads, signal, syncronizedLockIsMineequals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitinterruptWaitingThreads, registerAwaitingThreadspublic GroupMutex(int numberOfGroups)
numberOfGroups groups and
CURRENT_ACTIVE_GROUP priority.
numberOfGroups >= 2numberOfGroups - number of groupspublic GroupMutex(int numberOfGroups,
GroupMutex.Priority priority)
numberOfGroups groups and
with a selected priority.
numberOfGroups >= 2numberOfGroups - number of groupspriority - locking prioritypublic GroupMutex(int numberOfGroups,
GroupMutex.Priority priority,
boolean registerAwaitingThreads)
numberOfGroups groups and
with a selected priority.
numberOfGroups >= 2numberOfGroups - number of groupspriority - locking priorityregisterAwaitingThreads - if true, threads are registered when waitingpublic GroupMutex.Priority priority()
public void changePriority(GroupMutex.Priority priority)
priority - locking prioritypublic int numberOfGroups()
public int activeGroup()
lockIsMine()public void lock(int group)
group >= 1 && group <= numberOfGroups()!lockIsMine()lockIsMine() && activeGroup() == groupgroup - group numberpublic boolean tryLock(int group)
group >= 1 && group <= numberOfGroups()!lockIsMine()!result || (lockIsMine() && activeGroup() == group)group - group numberpublic void unlock(int group)
group >= 1 && group <= numberOfGroups()lockIsMine() && activeGroup() == group!lockIsMine()group - group numberpublic boolean lockIsMine()
CObjectlockIsMine in interface SynclockIsMine in interface SyncCVlockIsMine in class CObjectpublic GroupMutexCV newCV()
public GroupMutexComposedCV newCV(Sync[] list)
GroupMutexComposedCV.validList(list)list - the array of Sync objectspublic SyncState getStateAndUnlock()
SynclockIsMine()getStateAndUnlock in interface Syncpublic void recoverState(SyncState state) throws ThreadInterruptedException
Syncstate != null && state.obj() == this - my state object required!lockIsMine()recoverState in interface Syncstate - Sync state object (returned by getStateAndUnlock)ThreadInterruptedException