Added lots of new utilities
This commit is contained in:
33
binary_semaphore.h
Normal file
33
binary_semaphore.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
// espressif includes
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
// local includes
|
||||
#include "esputils.h"
|
||||
|
||||
namespace espcpputils {
|
||||
class binary_semaphore
|
||||
{
|
||||
ESP_DISABLE_COPY_MOVE(binary_semaphore)
|
||||
|
||||
public:
|
||||
binary_semaphore() :
|
||||
handle{xSemaphoreCreateBinary()}
|
||||
{}
|
||||
|
||||
~binary_semaphore() { if (handle) vSemaphoreDelete(handle); }
|
||||
|
||||
TaskHandle_t getMutexHolder() const { return xSemaphoreGetMutexHolder(handle); }
|
||||
|
||||
UBaseType_t getCount() const { return uxSemaphoreGetCount(handle); }
|
||||
|
||||
bool take(TickType_t xTicksToWait) { return xSemaphoreTake(handle, xTicksToWait); }
|
||||
|
||||
bool give() { return xSemaphoreGive(handle); }
|
||||
|
||||
const SemaphoreHandle_t handle;
|
||||
};
|
||||
} // namespace espcpputils
|
33
counting_semaphore.h
Normal file
33
counting_semaphore.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
// espressif includes
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
// local includes
|
||||
#include "esputils.h"
|
||||
|
||||
namespace espcpputils {
|
||||
class counting_semaphore
|
||||
{
|
||||
ESP_DISABLE_COPY_MOVE(counting_semaphore)
|
||||
|
||||
public:
|
||||
counting_semaphore(UBaseType_t uxMaxCount, UBaseType_t uxInitialCount) :
|
||||
handle{xSemaphoreCreateCounting(uxMaxCount, uxInitialCount)}
|
||||
{}
|
||||
|
||||
~counting_semaphore() { if (handle) vSemaphoreDelete(handle); }
|
||||
|
||||
TaskHandle_t getMutexHolder() const { return xSemaphoreGetMutexHolder(handle); }
|
||||
|
||||
UBaseType_t getCount() const { return uxSemaphoreGetCount(handle); }
|
||||
|
||||
bool take(TickType_t xTicksToWait) { return xSemaphoreTake(handle, xTicksToWait); }
|
||||
|
||||
bool give() { return xSemaphoreGive(handle); }
|
||||
|
||||
const SemaphoreHandle_t handle;
|
||||
};
|
||||
} // namespace espcpputils
|
@@ -106,6 +106,4 @@ public:
|
||||
private:
|
||||
std::vector<Slot> m_slots;
|
||||
};
|
||||
|
||||
|
||||
} // namespace espcpputils
|
||||
|
52
lockhelper.h
Normal file
52
lockhelper.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
// espressif includes
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
// local includes
|
||||
#include "esputils.h"
|
||||
|
||||
namespace espcpputils {
|
||||
class LockHelper
|
||||
{
|
||||
ESP_DISABLE_COPY_MOVE(LockHelper)
|
||||
|
||||
public:
|
||||
LockHelper(QueueHandle_t _xMutex, TickType_t xTicksToWait=portMAX_DELAY) :
|
||||
xMutex{_xMutex}
|
||||
{
|
||||
locked = xSemaphoreTakeRecursive(xMutex, xTicksToWait);
|
||||
}
|
||||
|
||||
~LockHelper()
|
||||
{
|
||||
if (locked)
|
||||
xSemaphoreGiveRecursive(xMutex);
|
||||
}
|
||||
|
||||
bool lock(TickType_t xTicksToWait=portMAX_DELAY)
|
||||
{
|
||||
if (locked)
|
||||
return false;
|
||||
|
||||
locked = xSemaphoreTakeRecursive(xMutex, xTicksToWait);
|
||||
|
||||
return locked;
|
||||
}
|
||||
|
||||
bool unlock()
|
||||
{
|
||||
if (!locked)
|
||||
return false;
|
||||
|
||||
locked = !xSemaphoreGiveRecursive(xMutex);
|
||||
|
||||
return locked;
|
||||
}
|
||||
|
||||
private:
|
||||
const QueueHandle_t xMutex;
|
||||
bool locked;
|
||||
};
|
||||
} // namespace espcpputils
|
71
lockingqueue.h
Normal file
71
lockingqueue.h
Normal file
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
|
||||
// system includes
|
||||
#include <optional>
|
||||
|
||||
// espressif includes
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
// local includes
|
||||
#include "esputils.h"
|
||||
#include "delayedconstruction.h"
|
||||
#include "recursive_mutex_semaphore.h"
|
||||
#include "recursivelockhelper.h"
|
||||
|
||||
namespace espcpputils {
|
||||
template<typename T>
|
||||
class LockingQueue
|
||||
{
|
||||
public:
|
||||
void push(const T &val);
|
||||
void push(T &&val);
|
||||
|
||||
std::optional<T> tryPop();
|
||||
|
||||
void clear();
|
||||
|
||||
std::size_t size() const { return m_size; }
|
||||
|
||||
private:
|
||||
espcpputils::recursive_mutex_semaphore m_lock;
|
||||
std::vector<T> m_queue;
|
||||
std::size_t m_size{}; // double-buffered to allow for reading without taking a lock
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void LockingQueue<T>::push(const T &val)
|
||||
{
|
||||
RecursiveLockHelper helper{m_lock.handle};
|
||||
m_queue.push_back(val);
|
||||
m_size = m_queue.size();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void LockingQueue<T>::push(T &&val)
|
||||
{
|
||||
RecursiveLockHelper helper{m_lock.handle};
|
||||
m_queue.emplace_back(std::move(val));
|
||||
m_size = m_queue.size();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> LockingQueue<T>::tryPop()
|
||||
{
|
||||
RecursiveLockHelper helper{m_lock.handle};
|
||||
if (m_queue.empty())
|
||||
return std::nullopt;
|
||||
|
||||
std::optional<T> temp = std::move(m_queue.front());
|
||||
m_queue.erase(std::begin(m_queue));
|
||||
m_size = m_queue.size();
|
||||
return temp;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void LockingQueue<T>::clear()
|
||||
{
|
||||
RecursiveLockHelper helper{m_lock.handle};
|
||||
m_queue.clear();
|
||||
}
|
||||
} // namespace espcpputils
|
33
mutex_semaphore.h
Normal file
33
mutex_semaphore.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
// espressif includes
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
// local includes
|
||||
#include "esputils.h"
|
||||
|
||||
namespace espcpputils {
|
||||
class mutex_semaphore
|
||||
{
|
||||
ESP_DISABLE_COPY_MOVE(mutex_semaphore)
|
||||
|
||||
public:
|
||||
mutex_semaphore() :
|
||||
handle{xSemaphoreCreateMutex()}
|
||||
{}
|
||||
|
||||
~mutex_semaphore() { if (handle) vSemaphoreDelete(handle); }
|
||||
|
||||
TaskHandle_t getMutexHolder() const { return xSemaphoreGetMutexHolder(handle); }
|
||||
|
||||
UBaseType_t getCount() const { return uxSemaphoreGetCount(handle); }
|
||||
|
||||
bool take(TickType_t xTicksToWait) { return xSemaphoreTake(handle, xTicksToWait); }
|
||||
|
||||
bool give() { return xSemaphoreGive(handle); }
|
||||
|
||||
const SemaphoreHandle_t handle;
|
||||
};
|
||||
} // namespace espcpputils
|
33
recursive_mutex_semaphore.h
Normal file
33
recursive_mutex_semaphore.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
// espressif includes
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
// local includes
|
||||
#include "esputils.h"
|
||||
|
||||
namespace espcpputils {
|
||||
class recursive_mutex_semaphore
|
||||
{
|
||||
ESP_DISABLE_COPY_MOVE(recursive_mutex_semaphore)
|
||||
|
||||
public:
|
||||
recursive_mutex_semaphore() :
|
||||
handle{xSemaphoreCreateRecursiveMutex()}
|
||||
{}
|
||||
|
||||
~recursive_mutex_semaphore() { if (handle) vSemaphoreDelete(handle); }
|
||||
|
||||
TaskHandle_t getMutexHolder() const { return xSemaphoreGetMutexHolder(handle); }
|
||||
|
||||
UBaseType_t getCount() const { return uxSemaphoreGetCount(handle); }
|
||||
|
||||
bool takeRecursive(TickType_t xTicksToWait) { return xSemaphoreTakeRecursive(handle, xTicksToWait); }
|
||||
|
||||
bool giveRecursive() { return xSemaphoreGiveRecursive(handle); }
|
||||
|
||||
const SemaphoreHandle_t handle;
|
||||
};
|
||||
} // namespace espcpputils
|
52
recursivelockhelper.h
Normal file
52
recursivelockhelper.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
// espressif includes
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
// local includes
|
||||
#include "esputils.h"
|
||||
|
||||
namespace espcpputils {
|
||||
class RecursiveLockHelper
|
||||
{
|
||||
ESP_DISABLE_COPY_MOVE(RecursiveLockHelper)
|
||||
|
||||
public:
|
||||
RecursiveLockHelper(QueueHandle_t _xMutex, TickType_t xTicksToWait=portMAX_DELAY) :
|
||||
xMutex{_xMutex}
|
||||
{
|
||||
locked = xSemaphoreTakeRecursive(xMutex, xTicksToWait);
|
||||
}
|
||||
|
||||
~RecursiveLockHelper()
|
||||
{
|
||||
if (locked)
|
||||
xSemaphoreGiveRecursive(xMutex);
|
||||
}
|
||||
|
||||
bool lock(TickType_t xTicksToWait=portMAX_DELAY)
|
||||
{
|
||||
if (locked)
|
||||
return false;
|
||||
|
||||
locked = xSemaphoreTakeRecursive(xMutex, xTicksToWait);
|
||||
|
||||
return locked;
|
||||
}
|
||||
|
||||
bool unlock()
|
||||
{
|
||||
if (!locked)
|
||||
return false;
|
||||
|
||||
locked = !xSemaphoreGiveRecursive(xMutex);
|
||||
|
||||
return locked;
|
||||
}
|
||||
|
||||
private:
|
||||
const QueueHandle_t xMutex;
|
||||
bool locked;
|
||||
};
|
||||
} // namespace espcpputils
|
Reference in New Issue
Block a user