RTOS Wrapper
API reference
Loading...
Searching...
No Matches
BaseThread.h
Go to the documentation of this file.
1
13#ifndef BaseThread_H
14#define BaseThread_H
15
16#include <memory>
17#include <atomic>
18#include "OsAbstraction.h"
19#include "OsUtility.h"
20#include "SignalSemaphore.h"
21
32{
33public:
39 BaseThread(const char *threadName);
40
45 BaseThread(const BaseThread&) = delete;
46
51 BaseThread& operator =(const BaseThread&) = delete;
52
58 virtual ~BaseThread() noexcept;
59
66 {
67 return initialized ? &osThread : nullptr;
68 }
69
75 inline bool IsInitialized() const noexcept
76 {
77 return initialized;
78 }
79
88 bool EnsureInitialized() noexcept
89 {
90 if (!initialized)
91 {
93 }
94 return initialized;
95 }
96
101 bool Suspend() noexcept;
102
108 bool Resume() noexcept;
109
115 bool IsSuspended() noexcept;
116
117 bool IsThreadCreated() noexcept { return osThreadCreated; }
118 bool IsThreadStopRequested() noexcept { return stopThreadRequested; }
119 bool IsSetupComplete() const noexcept { return setupComplete; }
120 bool IsThreadRunning() const noexcept {
121 return threadRunning.load(std::memory_order_acquire);
122 }
123 bool IsThreadStopped() const noexcept {
124 return !threadRunning.load(std::memory_order_acquire);
125 }
126 bool IsCleanupComplete() const noexcept {return cleanupComplete; }
127 bool IsThreadInStepDelay() const noexcept {return threadStepInDelay; }
128
129
143 bool Start() noexcept;
144
150 virtual bool StartAction() noexcept;
151
161 bool Stop() noexcept;
162
172 virtual bool Setup() noexcept = 0;
173
183 virtual uint32_t Step() noexcept = 0;
184
192 virtual bool Cleanup() noexcept = 0;
193
199 const char* GetThreadName() noexcept;
200
209 bool StartThreadAndWaitToVerify(uint32_t startTimeoutMsec = 1000);
210
219 bool StopThreadAndWaitToVerify(uint32_t stopTimeoutMsec);
220
224 uint32_t GetStackHighWaterMark() const noexcept;
225
229 bool ChangePriority(uint32_t newPriority) noexcept;
230
231
232protected:
233
242 void WaitForStart() noexcept;
243
249 virtual bool Initialize() noexcept = 0;
250
261 virtual bool ResetVariables() noexcept = 0;
262
267 static void ThreadEntry(OS_Ulong instanceAddress);
268
280 bool CreateBaseThread(uint8_t *stack, OS_Ulong stackSizeBytes, OS_Uint priority,
281 OS_Uint preempt_threshold, OS_Ulong timeSliceAllowed, OS_Uint auto_start) noexcept;
282
292 bool CreateBaseThread(uint8_t *stack, OS_Ulong stackSizeBytes, OS_Uint priority,
293 OS_Uint preempt_threshold, OS_Ulong timeSliceAllowed, OS_Uint auto_start,
294 int core_id) noexcept;
295
299 void MarkSetupComplete() noexcept { setupComplete = true; }
300
304 void ClearSetupComplete() noexcept { setupComplete = false; }
305
309 void MarkCleanupComplete() noexcept { cleanupComplete = true; }
310
314 void ClearCleanupComplete() noexcept { cleanupComplete = false; }
315
319 void MarkThreadRunning() noexcept { threadRunning.store(true, std::memory_order_release); }
320
324 void ClearThreadRunning() noexcept { threadRunning.store(false, std::memory_order_release); }
325
330
335
339 void MarkThreadStepInDelay() noexcept { threadStepInDelay = true; }
340
344 void ClearThreadStepInDelay() noexcept { threadStepInDelay = false; }
345
348 const char *osThreadName;
351 static constexpr char baseThreadStartSemaphoreBaseName[] = "BaseThreadStartSem-";
352
353private:
354
357
358 std::atomic<bool> threadRunning;
359 std::atomic<bool> threadStepInDelay;
360 std::atomic<bool> setupComplete;
361 std::atomic<bool> cleanupComplete;
362 std::atomic<bool> stopThreadRequested;
363
364};
365
366#endif /* BaseThread */
Generic RTOS abstraction — public header.
C-facing helper functions for delays, cycle counters, and creation of RTOS handles (queues,...
SignalSemaphore class definition.
Abstract class to assist in designing thread-based execution sequences.
Definition BaseThread.h:32
virtual bool Initialize() noexcept=0
Function to initialize thread. Generally, the initialize function should return false rather than cau...
bool EnsureInitialized() noexcept
Initialize the thread.
Definition BaseThread.h:88
virtual uint32_t Step() noexcept=0
Steps the thread through a single execution cycle .
bool IsThreadInStepDelay() const noexcept
Definition BaseThread.h:127
bool IsSuspended() noexcept
Check the status of the underlying thread OS task.
Definition BaseThread.cpp:155
std::atomic< bool > setupComplete
Definition BaseThread.h:360
bool osThreadCreated
Definition BaseThread.h:349
bool initialized
Definition BaseThread.h:346
BaseThread(const BaseThread &)=delete
The Copy constructor is deleted to avoid copying instances.
virtual bool Setup() noexcept=0
Setup the thread.
virtual bool StartAction() noexcept
Start Action that can be run to see if Start() qualifies to start thread.
Definition BaseThread.cpp:192
static constexpr char baseThreadStartSemaphoreBaseName[]
Definition BaseThread.h:351
const char * osThreadName
Definition BaseThread.h:348
bool CreateBaseThread(uint8_t *stack, OS_Ulong stackSizeBytes, OS_Uint priority, OS_Uint preempt_threshold, OS_Ulong timeSliceAllowed, OS_Uint auto_start) noexcept
Function to Create TX thread.
Definition BaseThread.cpp:81
std::atomic< bool > stopThreadRequested
Definition BaseThread.h:362
void WaitForStart() noexcept
Starts the thread.
Definition BaseThread.cpp:259
virtual bool ResetVariables() noexcept=0
Resets all member variables to their initial states.
bool IsCleanupComplete() const noexcept
Definition BaseThread.h:126
std::atomic< bool > cleanupComplete
Definition BaseThread.h:361
uint32_t waitBeforeStep
Definition BaseThread.h:355
bool IsThreadRunning() const noexcept
Definition BaseThread.h:120
bool ChangePriority(uint32_t newPriority) noexcept
Change the running priority of the thread.
Definition BaseThread.cpp:331
uint32_t GetStackHighWaterMark() const noexcept
Get the minimum remaining stack of the task.
Definition BaseThread.cpp:307
void MarkThreadStepInDelay() noexcept
Indicate a request to stop the thread.
Definition BaseThread.h:339
void ClearThreadRunning() noexcept
Mark the thread as not running.
Definition BaseThread.h:324
bool Resume() noexcept
Resume the underlying thread OS task.
Definition BaseThread.cpp:140
void MarkThreadStopRequested() noexcept
Indicate a request to stop the thread.
Definition BaseThread.h:329
bool StopThreadAndWaitToVerify(uint32_t stopTimeoutMsec)
[BLOCKING] Signals a BaseThread to stop and waits to make sure it's started within the given timeout.
Definition BaseThread.cpp:287
virtual bool Cleanup() noexcept=0
Cleanup code that exists once the thread step sequence is stopped. The cleanup function should genera...
bool IsSetupComplete() const noexcept
Definition BaseThread.h:119
bool IsInitialized() const noexcept
This function checks if the class is initialized;.
Definition BaseThread.h:75
std::atomic< bool > threadRunning
Definition BaseThread.h:358
bool IsThreadStopRequested() noexcept
Definition BaseThread.h:118
OS_Thread osThread
Definition BaseThread.h:347
void MarkThreadRunning() noexcept
Mark the thread as running.
Definition BaseThread.h:319
void MarkSetupComplete() noexcept
Mark the setup as complete.
Definition BaseThread.h:299
static void ThreadEntry(OS_Ulong instanceAddress)
Thread entry that will be running.
Definition BaseThread.cpp:208
OS_Thread * GetThreadId() noexcept
Get the pointer to the thread.
Definition BaseThread.h:65
bool IsThreadCreated() noexcept
Definition BaseThread.h:117
void ClearThreadStepInDelay() noexcept
Clear the stop request for the thread.
Definition BaseThread.h:344
bool IsThreadStopped() const noexcept
Definition BaseThread.h:123
void ClearThreadStopRequested() noexcept
Clear the stop request for the thread.
Definition BaseThread.h:334
bool StartThreadAndWaitToVerify(uint32_t startTimeoutMsec=1000)
[BLOCKING] Signals a BaseThread to start and waits to make sure it's started within the given timeout...
Definition BaseThread.cpp:270
void ClearCleanupComplete() noexcept
Clear the cleanup flag.
Definition BaseThread.h:314
bool Stop() noexcept
Stops the thread.
Definition BaseThread.cpp:198
BaseThread & operator=(const BaseThread &)=delete
The assignment operator constructor is deleted to avoid copying instances.
void ClearSetupComplete() noexcept
Clear the setup completion flag.
Definition BaseThread.h:304
virtual ~BaseThread() noexcept
The destructor de-initializes the singleton. It is not likely to be called in released products,...
Definition BaseThread.cpp:52
bool Suspend() noexcept
Suspend the underlying thread OS task.
Definition BaseThread.cpp:127
std::atomic< bool > threadStepInDelay
Definition BaseThread.h:359
uint32_t minTimestampBeforeNextStep
Definition BaseThread.h:356
SignalSemaphore signalSemaphore
Definition BaseThread.h:350
const char * GetThreadName() noexcept
Returns the Base thread name.
Definition BaseThread.cpp:65
void MarkCleanupComplete() noexcept
Mark the cleanup as complete.
Definition BaseThread.h:309
bool Start() noexcept
Starts the thread.
Definition BaseThread.cpp:176
SignalSemaphore class for managing a named semaphore.
Definition SignalSemaphore.h:21
unsigned int OS_Uint
Definition os_cmsis.h:36
unsigned long OS_Ulong
Definition os_cmsis.h:35
osThreadId_t OS_Thread
Definition os_cmsis.h:27