Dawn Framework 1.0
Universal data acquisition framework for embedded systems
thread.hxx
1// dawn/include/dawn/common/thread.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include <atomic>
9#include <cstddef>
10#include <cstdint>
11#include <functional>
12#include <pthread.h>
13#include <utility>
14
15#include "dawn/porting/config.hxx"
16
17namespace dawn
18{
32{
33public:
40 static constexpr int THREAD_PRIORITY_DEFAULT = 0;
41
48 static constexpr int THREAD_SCHEDULER_DEFAULT = -1;
49
55 {
56 size_t stackSize;
59
60 constexpr SThreadConfig(size_t stackSizeBytes = 0,
61 int priorityValue = THREAD_PRIORITY_DEFAULT,
62 int schedulerPolicy = THREAD_SCHEDULER_DEFAULT)
63 : stackSize(stackSizeBytes)
64 , priority(priorityValue)
65 , scheduler(schedulerPolicy)
66 {
67 }
68 };
69
75 : th()
76 , thCreated(false)
77 , thQuit(true)
78 , thQuitDone(true)
79 , threadFunc(nullptr)
80 , threadConfig()
81 {
82 }
83
84 CThreadedObject(const CThreadedObject &) = delete;
85 CThreadedObject &operator=(const CThreadedObject &) = delete;
86
91 virtual ~CThreadedObject();
92
99 template<typename Func>
100 void setThreadFunc(Func &&func)
101 {
102 threadFunc = std::forward<Func>(func);
103 }
104
109 void setThreadConfig(const SThreadConfig &config)
110 {
111 threadConfig = config;
112 }
113
119 {
120 return threadConfig;
121 }
122
129 void setThreadStackSize(size_t stackSize)
130 {
131 threadConfig.stackSize = stackSize;
132 }
133
138 size_t getThreadStackSize() const
139 {
140 return threadConfig.stackSize;
141 }
142
149 void setThreadPriority(int priority)
150 {
151 threadConfig.priority = priority;
152 }
153
159 {
160 return threadConfig.priority;
161 }
162
169 void setThreadScheduler(int scheduler)
170 {
171 threadConfig.scheduler = scheduler;
172 }
173
179 {
180 return threadConfig.scheduler;
181 }
182
189 int threadStart();
190
197 int threadStop();
198
205 bool isRunning() const;
206
207 bool shouldQuit() const
208 {
209 return thQuit.load();
210 }
211
212 bool isStopped() const
213 {
214 return thQuitDone.load();
215 }
216
217 bool hasThreadObject() const
218 {
219 return thCreated;
220 }
221
222 void requestStop()
223 {
224 thQuit = true;
225 }
226
227 void clearStopRequest()
228 {
229 thQuit = false;
230 }
231
232 void markThreadFinished()
233 {
234 thQuitDone = true;
235 }
236
237protected:
245 template<typename Func>
246 int startWorkerThread(Func &&func)
247 {
248 setThreadFunc(std::forward<Func>(func));
249 return threadStart();
250 }
251
259 {
260 return threadStop();
261 }
262
270 {
271 return isRunning();
272 }
273
281 {
282 return *this;
283 }
284
292 {
293 return *this;
294 }
295
296private:
297 pthread_t th;
298 bool thCreated;
299 std::atomic_bool thQuit;
300 std::atomic_bool thQuitDone;
301 std::function<void()> threadFunc;
302 SThreadConfig threadConfig;
303
304 int joinThread();
305 int buildThreadAttr(pthread_attr_t &attr, bool &needsDestroy) const;
306 void threadWrapper();
307 static void *threadEntry(void *arg);
308};
309
310} // Namespace dawn
Portable thread owner abstraction for Dawn components.
Definition thread.hxx:32
bool isRunning() const
Check if the worker thread is running.
Definition thread.cxx:256
void setThreadScheduler(int scheduler)
Configure worker thread scheduler policy.
Definition thread.hxx:169
const CThreadedObject & workerThread() const
Get a const reference to this thread controller.
Definition thread.hxx:291
int threadStop()
Stop the worker thread.
Definition thread.cxx:240
virtual ~CThreadedObject()
Destructor - cleans up thread resources.
Definition thread.cxx:16
CThreadedObject()
Constructor - initializes thread management state.
Definition thread.hxx:74
size_t getThreadStackSize() const
Get configured worker thread stack size.
Definition thread.hxx:138
void setThreadStackSize(size_t stackSize)
Configure worker thread stack size.
Definition thread.hxx:129
static int THREAD_SCHEDULER_DEFAULT
Default scheduler behavior.
Definition thread.hxx:48
bool workerThreadRunning() const
Check if the worker thread is running.
Definition thread.hxx:269
const SThreadConfig & getThreadConfig() const
Get current thread configuration.
Definition thread.hxx:118
int threadStart()
Start the worker thread.
Definition thread.cxx:166
int getThreadScheduler() const
Get configured worker thread scheduler policy.
Definition thread.hxx:178
void setThreadFunc(Func &&func)
Assign the function executed by threadStart().
Definition thread.hxx:100
int stopWorkerThread()
Stop the worker thread.
Definition thread.hxx:258
int getThreadPriority() const
Get configured worker thread priority.
Definition thread.hxx:158
int startWorkerThread(Func &&func)
Start the worker thread with a given function.
Definition thread.hxx:246
void setThreadPriority(int priority)
Configure worker thread priority.
Definition thread.hxx:149
CThreadedObject & workerThread()
Get a reference to this thread controller.
Definition thread.hxx:280
static int THREAD_PRIORITY_DEFAULT
Default thread priority behavior.
Definition thread.hxx:40
void setThreadConfig(const SThreadConfig &config)
Replace the full thread configuration.
Definition thread.hxx:109
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Per-thread runtime configuration.
Definition thread.hxx:55
int priority
Requested thread priority (0 = creator default).
Definition thread.hxx:57
size_t stackSize
Requested stack size in bytes (0 = OS default).
Definition thread.hxx:56
int scheduler
Requested scheduler policy (-1 = creator default).
Definition thread.hxx:58