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 |
| pw::sync::SharedMutex::~SharedMutex | ( | ) |
Destroys the SharedMutex.
|
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.
| 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.
| 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.
|
inline |
Returns the maximum number of concurrent readers observed.
Only available when PW_SYNC_SHARED_MUTEX_ENABLE_METRICS is enabled.
|
inline |
Returns the metric tracking the maximum number of concurrent readers.
Only available when PW_SYNC_SHARED_MUTEX_ENABLE_METRICS is enabled.
|
inline |
Returns the number of readers currently holding shared access.
|
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.
true if exclusive ownership was successfully acquired, false otherwise.| bool pw::sync::SharedMutex::try_lock_exclusive | ( | ) |
Attempts to lock the mutex for exclusive access (writer) in a non-blocking manner.
true if exclusive ownership was successfully acquired, false otherwise.| bool pw::sync::SharedMutex::try_lock_shared | ( | ) |
Attempts to lock the mutex for shared access (reader) in a non-blocking manner.
true if shared access was successfully acquired, false otherwise.
|
inline |
Unlocks the mutex from exclusive access (writer). Failures are fatal.
This is equivalent to unlock_exclusive() and implements the C++ BasicLockable requirements.
| void pw::sync::SharedMutex::unlock_exclusive | ( | ) |
Unlocks the mutex from exclusive access (writer). Failures are fatal.
| void pw::sync::SharedMutex::unlock_shared | ( | ) |
Releases shared access (reader). Failures are fatal.