|
CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
|
Recursive mutual-exclusion lock, optionally named for cross-process use. More...
#include <Utils.h>
Public Member Functions | |
| Mutex () | |
| Create an anonymous, process-local mutex. | |
| Mutex (const char *name, bool force=false) | |
| Create or open a named, cross-process mutex. | |
| ~Mutex () | |
| bool | enter () |
| Block until the mutex is acquired. | |
| bool | enter (uint32 timeout, const char *errorMsg=NULL) |
| Acquire with a timeout. | |
| bool | leave () |
| Release the mutex. | |
| void | setCancelSafe (bool on) |
| Make this mutex cancellation-safe (POSIX only; no-op on Windows). | |
Protected Attributes | |
| char * | name |
| bool | created |
| uint32 | osid |
| uint32 | total |
| uint32 | count |
| pthread_mutex_t * | mutex |
Recursive mutual-exclusion lock, optionally named for cross-process use.
Unnamed mutexes are process-local; named mutexes are created in shared memory / as kernel objects so multiple processes can synchronize on the same name. Prefer the timeout overload of enter() in production code so a dead peer cannot deadlock the caller forever.
| cmlabs::utils::Mutex::Mutex | ( | ) |
| cmlabs::utils::Mutex::Mutex | ( | const char * | name, |
| bool | force = false ) |
Create or open a named, cross-process mutex.
| name | Global name (namespaced by the shared system instance). |
| force | Re-initialize the underlying object even if it already exists. |
Definition at line 1068 of file Utils.cpp.
References count, created, cmlabs::utils::CreateSharedMemorySegment(), FALSE, MAXSHMEMNAMELEN, mutex, MUTEXMEMSIZE, name, cmlabs::utils::OpenSharedMemorySegment(), osid, total, and WINSHMEMSPACE.
| cmlabs::utils::Mutex::~Mutex | ( | ) |
Definition at line 1123 of file Utils.cpp.
References cmlabs::utils::CloseSharedMemorySegment(), created, mutex, MUTEXMEMSIZE, name, and setCancelSafe().
| bool cmlabs::utils::Mutex::enter | ( | ) |
Block until the mutex is acquired.
Definition at line 1158 of file Utils.cpp.
References count, cmlabs::utils::g_savedCancelState, cmlabs::utils::GetCurrentThreadOSID(), LOG_SYSTEM, LogPrint, mutex, cmlabs::utils::MutexIsCancelSafe(), name, osid, and total.
Referenced by cmlabs::ConcResizer(), cmlabs::ConcWriter(), cmlabs::PsySpace::emitSignal(), cmlabs::utils::EnterMutex(), cmlabs::ThreadManager::UnitTest(), and cmlabs::PsySpace::waitForSignal().
| bool cmlabs::utils::Mutex::enter | ( | uint32 | timeout, |
| const char * | errorMsg = NULL ) |
Acquire with a timeout.
| timeout | Max wait in milliseconds. |
| errorMsg | Optional label (typically FUNCTION) logged when the wait times out. |
Definition at line 1214 of file Utils.cpp.
References cmlabs::utils::CalcTimeout(), count, cmlabs::utils::g_savedCancelState, cmlabs::utils::GetCurrentThreadOSID(), LOG_SYSTEM, LogPrint, mutex, cmlabs::utils::MutexIsCancelSafe(), osid, and total.
| bool cmlabs::utils::Mutex::leave | ( | ) |
Release the mutex.
Definition at line 1330 of file Utils.cpp.
References count, cmlabs::utils::g_savedCancelState, mutex, cmlabs::utils::MutexIsCancelSafe(), and osid.
Referenced by cmlabs::ConcResizer(), cmlabs::ConcWriter(), cmlabs::PsySpace::emitSignal(), cmlabs::MemoryRequestConnection::shutdown(), cmlabs::ThreadManager::UnitTest(), and cmlabs::PsySpace::waitForSignal().
| void cmlabs::utils::Mutex::setCancelSafe | ( | bool | on | ) |
Make this mutex cancellation-safe (POSIX only; no-op on Windows).
When enabled, enter() disables thread cancellation BEFORE acquiring and leave() restores it on the outermost release. This guarantees a thread can never be cancelled (deferred OR asynchronous) while holding the lock, so pthread_cancel() during teardown cannot orphan the mutex and hang every later waiter. Off by default so ordinary mutexes are byte-for-byte unchanged; enable only where holders may be force-terminated (e.g. the ThreadManager registry lock). Requires balanced enter()/leave() pairing.
IMPORTANT: the "cancel-safe" bit is stored OUT-OF-LINE (in a side registry keyed by mutex pointer, see Utils.cpp) and the saved cancel state lives in a thread-local. This keeps sizeof(Mutex) and its member layout BYTE-IDENTICAL to the original — Mutex is embedded by value in structs that are sensitive to its size/layout (PsySpace, network connections, SWIG-wrapped types), and growing it corrupted those on the Linux libPsySystem.so build.
Definition at line 1016 of file Utils.cpp.
References cmlabs::utils::g_cancelSafeCount, cmlabs::utils::g_cancelSafeSet, and cmlabs::utils::g_cancelSafeSetLock.
Referenced by ~Mutex().
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |