Skip to main content
Pigweed's logo Pigweed
  1. Home
  2. Reference
  3. C/C++
  4. pw::sync::SharedMutex Class Reference
Loading...
Searching...
No Matches
pw::sync::SharedMutex Class Reference

Overview

The SharedMutex is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads. It offers shared (multiple readers) and exclusive (single writer), non-recursive ownership semantics.

Multiple threads may concurrently acquire shared ownership by calling lock_shared() or try_lock_shared(), while only a single thread may hold exclusive ownership via lock_exclusive() (or lock()). Once a writer begins waiting for exclusive ownership, subsequent attempts to acquire shared ownership will block until the writer has acquired and released the lock, preventing writer starvation.

This primitive is thread safe, but NOT IRQ safe.

Public Member Functions

 SharedMutex ()=default
 Constructs an unlocked SharedMutex.
 
 ~SharedMutex ()
 
 SharedMutex (const SharedMutex &)=delete
 
 SharedMutex (SharedMutex &&)=delete
 
SharedMutex & operator= (const SharedMutex &)=delete
 
SharedMutex & operator= (SharedMutex &&)=delete
 
void lock ()
 
bool try_lock ()
 
void unlock ()
 
void lock_exclusive ()
 
bool try_lock_exclusive ()
 
void unlock_exclusive ()
 
void lock_shared () PW_SHARED_LOCK_FUNCTION()
 
bool try_lock_shared () PW_SHARED_TRYLOCK_FUNCTION(true)
 
void unlock_shared ()
 
size_t reader_count () const
 
size_t max_concurrent_readers () const
 
const pw::metric::TypedMetric< uint32_t > & max_concurrent_readers_metric () const
 

Constructor & Destructor Documentation

◆ ~SharedMutex()

pw::sync::SharedMutex::~SharedMutex ( )

Destroys the SharedMutex.

Precondition
The mutex is not held by any reader or writer. Failures are fatal.

Member Function Documentation

◆ lock()

void pw::sync::SharedMutex::lock ( )
inline

Locks the mutex for exclusive access (writer), blocking indefinitely until acquired. Failures are fatal.

This is equivalent to lock_exclusive() and implements the C++ BasicLockable requirements.

Precondition
The lock isn't already held by this thread. Recursive locking is undefined behavior.

thread safeyes isr safeno nmi safeno

◆ lock_exclusive()

void pw::sync::SharedMutex::lock_exclusive ( )

Locks the mutex for exclusive access (writer), blocking indefinitely until acquired. Failures are fatal.

Blocks until all active readers and any prior writer have released the lock. Once a thread begins waiting for exclusive access, subsequent calls to lock_shared() will block until the writer has finished.

Precondition
The lock isn't already held by this thread. Recursive locking is undefined behavior.

thread safeyes isr safeno nmi safeno

◆ lock_shared()

void pw::sync::SharedMutex::lock_shared ( )

Locks the mutex for shared access (reader), blocking indefinitely until acquired. Failures are fatal.

Multiple threads may hold shared ownership concurrently. If another thread holds or is waiting for exclusive access, this call will block.

Precondition
Recursive locking is undefined behavior.

thread safeyes isr safeno nmi safeno

◆ max_concurrent_readers()

size_t pw::sync::SharedMutex::max_concurrent_readers ( ) const
inline

Returns the maximum number of concurrent readers observed.

Only available when PW_SYNC_SHARED_MUTEX_ENABLE_METRICS is enabled.

Returns
Peak concurrent reader count.

thread safeyes isr safeyes nmi safeno

◆ max_concurrent_readers_metric()

const pw::metric::TypedMetric< uint32_t > & pw::sync::SharedMutex::max_concurrent_readers_metric ( ) const
inline

Returns the metric tracking the maximum number of concurrent readers.

Only available when PW_SYNC_SHARED_MUTEX_ENABLE_METRICS is enabled.

Returns
A reference to the max concurrent readers metric.

thread safeyes isr safeyes nmi safeyes

◆ reader_count()

size_t pw::sync::SharedMutex::reader_count ( ) const
inline

Returns the number of readers currently holding shared access.

Note
This value is read using relaxed memory ordering and should only be relied on for diagnostics, metrics, or testing.
Returns
The current active reader count.

thread safeyes isr safeyes nmi safeno

◆ try_lock()

bool pw::sync::SharedMutex::try_lock ( )
inline

Attempts to lock the mutex for exclusive access (writer) in a non-blocking manner.

This is equivalent to try_lock_exclusive() and implements the C++ Lockable requirements.

Returns
true if exclusive ownership was successfully acquired, false otherwise.
Precondition
The lock isn't already held by this thread. Recursive locking is undefined behavior.

thread safeyes isr safeno nmi safeno

◆ try_lock_exclusive()

bool pw::sync::SharedMutex::try_lock_exclusive ( )

Attempts to lock the mutex for exclusive access (writer) in a non-blocking manner.

Returns
true if exclusive ownership was successfully acquired, false otherwise.
Precondition
The lock isn't already held by this thread. Recursive locking is undefined behavior.

thread safeyes isr safeno nmi safeno

◆ try_lock_shared()

bool pw::sync::SharedMutex::try_lock_shared ( )

Attempts to lock the mutex for shared access (reader) in a non-blocking manner.

Returns
true if shared access was successfully acquired, false otherwise.
Precondition
Recursive locking is undefined behavior.

thread safeyes isr safeno nmi safeno

◆ unlock()

void pw::sync::SharedMutex::unlock ( )
inline

Unlocks the mutex from exclusive access (writer). Failures are fatal.

This is equivalent to unlock_exclusive() and implements the C++ BasicLockable requirements.

Precondition
Exclusive ownership is held by this thread.

thread safeyes isr safeno nmi safeno

◆ unlock_exclusive()

void pw::sync::SharedMutex::unlock_exclusive ( )

Unlocks the mutex from exclusive access (writer). Failures are fatal.

Precondition
Exclusive ownership is held by this thread.

thread safeyes isr safeno nmi safeno

◆ unlock_shared()

void pw::sync::SharedMutex::unlock_shared ( )

Releases shared access (reader). Failures are fatal.

Precondition
Shared ownership is held by this thread.

thread safeyes isr safeno nmi safeno


The documentation for this class was generated from the following file: