CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
cmlabs::utils::Mutex Class Reference

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.

Protected Attributes

char * name
bool created
uint32 osid
uint32 total
uint32 count
pthread_mutex_t * mutex

Detailed Description

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.

Definition at line 463 of file Utils.h.

Constructor & Destructor Documentation

◆ Mutex() [1/2]

cmlabs::utils::Mutex::Mutex ( )

Create an anonymous, process-local mutex.

Definition at line 948 of file Utils.cpp.

References count, created, FALSE, mutex, name, osid, and total.

◆ Mutex() [2/2]

cmlabs::utils::Mutex::Mutex ( const char * name,
bool force = false )

Create or open a named, cross-process mutex.

Parameters
nameGlobal name (namespaced by the shared system instance).
forceRe-initialize the underlying object even if it already exists.

Definition at line 976 of file Utils.cpp.

References count, created, cmlabs::utils::CreateSharedMemorySegment(), FALSE, MAXSHMEMNAMELEN, mutex, MUTEXMEMSIZE, name, cmlabs::utils::OpenSharedMemorySegment(), osid, total, and WINSHMEMSPACE.

◆ ~Mutex()

cmlabs::utils::Mutex::~Mutex ( )

Definition at line 1031 of file Utils.cpp.

References cmlabs::utils::CloseSharedMemorySegment(), created, mutex, MUTEXMEMSIZE, and name.

Member Function Documentation

◆ enter() [1/2]

bool cmlabs::utils::Mutex::enter ( )

Block until the mutex is acquired.

Returns
true when acquired.

Definition at line 1059 of file Utils.cpp.

References count, cmlabs::utils::GetCurrentThreadOSID(), LOG_SYSTEM, LogPrint, mutex, name, osid, and total.

Referenced by cmlabs::PsySpace::emitSignal(), cmlabs::utils::EnterMutex(), cmlabs::ThreadManager::UnitTest(), and cmlabs::PsySpace::waitForSignal().

◆ enter() [2/2]

bool cmlabs::utils::Mutex::enter ( uint32 timeout,
const char * errorMsg = NULL )

Acquire with a timeout.

Parameters
timeoutMax wait in milliseconds.
errorMsgOptional label (typically FUNCTION) logged when the wait times out.
Returns
true when acquired, false on timeout/error.

Definition at line 1101 of file Utils.cpp.

References cmlabs::utils::CalcTimeout(), count, cmlabs::utils::GetCurrentThreadOSID(), LOG_SYSTEM, LogPrint, mutex, osid, and total.

◆ leave()

bool cmlabs::utils::Mutex::leave ( )

Release the mutex.

Returns
true on success.

Definition at line 1204 of file Utils.cpp.

References count, mutex, and osid.

Referenced by cmlabs::PsySpace::emitSignal(), cmlabs::MemoryRequestConnection::shutdown(), cmlabs::ThreadManager::UnitTest(), and cmlabs::PsySpace::waitForSignal().

Member Data Documentation

◆ count

uint32 cmlabs::utils::Mutex::count
protected

Definition at line 487 of file Utils.h.

Referenced by enter(), enter(), leave(), Mutex(), and Mutex().

◆ created

bool cmlabs::utils::Mutex::created
protected

Definition at line 484 of file Utils.h.

Referenced by Mutex(), Mutex(), and ~Mutex().

◆ mutex

pthread_mutex_t* cmlabs::utils::Mutex::mutex
protected

Definition at line 492 of file Utils.h.

Referenced by enter(), enter(), leave(), Mutex(), Mutex(), and ~Mutex().

◆ name

char* cmlabs::utils::Mutex::name
protected

Definition at line 483 of file Utils.h.

Referenced by enter(), Mutex(), Mutex(), and ~Mutex().

◆ osid

uint32 cmlabs::utils::Mutex::osid
protected

Definition at line 485 of file Utils.h.

Referenced by enter(), enter(), leave(), Mutex(), and Mutex().

◆ total

uint32 cmlabs::utils::Mutex::total
protected

Definition at line 486 of file Utils.h.

Referenced by enter(), enter(), Mutex(), and Mutex().


The documentation for this class was generated from the following files:
  • /home/ubuntu/c/partner/psyclone2/CMSDK/include/Utils.h
  • /home/ubuntu/c/partner/psyclone2/CMSDK/src/Utils.cpp