12#include <freertos/FreeRTOS.h>
13#include <freertos/portmacro.h>
14#include <freertos/semphr.h>
26 explicit MutexLock(SemaphoreHandle_t mutex) : _mutex(mutex)
28 if (this->_mutex !=
nullptr)
30 xSemaphoreTake(this->_mutex, portMAX_DELAY);
35 if (this->_mutex !=
nullptr)
37 xSemaphoreGive(this->_mutex);
44 SemaphoreHandle_t _mutex;
57 explicit SpinGuarded(
const T& initial) : _value(initial) {}
64 const Section section(&this->_lock);
71 const Section section(&this->_lock);
76 template <
typename F>
decltype(
auto)
update(F&& f)
78 const Section section(&this->_lock);
79 return f(this->_value);
83 template <
typename F>
decltype(
auto)
inspect(F&& f)
const
85 const Section section(&this->_lock);
86 return f(this->_value);
94 explicit Section(portMUX_TYPE* lock) : _lock(lock) { portENTER_CRITICAL(this->_lock); }
95 ~Section() { portEXIT_CRITICAL(this->_lock); }
96 Section(
const Section&) =
delete;
97 Section& operator=(
const Section&) =
delete;
103 mutable portMUX_TYPE _lock = portMUX_INITIALIZER_UNLOCKED;
Scoped lock on a FreeRTOS mutex; takes it forever on construction and gives it back on destruction.
Definition sync.h:24
A value of type T that owns its own spinlock; every access is one short critical section.
Definition sync.h:54
decltype(auto) inspect(F &&f) const
Runs f(const T&) under the lock and returns its result; use it to copy out one field.
Definition sync.h:83
T load() const
Returns a copy of the value, made under the lock.
Definition sync.h:62
decltype(auto) update(F &&f)
Runs f(T&) under the lock and returns its result (void is fine).
Definition sync.h:76
void store(const T &value)
Replaces the value under the lock.
Definition sync.h:69