From 9d871b4d646b388c2408b3f3980e29a654bee9ea Mon Sep 17 00:00:00 2001 From: Jochen Vothknecht <jochen3120@gmail.com> Date: Fri, 20 May 2022 11:11:51 +0200 Subject: [PATCH] Adding fancy mutex manager --- CLC-qthing/SiliconTorch/NVSExplorer.hpp | 4 +- CLC-qthing/SpiderLib/ManagedMutex.hpp | 51 +++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 CLC-qthing/SpiderLib/ManagedMutex.hpp diff --git a/CLC-qthing/SiliconTorch/NVSExplorer.hpp b/CLC-qthing/SiliconTorch/NVSExplorer.hpp index cad8a61..9ddddde 100644 --- a/CLC-qthing/SiliconTorch/NVSExplorer.hpp +++ b/CLC-qthing/SiliconTorch/NVSExplorer.hpp @@ -14,7 +14,7 @@ // #include "driver/uart.h" // project specific -// #include "" +#include "SpiderLib/ManagedMutex.hpp" // qthing stuff // #include <qthing> @@ -56,7 +56,7 @@ namespace SiliconTorch { NVSExplorer(); - + SpiderLib::ManagedMutex mutex; }; diff --git a/CLC-qthing/SpiderLib/ManagedMutex.hpp b/CLC-qthing/SpiderLib/ManagedMutex.hpp new file mode 100644 index 0000000..726e900 --- /dev/null +++ b/CLC-qthing/SpiderLib/ManagedMutex.hpp @@ -0,0 +1,51 @@ +#pragma once + +// C++ system level +#include <functional> + +// ESP32 specific +#include "esp_log.h" +#include "FreeRTOS.h" +#include "freertos/semphr.h" + +// project specific +// #include <SpiderLib/Util.hpp> + + +namespace SpiderLib { + + class ManagedMutex { + + public: + ManagedMutex() { + + semphr = xSemaphoreCreateBinary(); + + if (semphr == NULL) { + ESP_LOGE("ManagedMutex", "Memory allocation FAILED"); + abort(); + } + + xSemaphoreGive(semphr); + } + + template<typename T> + T run(std::function<T()> body) { + + // TODO: how infinitely is portMAX_DELAY really…? + while (xSemaphoreTake(semphr, portMAX_DELAY) == pdFALSE) {}; + + T result = body(); + + xSemaphoreGive(semphr); + + return result; + } + + private: + + SemaphoreHandle_t semphr; + + }; + +} -- GitLab