5#ifdef ROO_THREADS_USE_FREERTOS
10#include "freertos/FreeRTOS.h"
11#include "freertos/task.h"
28 mutex(const mutex&) = delete;
29 mutex& operator=(const mutex&) = delete;
40 void ensureInitialized() noexcept;
42 StaticSemaphore_t mutex_;
44#if ROO_THREADS_FREERTOS_LAZY_INITIALIZE
45 bool initialized_ =
false;
49template <
typename Mutex>
53 typedef Mutex mutex_type;
56 explicit lock_guard(mutex_type& mutex) : mutex_(mutex) { mutex_.lock(); }
59 ~lock_guard() { mutex_.unlock(); }
61 lock_guard(
const lock_guard&) =
delete;
62 lock_guard& operator=(
const lock_guard&) =
delete;
72 explicit defer_lock_t() =
default;
79 explicit try_to_lock_t() =
default;
87 explicit adopt_lock_t() =
default;
92inline constexpr defer_lock_t defer_lock{};
93inline constexpr try_to_lock_t try_to_lock{};
94inline constexpr adopt_lock_t adopt_lock{};
97void checkLockOwned(
bool owns);
98void checkLockUnowned(
const void* lock,
bool owns);
101template <
typename Mutex>
105 typedef Mutex mutex_type;
108 unique_lock() noexcept : lock_(
nullptr), owns_(false) {}
111 explicit unique_lock(mutex_type& mutex) : lock_(&mutex), owns_(false) {
117 unique_lock(mutex_type& mutex, defer_lock_t) noexcept
118 : lock_(&mutex), owns_(
false) {}
122 unique_lock(mutex_type& mutex, try_to_lock_t)
123 : lock_(&mutex), owns_(lock_->try_lock()) {}
127 unique_lock(mutex_type& mutex, adopt_lock_t) noexcept
128 : lock_(&mutex), owns_(
true) {}
132 unique_lock(mutex_type& mutex, roo_time::Uptime tp)
133 : lock_(&mutex), owns_(lock_->try_lock_until(tp)) {}
137 unique_lock(mutex_type& mutex, roo_time::Duration duration)
138 : lock_(&mutex), owns_(lock_->try_lock_for(duration)) {}
145 unique_lock(
const unique_lock&) =
delete;
146 unique_lock& operator=(
const unique_lock&) =
delete;
149 unique_lock(unique_lock&& lock) noexcept
150 : lock_(lock.lock_), owns_(lock.owns_) {
156 unique_lock& operator=(unique_lock&& lock)
noexcept {
159 unique_lock(std::move(lock)).swap(*
this);
169 internal::checkLockUnowned(lock_, owns_);
176 internal::checkLockUnowned(lock_, owns_);
177 owns_ = lock_->try_lock();
182 bool try_lock_until(roo_time::Uptime tp) {
183 internal::checkLockUnowned(lock_, owns_);
184 owns_ = lock_->try_lock_until(tp);
189 bool try_lock_for(roo_time::Duration duration) {
190 internal::checkLockUnowned(lock_, owns_);
191 owns_ = lock_->try_lock_for(duration);
197 internal::checkLockOwned(owns_);
198 if (lock_ !=
nullptr) {
205 void swap(unique_lock& other)
noexcept {
206 std::swap(lock_, other.lock_);
207 std::swap(owns_, other.owns_);
211 mutex_type* release() noexcept {
212 mutex_type* ret = lock_;
219 bool owns_lock() const noexcept {
return owns_; }
222 explicit operator bool() const noexcept {
return owns_lock(); }
225 mutex_type* mutex() const noexcept {
return lock_; }