Implement freertos mutex wrapper
This commit is contained in:
parent
96c39d4792
commit
48c2cdf97b
2 changed files with 39 additions and 0 deletions
20
modules/control/Firmware/src/FreeRTOS/Mutex.cpp
Normal file
20
modules/control/Firmware/src/FreeRTOS/Mutex.cpp
Normal 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;
|
||||||
|
}
|
19
modules/control/Firmware/src/FreeRTOS/Mutex.h
Normal file
19
modules/control/Firmware/src/FreeRTOS/Mutex.h
Normal 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
|
Reference in a new issue