From 3ff75471f64f51dff5329daff1c269c5ea9696b1 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Sat, 25 Mar 2023 18:09:28 +0100 Subject: [PATCH] Implement freertos mutex wrapper --- Firmware/src/FreeRTOS/Mutex.cpp | 20 ++++++++++++++++++++ Firmware/src/FreeRTOS/Mutex.h | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 Firmware/src/FreeRTOS/Mutex.cpp create mode 100644 Firmware/src/FreeRTOS/Mutex.h diff --git a/Firmware/src/FreeRTOS/Mutex.cpp b/Firmware/src/FreeRTOS/Mutex.cpp new file mode 100644 index 0000000..7f8683a --- /dev/null +++ b/Firmware/src/FreeRTOS/Mutex.cpp @@ -0,0 +1,20 @@ +#include "Mutex.h" +#include + +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; +} \ No newline at end of file diff --git a/Firmware/src/FreeRTOS/Mutex.h b/Firmware/src/FreeRTOS/Mutex.h new file mode 100644 index 0000000..77ec4b8 --- /dev/null +++ b/Firmware/src/FreeRTOS/Mutex.h @@ -0,0 +1,19 @@ +#pragma once +#include + +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