DelegateMQ
Loading...
Searching...
No Matches
zephyr/Thread.h
Go to the documentation of this file.
1#ifndef _THREAD_ZEPHYR_H
2#define _THREAD_ZEPHYR_H
3
6
7#include "delegate/IThread.h"
8#include <zephyr/kernel.h>
9#include <string>
10#include <memory>
11
12class ThreadMsg;
13
14class Thread : public dmq::IThread
15{
16public:
17 Thread(const std::string& threadName);
19
21 void ExitThread();
22
23 // Note: k_tid_t is a struct k_thread* in Zephyr
24 k_tid_t GetThreadId();
25 static k_tid_t GetCurrentThreadId();
26 std::string GetThreadName() { return THREAD_NAME; }
27
28 virtual void DispatchDelegate(std::shared_ptr<dmq::DelegateMsg> msg) override;
29
30private:
31 Thread(const Thread&) = delete;
32 Thread& operator=(const Thread&) = delete;
33
34 // Thread entry point
35 static void Process(void* p1, void* p2, void* p3);
36 void Run();
37
38 // Zephyr Kernel Objects
39 struct k_thread m_thread;
40 struct k_msgq m_msgq;
41 struct k_sem m_exitSem; // Semaphore to signal thread completion
42
43 // Define pointer type for the message queue
44 using MsgPtr = ThreadMsg*;
45
46 // Custom deleter for Zephyr kernel memory (wraps k_free)
47 using ZephyrDeleter = void(*)(void*);
48
49 // Dynamically allocated stack and message queue buffer
50 // Managed by unique_ptr but allocated via k_aligned_alloc and freed via k_free
51 std::unique_ptr<char, ZephyrDeleter> m_stackMemory{nullptr, k_free};
52 std::unique_ptr<char, ZephyrDeleter> m_msgqBuffer{nullptr, k_free};
53
54 const std::string THREAD_NAME;
55
56 // Stack size in bytes
57 static const size_t STACK_SIZE = 2048;
58 // Max items in queue
59 static const size_t MSGQ_MAX_MSGS = 20;
60 // Size of one message item (the pointer)
61 static const size_t MSG_SIZE = sizeof(MsgPtr);
62};
63
64#endif // _THREAD_ZEPHYR_H
Interface for cross-thread delegate dispatching.
Cross-platform thread for any system supporting C++11 std::thread (e.g. Windows, Linux).
Definition cmsis-rtos2/Thread.h:33
std::string GetThreadName()
Definition zephyr/Thread.h:26
Thread(const std::string &threadName)
bool CreateThread()
Thread(const std::string &threadName, size_t maxQueueSize=0)
Definition cmsis-rtos2/Thread.cpp:16
static k_tid_t GetCurrentThreadId()
void ExitThread()
k_tid_t GetThreadId()
virtual void DispatchDelegate(std::shared_ptr< dmq::DelegateMsg > msg) override
Enqueues a delegate message for execution on this thread.
Definition qt/Thread.cpp:93
A class to hold a platform-specific thread messsage that will be passed through the OS message queue.
Definition cmsis-rtos2/ThreadMsg.h:12
A base class for a delegate enabled execution thread. Implemented by application code if asynchronous...
Definition IThread.h:22