24#pragma GCC diagnostic push
25#pragma GCC diagnostic ignored "-Wpedantic"
33#ifdef HF_MCU_FAMILY_ESP32
34#include "freertos/FreeRTOS.h"
35#include "freertos/semphr.h"
36#include "freertos/task.h"
37#elif defined(HF_MCU_FAMILY_STM32)
42#elif defined(HF_MCU_FAMILY_RP2040)
48 "RTOS mutex implementation not available for this MCU platform. Please add support in PlatformMutex.h"
57#pragma GCC diagnostic pop
66 return static_cast<uint64_t
>(xTaskGetTickCount()) * 1000 / configTICK_RATE_HZ * 1000;
73 const TickType_t ticks = pdMS_TO_TICKS(ms);
74 return (ticks > 0) ? ticks : 1;
92 other.handle_ =
nullptr;
101 other.handle_ =
nullptr;
109 return xSemaphoreTakeRecursive(
handle_, portMAX_DELAY) == pdTRUE;
115 return xSemaphoreTakeRecursive(
handle_, 0) == pdTRUE;
122 return xSemaphoreTakeRecursive(
handle_, ticks) == pdTRUE;
127 xSemaphoreGiveRecursive(
handle_);
136 bool Take(uint32_t timeout_ms = 0) noexcept {
137 if (timeout_ms > 0) {
190 other.writer_mutex_ =
nullptr;
191 other.reader_mutex_ =
nullptr;
192 other.readers_.store(0);
193 other.writer_active_.store(
false);
197 if (
this != &other) {
206 readers_.store(other.readers_.load());
208 other.writer_mutex_ =
nullptr;
209 other.reader_mutex_ =
nullptr;
210 other.readers_.store(0);
211 other.writer_active_.store(
false);
217 if (xSemaphoreTake(
writer_mutex_, portMAX_DELAY) != pdTRUE) {
242 const TickType_t start_time = xTaskGetTickCount();
247 const TickType_t elapsed = xTaskGetTickCount() - start_time;
248 const TickType_t remaining = (elapsed < ticks) ? ticks - elapsed : 0;
249 const TickType_t reader_wait_end = xTaskGetTickCount() + remaining;
251 if (xTaskGetTickCount() >= reader_wait_end) {
268 if (xSemaphoreTake(
reader_mutex_, portMAX_DELAY) != pdTRUE) {
296 const TickType_t start_time = xTaskGetTickCount();
298 const TickType_t elapsed = xTaskGetTickCount() - start_time;
299 if (elapsed >= ticks) {
302 const TickType_t remaining = ticks - elapsed;
312 const TickType_t new_elapsed = xTaskGetTickCount() - start_time;
313 if (new_elapsed >= ticks) {
321 if (xSemaphoreTake(
reader_mutex_, portMAX_DELAY) == pdTRUE) {
336template <
typename Mutex>
341 if (timeout_ms > 0) {
358 other.mutex_ =
nullptr;
359 other.locked_ =
false;
363 if (
this != &other) {
369 other.mutex_ =
nullptr;
370 other.locked_ =
false;
391template <
typename SharedMutex>
396 if (timeout_ms > 0) {
413 other.mutex_ =
nullptr;
414 other.locked_ =
false;
418 if (
this != &other) {
424 other.mutex_ =
nullptr;
425 other.locked_ =
false;
451template <
typename Mutex>
Centralized MCU platform selection and configuration header.