48#if (defined(HF_RTOS_NONE) || defined(HF_MCU_FAMILY_NONE)) && defined(HF_RTOS_NONE_EXTERNAL_CLOCK)
49 return hf_os_none_clock_us();
50#elif defined(HF_RTOS_NONE) || defined(HF_MCU_FAMILY_NONE)
53 const uint32_t ticks =
static_cast<uint32_t
>(
os_time_get());
68 return (ticks > 0) ? ticks : 1;
128 bool Take(uint32_t timeout_ms = 0) noexcept {
129 if (timeout_ms > 0) {
173 bool lock() noexcept;
200template <typename MutexT>
209 : mutex_(&mutex), locked_(false) {
210 if (timeout_ms > 0) {
211 locked_ = mutex_->try_lock_for(timeout_ms);
213 locked_ = mutex_->lock();
218 if (locked_ && mutex_) {
227 other.mutex_ =
nullptr;
228 other.locked_ =
false;
232 if (
this != &other) {
233 if (locked_ && mutex_) {
237 locked_ = other.locked_;
238 other.mutex_ =
nullptr;
239 other.locked_ =
false;
251 if (locked_ && mutex_) {
266template <
typename SharedMutexT>
270 : mutex_(&mutex), locked_(false) {
271 if (timeout_ms > 0) {
272 locked_ = mutex_->try_lock_shared_for(timeout_ms);
274 locked_ = mutex_->lock_shared();
279 if (locked_ && mutex_) {
280 mutex_->unlock_shared();
288 other.mutex_ =
nullptr;
289 other.locked_ =
false;
293 if (
this != &other) {
294 if (locked_ && mutex_) {
295 mutex_->unlock_shared();
298 locked_ = other.locked_;
299 other.mutex_ =
nullptr;
300 other.locked_ =
false;
310 if (locked_ && mutex_) {
311 mutex_->unlock_shared();
326template <
typename MutexT>
Generic RTOS abstraction — public header.
C-facing helper functions for delays, cycle counters, and creation of RTOS handles (queues,...
constexpr uint32_t os_convert_msec_to_delay_ticks(uint32_t milliseconds)
Converts milliseconds to delay ticks, rounding down but never to zero.
Definition OsUtility.h:74
constexpr uint32_t os_convert_delay_ticks_to_msec(uint32_t delayTicks)
Converts delay ticks to milliseconds.
Definition OsUtility.h:85
Definition RtosMutex.h:76
bool try_lock() noexcept
Try to lock the mutex without blocking.
Definition RtosMutex.cpp:49
bool Take(uint32_t timeout_ms=0) noexcept
Take (lock) the mutex with optional timeout.
Definition RtosMutex.h:128
void unlock_shared() noexcept
Definition RtosMutex.h:147
RtosMutex(const RtosMutex &)=delete
OS_Mutex handle_
Definition RtosMutex.h:150
RtosMutex() noexcept
Definition RtosMutex.cpp:14
bool lock_shared() noexcept
Definition RtosMutex.h:144
bool try_lock_for(uint32_t timeout_ms) noexcept
Try to lock the mutex with a timeout.
Definition RtosMutex.cpp:53
RtosMutex & operator=(const RtosMutex &)=delete
bool try_lock_shared_for(uint32_t timeout_ms) noexcept
Definition RtosMutex.h:146
~RtosMutex() noexcept
Definition RtosMutex.cpp:19
OS_Mutex native_handle() const noexcept
Get the native RTOS handle (platform-specific).
Definition RtosMutex.h:117
bool try_lock_shared() noexcept
Definition RtosMutex.h:145
void unlock() noexcept
Unlock the mutex.
Definition RtosMutex.cpp:58
bool lock() noexcept
Lock the mutex, blocking indefinitely.
Definition RtosMutex.cpp:45
void Give() noexcept
Give (unlock) the mutex.
Definition RtosMutex.h:138
Definition RtosMutex.h:267
RtosSharedLock & operator=(const RtosSharedLock &)=delete
void Unlock() noexcept
Definition RtosMutex.h:309
RtosSharedLock & operator=(RtosSharedLock &&other) noexcept
Definition RtosMutex.h:292
bool locked_
Definition RtosMutex.h:318
bool IsLocked() const noexcept
Definition RtosMutex.h:305
~RtosSharedLock() noexcept
Definition RtosMutex.h:278
RtosSharedLock(const RtosSharedLock &)=delete
RtosSharedLock(SharedMutexT &mutex, uint32_t timeout_ms=0) noexcept
Definition RtosMutex.h:269
RtosSharedLock(RtosSharedLock &&other) noexcept
Definition RtosMutex.h:287
SharedMutexT * mutex_
Definition RtosMutex.h:317
Definition RtosMutex.h:160
OS_Mutex writer_mutex_
Definition RtosMutex.h:190
bool lock() noexcept
Definition RtosMutex.cpp:108
bool try_lock_for(uint32_t timeout_ms) noexcept
Definition RtosMutex.cpp:132
bool lock_shared() noexcept
Definition RtosMutex.cpp:156
OS_Mutex reader_mutex_
Definition RtosMutex.h:191
bool try_lock() noexcept
Definition RtosMutex.cpp:119
RtosSharedMutex() noexcept
Definition RtosMutex.cpp:64
~RtosSharedMutex() noexcept
Definition RtosMutex.cpp:70
bool try_lock_shared_for(uint32_t timeout_ms) noexcept
Definition RtosMutex.cpp:184
void unlock() noexcept
Definition RtosMutex.cpp:151
void unlock_shared() noexcept
Definition RtosMutex.cpp:210
bool try_lock_shared() noexcept
Definition RtosMutex.cpp:171
std::atomic< int > readers_
Definition RtosMutex.h:192
std::atomic< bool > writer_active_
Definition RtosMutex.h:193
RtosSharedMutex & operator=(const RtosSharedMutex &)=delete
RtosSharedMutex(const RtosSharedMutex &)=delete
Definition RtosMutex.h:41
static uint64_t GetCurrentTimeUs() noexcept
Get current time in microseconds.
Definition RtosMutex.h:47
static uint32_t MsToTicks(uint32_t ms) noexcept
Convert milliseconds to RTOS ticks.
Definition RtosMutex.h:63
Definition RtosMutex.h:201
~RtosUniqueLock() noexcept
Definition RtosMutex.h:217
bool IsLocked() const noexcept
Check if the mutex was successfully locked.
Definition RtosMutex.h:245
RtosUniqueLock(MutexT &mutex, uint32_t timeout_ms=0) noexcept
Construct and lock the mutex.
Definition RtosMutex.h:208
RtosUniqueLock(RtosUniqueLock &&other) noexcept
Definition RtosMutex.h:226
bool locked_
Definition RtosMutex.h:259
void Unlock() noexcept
Manually unlock before destruction.
Definition RtosMutex.h:250
RtosUniqueLock & operator=(const RtosUniqueLock &)=delete
MutexT * mutex_
Definition RtosMutex.h:258
RtosUniqueLock & operator=(RtosUniqueLock &&other) noexcept
Definition RtosMutex.h:231
RtosUniqueLock(const RtosUniqueLock &)=delete
static OS_Ulong os_time_get(void)
Definition os_cmsis.h:59
osMutexId_t OS_Mutex
Definition os_cmsis.h:28