ifw-ccf 6.0.0
 
Loading...
Searching...
No Matches
thread.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <string>
10#include <thread>
11#include <mutex>
12#include <time.h>
13
14#include <rad/logger.hpp>
15#include <rad/exceptions.hpp>
16
18
19namespace ifw::ccf::mptk {
20
21 const std::string THREAD_REGISTRY = "ThreadRegistry";
22
24 void SleepUntil(struct timespec* ts,
25 int delay);
26
31 class Thread {
32 public:
33 static std::map<std::string, Thread*> s_thread_registry;
34
41
48 Thread(const std::string& thread_name,
49 MessageBus& message_bus,
50 const double period = 0.1);
51 // TODO: Add Thread name in signature.
52
53 virtual ~Thread();
54
56 void SetPriority(const int prio = -1);
57
59 std::string GetName() const;
60
62 void Run();
63
69 virtual void UserLogic();
70
72 void Start();
73
75 void Stop();
76
78 void Pause();
79
81 void Continue();
82
83 // Get current drift (in seconds).
84 double GetDrift() const;
85
93 bool GetExecFlag();
94
97
99 bool Terminated();
100
103
105 std::string ToString() const;
106
107 private:
108 std::string m_thread_name;
109 double m_period;
110 ThreadExecControl m_exec_flag;
111 double m_start_time;
112 uint64_t m_iteration_count;
113 double m_drift;
114 double m_previous_start_time;
115 double m_next_execution;
116 bool m_thread_terminated;
117 bool m_gen_statistics;
118 struct sched_param m_sched_par;
119
120 std::unique_ptr<std::thread> m_sys_thread;
121 MessageBus* m_message_bus;
122 std::recursive_mutex m_thr_mutex;
123 };
124
125}
IFW CTD Multiprocessing Toolkit Message Bus.
Definition messageBus.hpp:92
bool GetExecFlag()
Check the Thread Execution Flag. The Thread Execution Flag shall be called regularly in the thread,...
Definition thread.cpp:210
void Stop()
Stop the thread execution.
Definition thread.cpp:187
double GetDrift() const
Definition thread.cpp:182
MessageBus & MsgBus()
Get acces to the MessageBus associated with this thread object.
Definition thread.cpp:236
std::string ToString() const
Generate ASCII output providing a status of the object.
virtual ~Thread()
Definition thread.cpp:68
void SetPriority(const int prio=-1)
If invoked, it will run the thread in real-time mode with the given priority [0; 99].
Definition thread.cpp:141
void Continue()
Continue a paused thread execution.
Definition thread.cpp:202
static std::map< std::string, Thread * > s_thread_registry
Definition thread.hpp:33
virtual void UserLogic()
User provided business logic. The UserLogic method need not execute an internal loop....
Definition thread.cpp:155
ThreadExecControl
Thread execution control/status.
Definition thread.hpp:36
@ THREAD_EXEC_RUNNING
Definition thread.hpp:38
@ THREAD_EXEC_PAUSED
Definition thread.hpp:39
@ THREAD_EXEC_STOPPED
Definition thread.hpp:37
std::string GetName() const
Return the thread Name.
Definition thread.cpp:73
void Start()
Start the thread execution.
Definition thread.cpp:166
Thread(const std::string &thread_name, MessageBus &message_bus, const double period=0.1)
Constructor method, setting up the internal members.
Definition thread.cpp:42
bool Terminated()
Returns true if thread no longer running.
Definition thread.cpp:241
void Run()
Method to invoke the user provided business logic of the thread. The method executes internally a loo...
Definition thread.cpp:78
void Pause()
Pause the thread execution.
Definition thread.cpp:195
Definition logger.hpp:13
void SleepUntil(struct timespec *ts, int delay)
Adds "delay" nanoseconds to timespecs and sleeps until that time.
Definition thread.cpp:18
const std::string THREAD_REGISTRY
Definition thread.hpp:21