ifw-ccf  3.0.0-pre2
manager.hpp
Go to the documentation of this file.
1 
5 #ifndef CCF_MPTK_MANAGER_HPP_
6 #define CCF_MPTK_MANAGER_HPP_
7 
8 #include <core/utils/system/system.hpp>
9 
10 #include <ccf/mptk/message.hpp>
11 #include <ccf/mptk/thread.hpp>
12 
13 // TODO: Make own logger for this library. Remove all RAD_LOG_TRACE etc.
14 
15 namespace ccf::mptk {
16 
21  class Manager {
22  public:
23 
24  Manager();
25 
26  ~Manager();
27 
29  void Reset();
30 
32  void StartThreads();
33 
35  void PauseThreads();
36 
38  void StopThreads();
39 
42  int16_t WaitForThreadTerm(const double time_out = core::utils::base::NO_TIMEOUT);
43 
45  int16_t NbOfThreadsRunning() const;
46 
48  MessageBus& MsgBus();
49 
51  std::string ToString() const;
52 
55  template <class THREAD_TYPE>
56  void CreateThread(const std::string& thread_name,
57  const double period = 0.1,
58  THREAD_TYPE** thread_obj = nullptr) {
59  RAD_LOG_TRACE();
60 
61  BEGIN_CRIT_SEC(m_mgr_id) {
62  m_threads[thread_name] = std::make_shared<THREAD_TYPE>(thread_name, MsgBus(), period);
63  if (m_threads[thread_name].get() == nullptr) {
64  throw rad::Exception(fmt::format("Could not create thread with ID: %s in MPTK instance",
65  thread_name.c_str()));
66  }
67  MsgBus().RegisterThread(thread_name);
68  } END_CRIT_SEC();
69  if (thread_obj != nullptr) {
70  *thread_obj = static_cast<THREAD_TYPE*>(m_threads[thread_name].get());
71  }
72  }
73 
76  template <class THREAD_TYPE>
77  void CreateThread(const THREAD_TYPE& factory_object,
78  const std::string& thread_name,
79  const double period = 0.1,
80  THREAD_TYPE** thread_obj = nullptr) {
81  RAD_LOG_TRACE();
82 
83  BEGIN_CRIT_SEC(m_mgr_id) {
84  m_threads[thread_name] = std::make_shared<THREAD_TYPE>(thread_name, MsgBus(), period);
85  if (m_threads[thread_name].get() == nullptr) {
86  throw rad::Exception(fmt::format("Could not create thread with ID: %s in MPTK instance",
87  thread_name.c_str()));
88  }
89  MsgBus().RegisterThread(thread_name);
90  } END_CRIT_SEC();
91  if (thread_obj != nullptr) {
92  *thread_obj = static_cast<THREAD_TYPE*>(m_threads[thread_name].get());
93  }
94  }
95 
97  template <class THREAD_TYPE>
98  void GetThread(const std::string& thread_name,
99  THREAD_TYPE** thread_obj) {
100  RAD_LOG_TRACE();
101 
102  core::utils::system::Mutex tmp_mutex(m_mgr_id); {
103  auto thr_obj_it = m_threads.find(thread_name);
104  if (thr_obj_it == m_threads.end()) {
105  throw rad::Exception(fmt::format("%s: Undefined Thread Name: %s",
106  m_mgr_id.c_str(), thread_name.c_str()));
107  }
108  *thread_obj = static_cast<THREAD_TYPE*>(thr_obj_it->second.get());
109  }
110 
111  }
112 
114  void GetThreadNames(std::list<std::string>& thread_names) const;
115 
116  private:
117  std::string m_mgr_id;
118  MessageBus m_message_bus;
119  std::map<std::string, std::shared_ptr<Thread>> m_threads;
120  };
121 
122 }
123 
124 #endif // CCF_MPTK_MANAGER_HPP_
IFW CTD Multiprocessing Toolkit Manager class.
Definition: manager.hpp:21
void CreateThread(const THREAD_TYPE &factory_object, const std::string &thread_name, const double period=0.1, THREAD_TYPE **thread_obj=nullptr)
Allocate new MPTK thread object of the given type. The allocated thread object is handled internally ...
Definition: manager.hpp:77
void GetThreadNames(std::list< std::string > &thread_names) const
Return list with names of threads registered.
Definition: manager.cpp:81
MessageBus & MsgBus()
Get reference to Message Bus.
Definition: manager.cpp:76
void PauseThreads()
Pause the threads.
Definition: manager.cpp:62
void GetThread(const std::string &thread_name, THREAD_TYPE **thread_obj)
Get reference to MPTK thread, registered in the Thread Registry.
Definition: manager.hpp:98
int16_t NbOfThreadsRunning() const
Returns the number of threads running; this includes the ones that may be paused.
Definition: manager.cpp:53
void CreateThread(const std::string &thread_name, const double period=0.1, THREAD_TYPE **thread_obj=nullptr)
Allocate new MPTK thread object of the given type. The allocated thread object is handled internally ...
Definition: manager.hpp:56
~Manager()
Definition: manager.cpp:17
void StopThreads()
Request threads to end execution.
Definition: manager.cpp:69
void Reset()
Reset the object. This includes stopping the possible running threads and deleting all thread objects...
Definition: manager.cpp:22
int16_t WaitForThreadTerm(const double time_out=core::utils::base::NO_TIMEOUT)
Wait for threads to terminate execution. Returns the number of threads running.
Definition: manager.cpp:37
std::string ToString() const
Generate ASCII output providing a status of the object.
Manager()
Definition: manager.cpp:12
void StartThreads()
Execute all threads registered.
Definition: manager.cpp:30
IFW CTD Multiprocessing Toolkit Message Bus.
Definition: messageBus.hpp:91
MessageBus & RegisterThread(const std::string &thread_name)
Register thread which will send/receive messages on the Message Bus.
Definition: messageBus.cpp:64
Definition: manager.hpp:15