mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-20 22:12:27 +02:00
added scope lock
This commit is contained in:
@ -6,6 +6,8 @@
|
||||
#define SIMPLE_CXX_CLIENT_TERMINAL_OBJECTS_HPP
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
|
||||
|
||||
class esp_err_exception: virtual public std::exception {
|
||||
@ -44,6 +46,29 @@ static inline void throw_if_esp_fail(esp_err_t err)
|
||||
}
|
||||
}
|
||||
|
||||
struct Lock {
|
||||
explicit Lock(): lock(nullptr)
|
||||
{
|
||||
lock = xSemaphoreCreateMutex();
|
||||
throw_if_false(lock != nullptr, "create signal event group failed");
|
||||
}
|
||||
~Lock() { vSemaphoreDelete(lock); }
|
||||
void take() { xSemaphoreTake(lock, portMAX_DELAY); }
|
||||
|
||||
void give() { xSemaphoreGive(lock); }
|
||||
xSemaphoreHandle lock;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class Scoped {
|
||||
public:
|
||||
explicit Scoped(T &l):lock(l) { lock.take(); }
|
||||
~Scoped() { lock.give(); }
|
||||
|
||||
private:
|
||||
T& lock;
|
||||
};
|
||||
|
||||
struct signal_group {
|
||||
explicit signal_group(): event_group(nullptr)
|
||||
{
|
||||
|
Reference in New Issue
Block a user