HardFOC Core
API reference
Loading...
Searching...
No Matches
PlatformMutexBackend.h
Go to the documentation of this file.
1
30#pragma once
31
32#include "OsAbstraction.h"
33
34//==============================================================================
35// FreeRTOS Mutex Backend -- only active when a real RTOS is configured
36//==============================================================================
37
38#if defined(HF_RTOS_FREERTOS)
39
50struct FreeRtosMutexBackend {
51 // -- Handle Types ------------------------------------------------------
52 using RecursiveMutexHandle = OS_Mutex;
53 using MutexHandle = OS_Mutex;
54
55 // -- Constants ---------------------------------------------------------
56 static constexpr uint32_t MAX_DELAY = static_cast<uint32_t>(OS_WAIT_FOREVER);
57 static constexpr uint32_t TICK_RATE_HZ = configTICK_RATE_HZ;
58
59 // -- Recursive Mutex Operations ----------------------------------------
60
61 static inline void createRecursive(RecursiveMutexHandle* h) noexcept {
62 os_recursive_mutex_create(h, "PlatMtx");
63 }
64
65 static inline void destroyRecursive(RecursiveMutexHandle* h) noexcept {
66 if (h && *h) {
67 os_recursive_mutex_delete(h);
68 *h = nullptr;
69 }
70 }
71
72 static inline bool lockRecursive(RecursiveMutexHandle* h, uint32_t timeout_ticks) noexcept {
73 return os_recursive_mutex_get(h, static_cast<OS_Ulong>(timeout_ticks)) == OS_SUCCESS;
74 }
75
76 static inline bool tryLockRecursive(RecursiveMutexHandle* h) noexcept {
77 return os_recursive_mutex_get(h, OS_NO_WAIT) == OS_SUCCESS;
78 }
79
80 static inline void unlockRecursive(RecursiveMutexHandle* h) noexcept {
81 os_recursive_mutex_put(h);
82 }
83
84 // -- Regular Mutex Operations ------------------------------------------
85
86 static inline void createMutex(MutexHandle* h) noexcept {
87 os_mutex_create(h, "PlatShrd", OS_INHERIT);
88 }
89
90 static inline void destroyMutex(MutexHandle* h) noexcept {
91 if (h && *h) {
92 os_mutex_delete(h);
93 *h = nullptr;
94 }
95 }
96
97 static inline bool lockMutex(MutexHandle* h, uint32_t timeout_ticks) noexcept {
98 return os_mutex_get(h, static_cast<OS_Ulong>(timeout_ticks)) == OS_SUCCESS;
99 }
100
101 static inline bool tryLockMutex(MutexHandle* h) noexcept {
102 return os_mutex_get(h, OS_NO_WAIT) == OS_SUCCESS;
103 }
104
105 static inline void unlockMutex(MutexHandle* h) noexcept {
106 os_mutex_put(h);
107 }
108
109 // -- Time / Scheduling -------------------------------------------------
110
111 static inline uint32_t getTickCount() noexcept {
112 return static_cast<uint32_t>(os_time_get());
113 }
114
115 static inline uint32_t msToTicks(uint32_t ms) noexcept {
116 return static_cast<uint32_t>(pdMS_TO_TICKS(ms));
117 }
118
119 static inline void yield() noexcept {
120 taskYIELD();
121 }
122};
123
125using PlatformMutexActiveBackend = FreeRtosMutexBackend;
126
128#define PLATFORM_MUTEX_BACKEND_CONFIGURED
129
130#endif // HF_RTOS_FREERTOS
131
132//==============================================================================
133// HF_RTOS_NONE path
134//==============================================================================
135// When RTOS=NONE, this header is included (it's on the include path) but
136// nothing is defined -- no PlatformMutexActiveBackend, no PLATFORM_MUTEX_BACKEND_CONFIGURED.
137// PlatformMutex.h falls back to its built-in NullMutexBackend with zero overhead.
138// This is the correct behavior: users who build without an RTOS get mutex no-ops.
139//==============================================================================