Skip to content
Snippets Groups Projects
Commit 9d871b4d authored by fxk8y's avatar fxk8y :spider:
Browse files

Adding fancy mutex manager

parent 9fa8c888
No related branches found
No related tags found
No related merge requests found
......@@ -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;
};
......
#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;
};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment