Implement freertos mutex wrapper

This commit is contained in:
GHOSCHT 2023-03-25 18:09:28 +01:00
parent 96c39d4792
commit 48c2cdf97b
No known key found for this signature in database
2 changed files with 39 additions and 0 deletions

View file

@ -0,0 +1,20 @@
#include "Mutex.h"
#include <etl/utility.h>
freertos::Mutex::Mutex() : handle{xSemaphoreCreateMutex()} {}
freertos::Mutex::Mutex(Mutex &&other) noexcept {
handle = etl::move(other.handle);
}
freertos::Mutex::~Mutex() { vSemaphoreDelete(handle); }
auto freertos::Mutex::operator=(const Mutex &&other) noexcept -> Mutex & {
handle = etl::move(other.handle);
return *this;
}
auto freertos::Mutex::lock(TickType_t timeout) -> bool{
BaseType_t success = xSemaphoreTake(handle, timeout);
return success == pdTRUE ? true : false;
}
auto freertos::Mutex::unlock() -> bool {
BaseType_t success = xSemaphoreGive(handle);
return success == pdTRUE ? true : false;
}

View file

@ -0,0 +1,19 @@
#pragma once
#include <Arduino.h>
namespace freertos {
class Mutex {
public:
Mutex();
Mutex(const Mutex &other) = delete;
Mutex(Mutex &&other) noexcept;
~Mutex();
Mutex &operator=(const Mutex &other) = delete;
Mutex &operator=(const Mutex &&other) noexcept;
bool lock(TickType_t timeout = portMAX_DELAY);
bool unlock();
private:
SemaphoreHandle_t handle;
};
} // namespace freertos