![]() |
SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
|
Reader-Writer Lock synchronization primitive for non-recursive shared and exclusive access. More...
#include <stk_sync_rwmutex.h>
Classes | |
| class | ScopedTimedLock |
| RAII wrapper for attempting exclusive write access with a timeout. More... | |
| class | ScopedTimedReadMutex |
| RAII wrapper for attempting shared read access with a timeout. More... | |
Public Member Functions | |
| RWMutex () | |
| Construct an RWMutex in the unlocked state with no active readers or writers. | |
| ~RWMutex () | |
| Destructor. | |
| bool | TimedReadLock (Timeout timeout_ticks) |
| Acquire the lock for shared reading with a timeout. | |
| void | ReadLock () |
| Acquire the lock for shared reading. | |
| bool | TryReadLock () |
| Attempt to acquire the lock for shared reading without blocking. | |
| void | ReadUnlock () |
| Release the shared reader lock. | |
| bool | TimedLock (Timeout timeout_ticks) |
| Acquire the lock for exclusive writing with a timeout. | |
| void | Lock () override |
| Acquire the lock for exclusive writing (IMutex interface). | |
| bool | TryLock () |
| Attempt to acquire the lock for exclusive writing without blocking. | |
| void | Unlock () override |
| Release the exclusive writer lock (IMutex interface). | |
| void | SetTraceName (const char *name) |
| Set name. | |
| const char * | GetTraceName () const |
| Get name. | |
Private Member Functions | |
| RWMutex (const RWMutex &)=delete | |
| RWMutex & | operator= (const RWMutex &)=delete |
Private Attributes | |
| ConditionVariable | m_cv_readers |
| signaled when readers can proceed | |
| ConditionVariable | m_cv_writers |
| signaled when a writer can proceed | |
| uint16_t | m_readers |
| current active reader count | |
| uint16_t | m_writers_waiting |
| count of writers waiting for access | |
| bool | m_writer_active |
| true if a writer currently holds the lock | |
Static Private Attributes | |
| static const uint16_t | READERS_MAX = 0xFFFEU |
| maximum number of concurrent readers | |
| static const uint16_t | WRITERS_MAX = 0xFFFEU |
| maximum number of waiting writers | |
Reader-Writer Lock synchronization primitive for non-recursive shared and exclusive access.
RWMutex allows multiple tasks to read a shared resource simultaneously (shared access) while ensuring that only one task can write to the resource at a time (exclusive access). This is particularly efficient for data structures that are read frequently but modified infrequently.
Definition at line 61 of file stk_sync_rwmutex.h.
|
inlineexplicit |
Construct an RWMutex in the unlocked state with no active readers or writers.
Definition at line 66 of file stk_sync_rwmutex.h.
References m_readers, m_writer_active, and m_writers_waiting.
Referenced by operator=(), stk::sync::RWMutex::ScopedTimedLock::ScopedTimedLock(), and stk::sync::RWMutex::ScopedTimedReadMutex::ScopedTimedReadMutex().
|
inline |
Destructor.
Definition at line 75 of file stk_sync_rwmutex.h.
References m_readers, m_writer_active, m_writers_waiting, STK_ASSERT, and STK_VIRT_DTOR.
|
privatedelete |
|
inlineinherited |
Get name.
NULL if not set or if STK_SYNC_DEBUG_NAMES is 0. Definition at line 534 of file stk_common.h.
|
inlineoverridevirtual |
Acquire the lock for exclusive writing (IMutex interface).
Blocks the calling task until all active readers have released their locks and no other writer is active.
Implements stk::IMutex.
Definition at line 178 of file stk_sync_rwmutex.h.
References STK_UNUSED, TimedLock(), and stk::WAIT_INFINITE.
Referenced by stk_rwmutex_lock().
|
inline |
Acquire the lock for shared reading.
Blocks the calling task if a writer is currently active or if there are writers waiting to acquire the lock.
Definition at line 146 of file stk_sync_rwmutex.h.
References STK_UNUSED, TimedReadLock(), and stk::WAIT_INFINITE.
Referenced by stk_rwmutex_read_lock().
|
inline |
Release the shared reader lock.
Decrements the reader count. If this was the last active reader, notifies any waiting writers.
Definition at line 244 of file stk_sync_rwmutex.h.
References m_cv_writers, m_readers, and STK_ASSERT.
Referenced by stk_rwmutex_read_unlock().
|
inlineinherited |
Set name.
| [in] | name | Null-terminated string or NULL. |
Definition at line 522 of file stk_common.h.
References STK_UNUSED.
Referenced by stk::memory::BlockMemoryPool::BlockMemoryPool(), and stk::memory::BlockMemoryPool::BlockMemoryPool().
|
inline |
Acquire the lock for exclusive writing with a timeout.
| [in] | timeout_ticks | Maximum time to wait (ticks). |
NO_WAIT, ISR-unsafe otherwise. Definition at line 263 of file stk_sync_rwmutex.h.
References stk::KERNEL_PANIC_ASSERT, m_cv_writers, m_readers, m_writer_active, m_writers_waiting, STK_ASSERT, stk::STK_KERNEL_PANIC(), and WRITERS_MAX.
Referenced by Lock(), stk::sync::RWMutex::ScopedTimedLock::ScopedTimedLock(), stk_rwmutex_timed_lock(), and TryLock().
|
inline |
Acquire the lock for shared reading with a timeout.
| [in] | timeout_ticks | Maximum time to wait (ticks). |
NO_WAIT, ISR-unsafe otherwise. Definition at line 212 of file stk_sync_rwmutex.h.
References m_cv_readers, m_readers, m_writer_active, m_writers_waiting, READERS_MAX, and STK_ASSERT.
Referenced by ReadLock(), stk::sync::RWMutex::ScopedTimedReadMutex::ScopedTimedReadMutex(), stk_rwmutex_timed_read_lock(), and TryReadLock().
|
inline |
Attempt to acquire the lock for exclusive writing without blocking.
Checks if any readers are active or if another writer is active.
Definition at line 186 of file stk_sync_rwmutex.h.
References stk::NO_WAIT, and TimedLock().
Referenced by stk_rwmutex_trylock().
|
inline |
Attempt to acquire the lock for shared reading without blocking.
Checks if a writer is active or waiting. If the resource is available for reading, it increments the reader count and returns immediately.
Definition at line 155 of file stk_sync_rwmutex.h.
References stk::NO_WAIT, and TimedReadLock().
Referenced by stk_rwmutex_try_read_lock().
|
inlineoverridevirtual |
Release the exclusive writer lock (IMutex interface).
Releases the lock and prioritizes waking waiting writers. If no writers are waiting, wakes all waiting readers.
Implements stk::IMutex.
Definition at line 305 of file stk_sync_rwmutex.h.
References m_cv_readers, m_cv_writers, m_writer_active, m_writers_waiting, and STK_ASSERT.
Referenced by stk_rwmutex_unlock().
|
private |
signaled when readers can proceed
Definition at line 201 of file stk_sync_rwmutex.h.
Referenced by TimedReadLock(), and Unlock().
|
private |
signaled when a writer can proceed
Definition at line 202 of file stk_sync_rwmutex.h.
Referenced by ReadUnlock(), TimedLock(), and Unlock().
|
private |
current active reader count
Definition at line 203 of file stk_sync_rwmutex.h.
Referenced by ReadUnlock(), RWMutex(), TimedLock(), TimedReadLock(), and ~RWMutex().
|
private |
true if a writer currently holds the lock
Definition at line 205 of file stk_sync_rwmutex.h.
Referenced by RWMutex(), TimedLock(), TimedReadLock(), Unlock(), and ~RWMutex().
|
private |
count of writers waiting for access
Definition at line 204 of file stk_sync_rwmutex.h.
Referenced by RWMutex(), TimedLock(), TimedReadLock(), Unlock(), and ~RWMutex().
|
staticprivate |
maximum number of concurrent readers
Definition at line 198 of file stk_sync_rwmutex.h.
Referenced by TimedReadLock().
|
staticprivate |
maximum number of waiting writers
Definition at line 199 of file stk_sync_rwmutex.h.
Referenced by TimedLock().