-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32-event.hpp
62 lines (58 loc) · 1.66 KB
/
esp32-event.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef ESP32_EVENT_LIB_HPP
#define ESP32_EVENT_LIB_HPP
#include "esp32-event.h"
#include <utility>
//A bit more efficient than std::unique_ptr - doesn't check for null in dtor
template <class M>
struct uvAutoDel
{
M* mMsg;
uvAutoDel(M* aMsg): mMsg(aMsg){}
~uvAutoDel() { delete mMsg; }
};
template <class F>
void uvLoopExecAsync(uv_loop_t* loop, F&& func)
{
struct Message: public uv_message
{
F mFunc;
Message(F&& func, uv_msg_handler cfunc)
: uv_message(cfunc), mFunc(std::forward<F>(func)){}
};
Message* newmsg = new Message(std::forward<F>(func),
[](uv_message* msg)
{
auto custMsg = static_cast<Message*>(msg);
uvAutoDel<Message> autodel(custMsg);
custMsg->mFunc();
});
uvx_loop_post_message(loop, newmsg);
}
template <class F>
auto uvLoopExecSync(uv_loop_t* loop, F&& func) -> decltype(func())
{
class Message: public uv_message
{
F mFunc;
decltype(func()) mRet;
SemaphoreHandle_t mMutex;
StaticSemaphore_t mMutexMem;
Message(F&& func, uv_msg_handler cfunc)
: uv_message(cfunc), mFunc(std::forward<F>(func)),
mMutex(xSemaphoreCreateMutexStatic(&mMutexMem))
{}
~Message() { vSemaphoreDelete(mMutex); }
};
Message* newmsg = new Message(std::forward<F>(func),
[](uv_message* msg)
{
auto custMsg = static_cast<Message*>(msg);
custMsg->mRet = custMsg->mFunc();
xSemaphoreGive(custMsg->mMutex);
});
uv_loop_post_message(loop, newmsg);
while(xSemaphoreTake(newmsg->mMutex, portMAX_DELAY) != pdPASS);
uvAutoDel<Message> autodel(newmsg);
return newmsg->mRet;
}
#endif