|
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. | |
| EWaitResult | TimedReadLockEx (Timeout timeout_ticks) |
| Acquire the lock for shared reading with a timeout, preserving the full wait outcome. | |
| 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. | |
| EWaitResult | TimedLockEx (Timeout timeout_ticks) |
| Acquire the lock for exclusive writing with a timeout, preserving the full wait outcome. | |
| 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 582 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.
void Lock() cannot report failure and must return with the lock held, so a cancellation (IKernel::CancelTaskWait()) is absorbed and the wait is retried. This also keeps the mutex re-acquisition at the end of ConditionVariable::Wait() reliable (the kernel re-locks via this method). Use TimedLockEx() if cancellation must be observed. Panics (KERNEL_PANIC_ASSERT) on any other failure. Implements stk::IMutex.
Definition at line 430 of file stk_sync_rwmutex.h.
References stk::KERNEL_PANIC_ASSERT, stk::STK_KERNEL_PANIC(), TimedLockEx(), stk::WAIT_INFINITE, stk::WAIT_RESULT_CANCELED, and stk::WAIT_RESULT_SIGNAL.
Referenced by pthread_rwlock_wrlock(), and 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.
void ReadLock() cannot report failure and must return with the lock held, so a cancellation (IKernel::CancelTaskWait()) is absorbed and the wait is retried. Use TimedReadLockEx() if cancellation must be observed. Panics (KERNEL_PANIC_ASSERT) on any other failure. Definition at line 309 of file stk_sync_rwmutex.h.
References stk::KERNEL_PANIC_ASSERT, stk::STK_KERNEL_PANIC(), TimedReadLockEx(), stk::WAIT_INFINITE, stk::WAIT_RESULT_CANCELED, and stk::WAIT_RESULT_SIGNAL.
Referenced by pthread_rwlock_rdlock(), and 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 329 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 570 of file stk_common.h.
References STK_UNUSED.
Referenced by stk::memory::BlockMemoryPool::BlockMemoryPool(), stk::memory::BlockMemoryPool::BlockMemoryPool(), stk::memory::BytePool::BytePool(), stk::memory::BytePool::BytePool(), tx_block_pool_create(), tx_event_flags_create(), tx_mutex_create(), tx_queue_create(), and tx_semaphore_create().
|
inline |
Acquire the lock for exclusive writing with a timeout.
| [in] | timeout_ticks | Maximum time to wait (ticks). |
TimedLockEx() to tell these apart. NO_WAIT, ISR-unsafe otherwise. Definition at line 212 of file stk_sync_rwmutex.h.
References TimedLockEx(), and stk::WAIT_RESULT_SIGNAL.
Referenced by stk_rwmutex_timed_lock(), and TryLock().
|
inline |
Acquire the lock for exclusive writing with a timeout, preserving the full wait outcome.
| [in] | timeout_ticks | Maximum time to wait (ticks). |
WAIT_RESULT_SIGNAL if the lock was acquired (here "signal" means "lock acquired": the predicate is re-checked after every wake), WAIT_RESULT_TIMEOUT if the timeout expired (or NO_WAIT was passed and the lock was busy), WAIT_RESULT_CANCELED if the wait was cancelled via IKernel::CancelTaskWait(), WAIT_RESULT_FAIL if the kernel rejected the wait. NO_WAIT, ISR-unsafe otherwise. Definition at line 348 of file stk_sync_rwmutex.h.
References stk::GetTicks(), stk::KERNEL_PANIC_ASSERT, m_cv_readers, m_cv_writers, m_readers, m_writer_active, m_writers_waiting, stk::NO_WAIT, STK_ASSERT, stk::STK_KERNEL_PANIC(), stk::WAIT_INFINITE, stk::WAIT_RESULT_SIGNAL, and WRITERS_MAX.
Referenced by Lock(), pthread_rwlock_timedwrlock(), stk::sync::RWMutex::ScopedTimedLock::ScopedTimedLock(), stk_rwmutex_timed_lock_ex(), and TimedLock().
|
inline |
Acquire the lock for shared reading with a timeout.
| [in] | timeout_ticks | Maximum time to wait (ticks). |
TimedReadLockEx() to tell these apart. NO_WAIT, ISR-unsafe otherwise. Definition at line 163 of file stk_sync_rwmutex.h.
References TimedReadLockEx(), and stk::WAIT_RESULT_SIGNAL.
Referenced by stk_rwmutex_timed_read_lock(), and TryReadLock().
|
inline |
Acquire the lock for shared reading with a timeout, preserving the full wait outcome.
| [in] | timeout_ticks | Maximum time to wait (ticks). |
WAIT_RESULT_SIGNAL if the lock was acquired (here "signal" means "lock acquired": the predicate is re-checked after every wake), WAIT_RESULT_TIMEOUT if the timeout expired (or NO_WAIT was passed and the lock was busy), WAIT_RESULT_CANCELED if the wait was cancelled via IKernel::CancelTaskWait(), WAIT_RESULT_FAIL if the kernel rejected the wait. NO_WAIT, ISR-unsafe otherwise. Definition at line 260 of file stk_sync_rwmutex.h.
References stk::GetTicks(), m_cv_readers, m_readers, m_writer_active, m_writers_waiting, stk::NO_WAIT, READERS_MAX, STK_ASSERT, stk::WAIT_INFINITE, and stk::WAIT_RESULT_SIGNAL.
Referenced by pthread_rwlock_timedrdlock(), ReadLock(), stk::sync::RWMutex::ScopedTimedReadMutex::ScopedTimedReadMutex(), stk_rwmutex_timed_read_lock_ex(), and TimedReadLock().
|
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 234 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 184 of file stk_sync_rwmutex.h.
References stk::NO_WAIT, and TimedReadLock().
Referenced by pthread_rwlock_tryrdlock(), and 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 450 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 249 of file stk_sync_rwmutex.h.
Referenced by TimedLockEx(), TimedReadLockEx(), and Unlock().
|
private |
signaled when a writer can proceed
Definition at line 250 of file stk_sync_rwmutex.h.
Referenced by ReadUnlock(), TimedLockEx(), and Unlock().
|
private |
current active reader count
Definition at line 251 of file stk_sync_rwmutex.h.
Referenced by ReadUnlock(), RWMutex(), TimedLockEx(), TimedReadLockEx(), and ~RWMutex().
|
private |
true if a writer currently holds the lock
Definition at line 253 of file stk_sync_rwmutex.h.
Referenced by RWMutex(), TimedLockEx(), TimedReadLockEx(), Unlock(), and ~RWMutex().
|
private |
count of writers waiting for access
Definition at line 252 of file stk_sync_rwmutex.h.
Referenced by RWMutex(), TimedLockEx(), TimedReadLockEx(), Unlock(), and ~RWMutex().
|
staticprivate |
maximum number of concurrent readers
Definition at line 246 of file stk_sync_rwmutex.h.
Referenced by TimedReadLockEx().
|
staticprivate |
maximum number of waiting writers
Definition at line 247 of file stk_sync_rwmutex.h.
Referenced by TimedLockEx().