10#include <freertos/FreeRTOS.h>
42template <
typename struct_type>
45 static constexpr const char* TAG =
"protected_struct";
48 mutable SemaphoreHandle_t mutex;
57 if (xSemaphoreGetMutexHolder(parent->mutex) == xTaskGetCurrentTaskHandle()) {
58 LOGE(
"Mutex deadlock detected, aborting");
62 xSemaphoreTake(parent->mutex, portMAX_DELAY);
73 if (parent !=
nullptr) {
74 xSemaphoreGive(parent->mutex);
80 return parent !=
nullptr ? &parent->data :
nullptr;
83 operator struct_type&() {
90 if (mutex ==
nullptr) {
91 LOGE(
"Mutex creation failed");
96 if (mutex !=
nullptr) {
97 vSemaphoreDelete(mutex);
111 return mutex_lock(
this);
120 return mutex_lock(
this);
protected_struct(const protected_struct &)=delete
mutex_lock operator->() const
Overload of the get value from dereferenced pointer operator to make it possible to get a value using...
Definition protected_struct.h:110
protected_struct & operator=(const protected_struct &)=delete
protected_struct()
Definition protected_struct.h:89
~protected_struct()
Definition protected_struct.h:95
mutex_lock lock() const
Locks the struct and holds the mutex for the returned object's scope.
Definition protected_struct.h:119
void halt_execution()
Definition halt_execution.h:13
#define LOGE(format,...)
Macro for error logging.
Definition logger.h:133