|
|
unsigned int | GetLevel (void) const volatile |
| | Returns the level of this mutex.
|
| |
|
bool | IsLocked (void) const volatile |
| | Returns true if this mutex was locked at least once.
|
| |
|
unsigned int | GetLockCount (void) const volatile |
| | Returns count of how many times this mutex got locked.
|
| |
|
const volatile LevelMutexInfo * | GetPrevious (void) const volatile |
| | Returns pointer to mutex previously locked by the thread which locked this.
|
| |
| virtual MutexErrors::Type | TryLock (void) volatile=0 |
| |
| virtual MutexErrors::Type | Lock (void) volatile=0 |
| |
| virtual MutexErrors::Type | Lock (unsigned int milliSeconds) volatile=0 |
| |
| virtual MutexErrors::Type | Unlock (void) volatile=0 |
| |
| bool | IsRecentLock (void) const volatile |
| |
| bool | IsRecentLock (unsigned int count) const volatile |
| |
|
bool | IsLockedByCurrentThread (void) const volatile |
| | Returns true if this was locked by current thread.
|
| |
|
bool | IsLockedByAnotherThread (void) const volatile |
| | Returns true if this was locked by another thread.
|
| |
This monolithic base class stores common info for a template class used to control mutexes. The template class, LevelMutex, is policy-based class.
- Implementation
- Each thread has a list of mutexes it locked. When a mutex first gets locked, it gets added to the head of the list. If locked again, LevelMutex merely increments a count. When unlocked, the count gets decremented until it reaches zero, and then it gets removed from the list. Each mutex has a pointer to the mutex most recently locked by the current thread. The current level of a thread is always the level of the most recently locked mutex, or UnlockedLevel if the thread does not have any mutexes locked now. A mutex is considered "recently" locked if it is at the head of the list, or the same level as the current mutex and also locked by the current thread.
- Class Invariants
- This class maintains invariants for each LevelMutexInfo so that no function calls corrupt a mutex. Each function makes a call to IsValid at the start so that LevelMutex knows it acts on valid internal data. Many functions call IsValid again when they return to insure the function did not leave any data in an invalid state. The exit call to IsValid occurs through a tiny helper class called Checker to insure all data remain valid even when exceptions occur. Another helper class, MutexUndoer, unlocks mutexes in a container if an exception occurs during calls to MultiLock.
- Error Results
- Many functions return an enum value to indicate an error status. Many enum values indicate errors detected within LevelMutex, but some indicate errors found in policy classes, SpinLevelMutex and SleepLevelMutex.
| MutexErrors::Type Loki::LevelMutexInfo::MultiLock |
( |
MutexContainer & |
mutexes | ) |
|
|
static |
Locks several mutexes at once. Requires O(m + n*n) actions where m is the number of mutexes currently locked by the thread and n is the number of mutexes in the container. This provides strong exception safety. If an exception occurs, any mutexes that were locked during this call will get unlocked.
- Parameters
-
| mutexes | Container of pointers to mutexes. Container must have at least 1 mutex, all mutexes must have the same level, no NULL pointers, and all mutexes must not exceed the thread's current level. This sorts the container by address order. |
- Returns
- Enum value indicating success or error.
References Loki::DoMutexesMatchContainer(), Loki::GetCurrentThreadsLevel(), GetLevel(), IsValidList(), and UnlockedLevel.
Referenced by Loki::MultiMutexLocker::Lock(), MultiLock(), and Loki::MultiMutexLocker::MultiMutexLocker().
| MutexErrors::Type Loki::LevelMutexInfo::MultiLock |
( |
MutexContainer & |
mutexes, |
|
|
unsigned int |
milliSeconds |
|
) |
| |
|
static |
Locks several mutexes at once. Requires O(m + n*n + n*t) actions where m is the number of mutexes currently locked by the thread, n is the number of mutexes in the container, and t is the wait time for each mutex. This provides strong exception safety. If an exception occurs, any mutexes that were locked during this call will ge unlocked.
- Parameters
-
| mutexes | Container of pointers to mutexes. Container must have at least 1 mutex, all mutexes must have the same level, no NULL pointers, and all mutexes must not exceed the thread's current level. This sorts the container by address order. |
| milliSeconds | Amount of time to wait for each mutex. |
- Returns
- Enum value indicating success or error.
References Loki::DoMutexesMatchContainer(), Loki::GetCurrentThreadsLevel(), IsValidList(), MultiLock(), and UnlockedLevel.