From df7ff5ef08c3943187816a70baa5a268b9033611 Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Mon, 10 Feb 2025 16:52:24 +0800 Subject: [PATCH] Add thread lock, not applied --- src/platform/SensorLockBase.hpp | 39 +++++++++++ src/platform/SensorSmartLock.hpp | 47 ++++++++++++++ .../arduino/SensorArduinoFreeRTOS_Lock.hpp | 64 +++++++++++++++++++ .../arduino/SensorArduinoMbed_Lock.hpp | 55 ++++++++++++++++ .../arduino/SensorArduinoZephyr_Lock.hpp | 57 +++++++++++++++++ src/platform/espidf/SensorEspIDF_Lock.hpp | 64 +++++++++++++++++++ 6 files changed, 326 insertions(+) create mode 100644 src/platform/SensorLockBase.hpp create mode 100644 src/platform/SensorSmartLock.hpp create mode 100644 src/platform/arduino/SensorArduinoFreeRTOS_Lock.hpp create mode 100644 src/platform/arduino/SensorArduinoMbed_Lock.hpp create mode 100644 src/platform/arduino/SensorArduinoZephyr_Lock.hpp create mode 100644 src/platform/espidf/SensorEspIDF_Lock.hpp diff --git a/src/platform/SensorLockBase.hpp b/src/platform/SensorLockBase.hpp new file mode 100644 index 0000000..2230a64 --- /dev/null +++ b/src/platform/SensorLockBase.hpp @@ -0,0 +1,39 @@ +/** + * + * @license MIT License + * + * Copyright (c) 2025 lewis he + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * @file SensorLockBase.hpp + * @author Lewis He (lewishe@outlook.com) + * @date 2025-02-10 + * + */ +#pragma once + +#include "../SensorLib.h" + +class SensorLockBase +{ +public: + virtual void lock() = 0; + virtual void unlock() = 0; +}; diff --git a/src/platform/SensorSmartLock.hpp b/src/platform/SensorSmartLock.hpp new file mode 100644 index 0000000..d136be3 --- /dev/null +++ b/src/platform/SensorSmartLock.hpp @@ -0,0 +1,47 @@ +/** + * + * @license MIT License + * + * Copyright (c) 2025 lewis he + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * @file SensorSmartLock.hpp + * @author Lewis He (lewishe@outlook.com) + * @date 2025-02-10 + * + */ +#pragma once + +#include "SensorLockBase.hpp" + +class SensorSmartLock +{ +private: + SensorLockBase &lock; +public: + SensorSmartLock(SensorLockBase &l) : lock(l) + { + lock.lock(); + } + ~SensorSmartLock() + { + lock.unlock(); + } +}; diff --git a/src/platform/arduino/SensorArduinoFreeRTOS_Lock.hpp b/src/platform/arduino/SensorArduinoFreeRTOS_Lock.hpp new file mode 100644 index 0000000..42e09fa --- /dev/null +++ b/src/platform/arduino/SensorArduinoFreeRTOS_Lock.hpp @@ -0,0 +1,64 @@ +/** + * + * @license MIT License + * + * Copyright (c) 2025 lewis he + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * @file SensorArduinoFreeRTOS_Lock.hpp + * @author Lewis He (lewishe@outlook.com) + * @date 2025-02-10 + * + */ +#pragma once + +#include "../SensorSmartLock.hpp" + +#if defined(ARDUINO_ARCH_ESP32) + +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" + +class FreeRTOSLock : public SensorLockBase +{ +private: + SemaphoreHandle_t mutex; +public: + FreeRTOSLock() + { + mutex = xSemaphoreCreateMutex(); + } + ~FreeRTOSLock() + { + vSemaphoreDelete(mutex); + } + void lock() override + { + xSemaphoreTake(mutex, portMAX_DELAY); + } + void unlock() override + { + xSemaphoreGive(mutex); + } +}; + +using HalLock = FreeRTOSLock; + +#endif /*ARDUINO_ARCH_ESP32*/ diff --git a/src/platform/arduino/SensorArduinoMbed_Lock.hpp b/src/platform/arduino/SensorArduinoMbed_Lock.hpp new file mode 100644 index 0000000..878c8e0 --- /dev/null +++ b/src/platform/arduino/SensorArduinoMbed_Lock.hpp @@ -0,0 +1,55 @@ +/** + * + * @license MIT License + * + * Copyright (c) 2025 lewis he + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * @file SensorArduinoMbed_Lock.hpp + * @author Lewis He (lewishe@outlook.com) + * @date 2025-02-10 + * + */ +#pragma once + +#include "../SensorLockBase.hpp" + +#if defined(ARDUINO_ARCH_MBED) + +#include "Mutex.h" + +class MbedLock : public SensorLockBase +{ +private: + Mutex mutex; +public: + void lock() override + { + mutex.lock(); + } + void unlock() override + { + mutex.unlock(); + } +}; + +using HalLock = MbedLock; + +#endif /*ARDUINO_ARCH_MBED*/ diff --git a/src/platform/arduino/SensorArduinoZephyr_Lock.hpp b/src/platform/arduino/SensorArduinoZephyr_Lock.hpp new file mode 100644 index 0000000..b68e275 --- /dev/null +++ b/src/platform/arduino/SensorArduinoZephyr_Lock.hpp @@ -0,0 +1,57 @@ +/** + * + * @license MIT License + * + * Copyright (c) 2025 lewis he + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * @file SensorArduinoZephyr_Lock.hpp + * @author Lewis He (lewishe@outlook.com) + * @date 2025-02-10 + * + */ +#pragma once + +#include "../SensorLockBase.hpp" + +#if defined(ARDUINO_ARCH_ZEPHYR) + +class ZephyrLock : public SensorLockBase +{ +private: + struct k_mutex mutex; +public: + ZephyrLock() + { + k_mutex_init(&mutex); + } + void lock() override + { + k_mutex_lock(&mutex, K_FOREVER); + } + void unlock() override + { + k_mutex_unlock(&mutex); + } +}; + +using HalLock = ZephyrLock; + +#endif /*ARDUINO_ARCH_ZEPHYR*/ diff --git a/src/platform/espidf/SensorEspIDF_Lock.hpp b/src/platform/espidf/SensorEspIDF_Lock.hpp new file mode 100644 index 0000000..71c7302 --- /dev/null +++ b/src/platform/espidf/SensorEspIDF_Lock.hpp @@ -0,0 +1,64 @@ +/** + * + * @license MIT License + * + * Copyright (c) 2025 lewis he + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * @file SensorEspIDF_Lock.hpp + * @author Lewis He (lewishe@outlook.com) + * @date 2025-02-10 + * + */ +#pragma once + +#include "../SensorSmartLock.hpp" + +#if !defined(ARDUINO) && defined(ESP_PLATFORM) + +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" + +class FreeRTOSLock : public SensorLockBase +{ +private: + SemaphoreHandle_t mutex; +public: + FreeRTOSLock() + { + mutex = xSemaphoreCreateMutex(); + } + ~FreeRTOSLock() + { + vSemaphoreDelete(mutex); + } + void lock() override + { + xSemaphoreTake(mutex, portMAX_DELAY); + } + void unlock() override + { + xSemaphoreGive(mutex); + } +}; + +using HalLock = FreeRTOSLock; + +#endif /*ESP_PLATFORM*/