|
||||||||
| 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.GroupMutex
public class GroupMutex
Group mutex.
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.
| Nested Class Summary | |
|---|---|
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 Summary | |
|---|---|
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. |
|
| Method Summary | |
|---|---|
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. |
| 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 GroupMutex(int numberOfGroups)
numberOfGroups groups and
CURRENT_ACTIVE_GROUP priority.
numberOfGroups >= 2
numberOfGroups - number of groups
public GroupMutex(int numberOfGroups,
GroupMutex.Priority priority)
numberOfGroups groups and
with a selected priority.
numberOfGroups >= 2
numberOfGroups - number of groupspriority - locking priority
public GroupMutex(int numberOfGroups,
GroupMutex.Priority priority,
boolean registerAwaitingThreads)
numberOfGroups groups and
with a selected priority.
numberOfGroups >= 2
numberOfGroups - number of groupspriority - locking priorityregisterAwaitingThreads - if true, threads are registered when waiting| Method Detail |
|---|
public 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() == group
group - group numberpublic boolean tryLock(int group)
group >= 1 && group <= numberOfGroups()!lockIsMine()!result || (lockIsMine() && activeGroup() == group)
group - group number
public void unlock(int group)
group >= 1 && group <= numberOfGroups()lockIsMine() && activeGroup() == group!lockIsMine()
group - group numberpublic boolean lockIsMine()
CObject
lockIsMine in interface SynclockIsMine in interface SyncCVlockIsMine in class CObjectpublic GroupMutexCV newCV()
newCV in interface Syncpublic GroupMutexComposedCV newCV(Sync[] list)
GroupMutexComposedCV.validList(list)
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 Syncstate - Sync state object (returned by getStateAndUnlock)
ThreadInterruptedException
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||