ifw-ccf 5.0.2
Loading...
Searching...
No Matches
thread.hpp
Go to the documentation of this file.
1
5#ifndef IFW_CCF_MPTK_THREAD_HPP_
6#define IFW_CCF_MPTK_THREAD_HPP_
7
8#include <string>
9#include <thread>
10#include <mutex>
11#include <time.h>
12
13#include <rad/logger.hpp>
14#include <rad/exceptions.hpp>
15
17
18namespace ifw::ccf::mptk {
19
20 const std::string THREAD_REGISTRY = "ThreadRegistry";
21
23 void SleepUntil(struct timespec* ts,
24 int delay);
25
30 class Thread {
31 public:
32 static std::map<std::string, Thread*> s_thread_registry;
33
40
47 Thread(const std::string& thread_name,
48 MessageBus& message_bus,
49 const double period = 0.1);
50 // TODO: Add Thread name in signature.
51
52 virtual ~Thread();
53
55 void SetPriority(const int prio = -1);
56
58 std::string GetName() const;
59
61 void Run();
62
68 virtual void UserLogic();
69
71 void Start();
72
74 void Stop();
75
77 void Pause();
78
80 void Continue();
81
82 // Get current drift (in seconds).
83 double GetDrift() const;
84
92 bool GetExecFlag();
93
96
98 bool Terminated();
99
102
104 std::string ToString() const;
105
106 private:
107 std::string m_thread_name;
108 double m_period;
109 ThreadExecControl m_exec_flag;
110 double m_start_time;
111 uint64_t m_iteration_count;
112 double m_drift;
113 double m_previous_start_time;
114 double m_next_execution;
115 bool m_thread_terminated;
116 bool m_gen_statistics;
117 struct sched_param m_sched_par;
118
119 std::unique_ptr<std::thread> m_sys_thread;
120 MessageBus* m_message_bus;
121 std::recursive_mutex m_thr_mutex;
122 };
123
124}
125
126#endif // IFW_CCF_MPTK_THREAD_HPP_
127
IFW CTD Multiprocessing Toolkit Message Bus.
Definition messageBus.hpp:91
IFW CTD Multiprocessing Toolkit Thread base class.
Definition thread.hpp:30
bool GetExecFlag()
Check the Thread Execution Flag. The Thread Execution Flag shall be called regularly in the thread,...
Definition thread.cpp:253
void Stop()
Stop the thread execution.
Definition thread.cpp:230
double GetDrift() const
Definition thread.cpp:225
MessageBus & MsgBus()
Get acces to the MessageBus associated with this thread object.
Definition thread.cpp:279
std::string ToString() const
Generate ASCII output providing a status of the object.
virtual ~Thread()
Definition thread.cpp:67
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:184
void Continue()
Continue a paused thread execution.
Definition thread.cpp:245
static std::map< std::string, Thread * > s_thread_registry
Definition thread.hpp:32
virtual void UserLogic()
User provided business logic. The UserLogic method need not execute an internal loop....
Definition thread.cpp:198
ThreadExecControl
Thread execution control/status.
Definition thread.hpp:35
@ THREAD_EXEC_RUNNING
Definition thread.hpp:37
@ THREAD_EXEC_PAUSED
Definition thread.hpp:38
@ THREAD_EXEC_STOPPED
Definition thread.hpp:36
std::string GetName() const
Return the thread Name.
Definition thread.cpp:72
void Start()
Start the thread execution.
Definition thread.cpp:209
Thread(const std::string &thread_name, MessageBus &message_bus, const double period=0.1)
Constructor method, setting up the internal members.
Definition thread.cpp:41
bool Terminated()
Returns true if thread no longer running.
Definition thread.cpp:284
void Run()
Method to invoke the user provided business logic of the thread. The method executes internally a loo...
Definition thread.cpp:119
void Pause()
Pause the thread execution.
Definition thread.cpp:238
Definition manager.hpp:15
void SleepUntil(struct timespec *ts, int delay)
Adds "delay" nanoseconds to timespecs and sleeps until that time.
Definition thread.cpp:17
const std::string THREAD_REGISTRY
Definition thread.hpp:20