6#include <zephyr/kernel.h>
7#include <zephyr/logging/log.h>
16#define ZCT_EVENT_THREAD_LOG_LEVEL LOG_LEVEL_WRN
36template <
typename EventType>
46 using MsgQueueItem = std::variant<EventType, std::function<void()>>;
64 k_thread_stack_t* threadStack,
65 size_t threadStackSize,
67 size_t eventQueueBufferNumItems
75 LOG_DBG(
"EventThread constructor called.");
93 LOG_DBG(
"%s() called.", __FUNCTION__);
106 LOG_DBG(
"EventThread start() called.");
220 LOG_DBG(
"%s() called.", __FUNCTION__);
227 while (nextTimerInfo.m_timer !=
nullptr && nextTimerInfo.m_durationToWaitUs == 0) {
228 LOG_DBG(
"Timer expired. Timer: %p.", nextTimerInfo.m_timer);
230 nextTimerInfo.m_timer->updateAfterExpiry();
233 const auto& callback = nextTimerInfo.m_timer->getExpiryCallback();
249 if (nextTimerInfo.m_timer !=
nullptr) {
250 timeout = Z_TIMEOUT_US(nextTimerInfo.m_durationToWaitUs);
261 if (std::holds_alternative<EventType>(msgQueueItem)) {
263 const auto&
event = std::get<EventType>(msgQueueItem);
272 LOG_WRN(
"Received external event in event thread \"%s\" but no external event callback is registered.",
m_name);
276 std::get<std::function<void()>>(msgQueueItem)();
279 }
else if (queueRc == -EAGAIN) {
282 LOG_DBG(
"Queue timed out, which means we need to handle timer expiry.");
284 }
else if (queueRc == -ENOMSG) {
286 __ASSERT(
false,
"Got -ENOMSG from queue, was not expecting this.");
288 __ASSERT(
false,
"Got unexpected return code from queue: %d.", queueRc);
#define ZCT_EVENT_THREAD_LOG_LEVEL
Definition EventThread.hpp:16
Use this class in your objects to create a thread that can wait for events.
Definition EventThread.hpp:37
const char * m_name
Definition EventThread.hpp:293
size_t m_threadStackSize
Definition EventThread.hpp:300
void onExternalEvent(std::function< void(const EventType &)> callback)
Definition EventThread.hpp:135
EventThread(const char *name, k_thread_stack_t *threadStack, size_t threadStackSize, int threadPriority, size_t eventQueueBufferNumItems)
Definition EventThread.hpp:62
void exitEventLoop()
Definition EventThread.hpp:177
int m_threadPriority
Definition EventThread.hpp:301
void sendEvent(const EventType &event)
Definition EventThread.hpp:146
std::variant< EventType, std::function< void()> > MsgQueueItem
Definition EventThread.hpp:46
void runEventLoop()
Definition EventThread.hpp:218
~EventThread()
Definition EventThread.hpp:91
void * m_msgQueueBuffer
Definition EventThread.hpp:294
TimerManager & timerManager()
Definition EventThread.hpp:163
void start()
Definition EventThread.hpp:104
static void staticThreadFunction(void *arg1, void *arg2, void *arg3)
Definition EventThread.hpp:195
struct k_thread m_thread
Definition EventThread.hpp:296
struct k_msgq m_threadMsgQueue
Definition EventThread.hpp:297
std::function< void(const EventType &)> m_externalEventCallback
Definition EventThread.hpp:304
TimerManager m_timerManager
Definition EventThread.hpp:303
void runInLoop(std::function< void()> func)
Definition EventThread.hpp:187
k_thread_stack_t * m_threadStack
Definition EventThread.hpp:299
bool m_exitEventLoop
Definition EventThread.hpp:309
Definition TimerManager.hpp:35
TimerExpiryInfo getNextExpiringTimer()
Definition TimerManager.hpp:94