Skip to content

Commit

Permalink
Add thread lock, not applied
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Feb 10, 2025
1 parent 2e1730b commit df7ff5e
Show file tree
Hide file tree
Showing 6 changed files with 326 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/platform/SensorLockBase.hpp
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
* @date 2025-02-10
*
*/
#pragma once

#include "../SensorLib.h"

class SensorLockBase
{
public:
virtual void lock() = 0;
virtual void unlock() = 0;
};
47 changes: 47 additions & 0 deletions src/platform/SensorSmartLock.hpp
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
* @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();
}
};
64 changes: 64 additions & 0 deletions src/platform/arduino/SensorArduinoFreeRTOS_Lock.hpp
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
* @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*/
55 changes: 55 additions & 0 deletions src/platform/arduino/SensorArduinoMbed_Lock.hpp
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
* @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*/
57 changes: 57 additions & 0 deletions src/platform/arduino/SensorArduinoZephyr_Lock.hpp
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
* @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*/
64 changes: 64 additions & 0 deletions src/platform/espidf/SensorEspIDF_Lock.hpp
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
* @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*/

0 comments on commit df7ff5e

Please sign in to comment.