ifw-ccf  1.0.0
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 <time.h>
11 
12 #include "rad/logger.hpp"
13 #include "rad/exceptions.hpp"
14 
15 #include "ccf/mptk/messageBus.hpp"
16 
17 namespace ccf::mptk {
18  const std::string THREAD_REGISTRY = "ThreadRegistry";
19 
21  void SleepUntil(struct timespec* ts,
22  int delay);
23 
28  class Thread {
29  public:
30  static std::map<std::string, Thread*> s_thread_registry;
31 
33  typedef enum {
38 
45  Thread(const std::string& thread_id,
46  MessageBus& message_bus,
47  const double period = 0.1);
48  // TODO: Add Thread name in signature.
49 
50  virtual ~Thread();
51 
53  void SetPriority(const int prio = -1);
54 
56  std::string GetId() const;
57 
59  void Run();
60 
66  virtual void UserLogic();
67 
69  void Start();
70 
72  void Stop();
73 
75  void Pause();
76 
78  void Continue();
79 
80  // Get current drift (in seconds).
81  double GetDrift() const;
82 
90  bool GetExecFlag();
91 
94 
96  bool Terminated() const;
97 
99  MessageBus& MsgBus();
100 
102  std::string ToString() const;
103 
104  private:
105  std::string m_thread_id;
106  double m_period;
107  ThreadExecControl m_exec_flag;
108  double m_start_time;
109  uint64_t m_iteration_count;
110  double m_drift;
111  double m_previous_start_time;
112  double m_next_execution;
113  bool m_thread_terminated;
114  bool m_gen_statistics;
115  struct sched_param m_sched_par;
116 
117  std::unique_ptr<std::thread> m_sys_thread;
118  MessageBus* m_message_bus;
119  };
120 
121 }
122 
123 #endif // IFW_CCF_MPTK_THREAD_HPP_
124 
ccf::mptk::Thread::GetExecFlag
bool GetExecFlag()
Check the Thread Execution Flag. The Thread Execution Flag shall be called regularly in the thread,...
Definition: thread.cpp:165
messageBus.hpp
ccf::mptk
Definition: manager.hpp:15
ccf::mptk::Thread::ThreadExecControl
ThreadExecControl
Thread execution control/status.
Definition: thread.hpp:33
ccf::mptk::Thread::ToString
std::string ToString() const
Generate ASCII output providing a status of the object.
ccf::mptk::Thread::THREAD_EXEC_STOPPED
@ THREAD_EXEC_STOPPED
Definition: thread.hpp:34
ccf::mptk::Thread
IFW CTD Multiprocessing Toolkit Thread base class.
Definition: thread.hpp:28
ccf::mptk::Thread::UserLogic
virtual void UserLogic()
User provided business logic. The UserLogic method need not execute an internal loop....
Definition: thread.cpp:120
ccf::mptk::Thread::GetId
std::string GetId() const
Return the thread ID.
Definition: thread.cpp:72
ccf::mptk::Thread::Pause
void Pause()
Pause the thread execution.
Definition: thread.cpp:154
ccf::mptk::Thread::~Thread
virtual ~Thread()
Definition: thread.cpp:67
ccf::mptk::Thread::Continue
void Continue()
Continue a paused thread execution.
Definition: thread.cpp:159
ccf::mptk::Thread::MsgBus
MessageBus & MsgBus()
Get acces to the MessageBus associated with this thread object.
Definition: thread.cpp:182
ccf::mptk::Thread::SetPriority
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:108
ccf::mptk::Thread::Terminated
bool Terminated() const
Returns true if thread no longer running.
Definition: thread.cpp:187
ccf::mptk::MessageBus
IFW CTD Multiprocessing Toolkit Message Bus.
Definition: messageBus.hpp:30
ccf::mptk::Thread::THREAD_EXEC_RUNNING
@ THREAD_EXEC_RUNNING
Definition: thread.hpp:35
ccf::mptk::Thread::Run
void Run()
Method to invoke the user provided business logic of the thread. The method executes internally a loo...
Definition: thread.cpp:77
ccf::mptk::Thread::GetDrift
double GetDrift() const
Definition: thread.cpp:143
ccf::mptk::Thread::s_thread_registry
static std::map< std::string, Thread * > s_thread_registry
Definition: thread.hpp:30
ccf::mptk::Thread::Stop
void Stop()
Stop the thread execution.
Definition: thread.cpp:148
ccf::mptk::Thread::Thread
Thread(const std::string &thread_id, MessageBus &message_bus, const double period=0.1)
Constructor method, setting up the internal members.
Definition: thread.cpp:41
ccf::mptk::SleepUntil
void SleepUntil(struct timespec *ts, int delay)
Adds "delay" nanoseconds to timespecs and sleeps until that time.
Definition: thread.cpp:17
ccf::mptk::Thread::Start
void Start()
Start the thread execution.
Definition: thread.cpp:131
ccf::mptk::THREAD_REGISTRY
const std::string THREAD_REGISTRY
Definition: thread.hpp:18
ccf::mptk::Thread::THREAD_EXEC_PAUSED
@ THREAD_EXEC_PAUSED
Definition: thread.hpp:36