100 static constexpr uint32_t
msToTicks(uint32_t ms)
noexcept {
return ms; }
102 static constexpr void yield() noexcept {}
120#if __has_include("PlatformMutexBackend.h")
121 #include "PlatformMutexBackend.h"
124#if !defined(PLATFORM_MUTEX_BACKEND_CONFIGURED)
141 if (hz == 0)
return 0;
150 if (ms == 0)
return 0;
152 return (ticks > 0) ? ticks : 1;
169template <
typename Backend = PlatformMutexActiveBackend>
173 Backend::createRecursive(&
handle_);
177 Backend::destroyRecursive(&
handle_);
190 if (
this != &other) {
191 Backend::destroyRecursive(&
handle_);
201 return Backend::lockRecursive(&
handle_, Backend::MAX_DELAY);
205 return Backend::tryLockRecursive(&
handle_);
210 return Backend::lockRecursive(&
handle_, ticks);
214 Backend::unlockRecursive(&
handle_);
222 bool Take(uint32_t timeout_ms = 0) noexcept {
236 typename Backend::RecursiveMutexHandle
handle_;
251template <
typename Backend = PlatformMutexActiveBackend>
273 other.writer_mutex_ = {};
274 other.reader_mutex_ = {};
275 other.readers_.store(0);
276 other.writer_active_.store(
false);
280 if (
this != &other) {
285 readers_.store(other.readers_.load());
287 other.writer_mutex_ = {};
288 other.reader_mutex_ = {};
289 other.readers_.store(0);
290 other.writer_active_.store(
false);
298 if (!Backend::lockMutex(&
writer_mutex_, Backend::MAX_DELAY))
return false;
300 while (
readers_.load() > 0) { Backend::yield(); }
317 const uint32_t start = Backend::getTickCount();
318 if (!Backend::lockMutex(&
writer_mutex_, ticks))
return false;
320 const uint32_t elapsed = Backend::getTickCount() - start;
321 const uint32_t remaining = (elapsed < ticks) ? ticks - elapsed : 0;
322 const uint32_t deadline = Backend::getTickCount() + remaining;
324 if (Backend::getTickCount() >= deadline) {
343 if (!Backend::lockMutex(&
reader_mutex_, Backend::MAX_DELAY))
return false;
367 const uint32_t start = Backend::getTickCount();
369 const uint32_t elapsed = Backend::getTickCount() - start;
370 if (elapsed >= ticks)
return false;
371 const uint32_t remaining = ticks - elapsed;
372 if (!Backend::lockMutex(&
reader_mutex_, remaining))
return false;
379 const uint32_t new_elapsed = Backend::getTickCount() - start;
380 if (new_elapsed >= ticks)
return false;
386 if (Backend::lockMutex(&
reader_mutex_, Backend::MAX_DELAY)) {
407template <
typename Mutex>
412 if (timeout_ms > 0) {
432 other.mutex_ =
nullptr;
433 other.locked_ =
false;
437 if (
this != &other) {
441 other.mutex_ =
nullptr;
442 other.locked_ =
false;
465template <
typename SharedMutex>
470 if (timeout_ms > 0) {
490 other.mutex_ =
nullptr;
491 other.locked_ =
false;
495 if (
this != &other) {
499 other.mutex_ =
nullptr;
500 other.locked_ =
false;
533template <
typename Mutex>
No-op mutex backend – all operations compile to nothing.
Definition PlatformMutex.h:61
uint8_t RecursiveMutexHandle
Opaque handle for a recursive mutex (1 byte, unused by no-op backend)
Definition PlatformMutex.h:64
static constexpr uint32_t MAX_DELAY
Infinite wait.
Definition PlatformMutex.h:69
static constexpr uint32_t msToTicks(uint32_t ms) noexcept
Convert milliseconds to RTOS ticks.
Definition PlatformMutex.h:100
static constexpr bool lockMutex(MutexHandle *, uint32_t) noexcept
Lock with timeout (ticks). Returns true on success.
Definition PlatformMutex.h:90
static constexpr void unlockMutex(MutexHandle *) noexcept
Unlock regular mutex.
Definition PlatformMutex.h:94
uint8_t MutexHandle
Opaque handle for a regular (non-recursive) mutex (1 byte, unused)
Definition PlatformMutex.h:66
static constexpr void destroyMutex(MutexHandle *) noexcept
Destroy a regular mutex pointed to by *h.
Definition PlatformMutex.h:88
static constexpr void unlockRecursive(RecursiveMutexHandle *) noexcept
Unlock recursive mutex.
Definition PlatformMutex.h:82
static constexpr void createRecursive(RecursiveMutexHandle *) noexcept
Create a recursive mutex. Backend writes into *h.
Definition PlatformMutex.h:74
static constexpr void createMutex(MutexHandle *) noexcept
Create a regular (non-recursive) mutex. Backend writes into *h.
Definition PlatformMutex.h:86
static constexpr uint32_t TICK_RATE_HZ
Assumed tick rate.
Definition PlatformMutex.h:70
static constexpr bool tryLockMutex(MutexHandle *) noexcept
Try lock (non-blocking). Returns true on success.
Definition PlatformMutex.h:92
static constexpr bool tryLockRecursive(RecursiveMutexHandle *) noexcept
Try lock (non-blocking). Returns true on success.
Definition PlatformMutex.h:80
static constexpr bool lockRecursive(RecursiveMutexHandle *, uint32_t) noexcept
Lock recursively with timeout (ticks). Returns true on success.
Definition PlatformMutex.h:78
static constexpr void yield() noexcept
Yield to other tasks (no-op in single-threaded).
Definition PlatformMutex.h:102
static constexpr uint32_t getTickCount() noexcept
Get current RTOS tick count.
Definition PlatformMutex.h:98
static constexpr void destroyRecursive(RecursiveMutexHandle *) noexcept
Destroy a recursive mutex pointed to by *h.
Definition PlatformMutex.h:76