ifw-ccf  2.0.0
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_id,
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_id] = std::make_shared<THREAD_TYPE>(thread_id, MsgBus(), period);
63  if (m_threads[thread_id].get() == nullptr) {
64  throw rad::Exception(fmt::format("Could not create thread with ID: %s in MPTK instance", thread_id.c_str()));
65  }
66  MsgBus().RegisterThread(thread_id);
67  } END_CRIT_SEC();
68  if (thread_obj != nullptr) {
69  *thread_obj = static_cast<THREAD_TYPE*>(m_threads[thread_id].get());
70  }
71  }
72 
75  template <class THREAD_TYPE>
76  void CreateThread(const THREAD_TYPE& factory_object,
77  const std::string& thread_id,
78  const double period = 0.1,
79  THREAD_TYPE** thread_obj = nullptr) {
80  RAD_LOG_TRACE();
81 
82  BEGIN_CRIT_SEC(m_mgr_id) {
83  m_threads[thread_id] = std::make_shared<THREAD_TYPE>(thread_id, MsgBus(), period);
84  if (m_threads[thread_id].get() == nullptr) {
85  throw rad::Exception(fmt::format("Could not create thread with ID: %s in MPTK instance", thread_id.c_str()));
86  }
87  MsgBus().RegisterThread(thread_id);
88  } END_CRIT_SEC();
89  if (thread_obj != nullptr) {
90  *thread_obj = static_cast<THREAD_TYPE*>(m_threads[thread_id].get());
91  }
92  }
93 
95  template <class THREAD_TYPE>
96  void GetThread(const std::string& thread_id,
97  THREAD_TYPE** thread_obj) {
98  RAD_LOG_TRACE();
99 
100  core::utils::system::Mutex tmp_mutex(m_mgr_id); {
101  //BEGIN_CRIT_SEC(m_mgr_id)
102  auto thr_obj_it = m_threads.find(thread_id);
103  if (thr_obj_it == m_threads.end()) {
104  throw rad::Exception(fmt::format("%s: Undefined Thread ID: %s",
105  m_mgr_id.c_str(), thread_id.c_str()));
106  }
107  *thread_obj = static_cast<THREAD_TYPE*>(thr_obj_it->second.get());
108  } // END_CRIT_SEC();
109 
110  }
111 
113  void GetThreadIds(std::list<std::string>& thread_ids) const;
114 
115  private:
116  std::string m_mgr_id;
117  MessageBus m_message_bus;
118  std::map<std::string, std::shared_ptr<Thread>> m_threads;
119  };
120 
121 }
122 
123 #endif // CCF_MPTK_MANAGER_HPP_
ccf::mptk::MessageBus::RegisterThread
MessageBus & RegisterThread(const std::string &thread_id)
Register thread which will send/receive messages on the Message Bus.
Definition: messageBus.cpp:64
ccf::mptk::Manager::~Manager
~Manager()
Definition: manager.cpp:17
ccf::mptk
Definition: manager.hpp:15
ccf::mptk::Manager::Reset
void Reset()
Reset the object. This includes stopping the possible running threads and deleting all thread objects...
Definition: manager.cpp:22
message.hpp
ccf::mptk::Manager::MsgBus
MessageBus & MsgBus()
Get reference to Message Bus.
Definition: manager.cpp:75
ccf::mptk::Manager::GetThread
void GetThread(const std::string &thread_id, THREAD_TYPE **thread_obj)
Get reference to MPTK thread, registered in the Thread Registry.
Definition: manager.hpp:96
ccf::mptk::Manager
IFW CTD Multiprocessing Toolkit Manager class.
Definition: manager.hpp:21
ccf::mptk::Manager::PauseThreads
void PauseThreads()
Pause the threads.
Definition: manager.cpp:61
ccf::mptk::Manager::GetThreadIds
void GetThreadIds(std::list< std::string > &thread_ids) const
Return list with IDs of threads registered.
Definition: manager.cpp:80
thread.hpp
ccf::mptk::MessageBus
IFW CTD Multiprocessing Toolkit Message Bus.
Definition: messageBus.hpp:30
ccf::mptk::Manager::CreateThread
void CreateThread(const std::string &thread_id, 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
ccf::mptk::Manager::NbOfThreadsRunning
int16_t NbOfThreadsRunning() const
Returns the number of threads running; this includes the ones that may be paused.
Definition: manager.cpp:52
ccf::mptk::Manager::ToString
std::string ToString() const
Generate ASCII output providing a status of the object.
ccf::mptk::Manager::StopThreads
void StopThreads()
Request threads to end execution.
Definition: manager.cpp:68
ccf::mptk::Manager::Manager
Manager()
Definition: manager.cpp:12
ccf::mptk::Manager::WaitForThreadTerm
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
ccf::mptk::Manager::StartThreads
void StartThreads()
Execute all threads registered.
Definition: manager.cpp:30
ccf::mptk::Manager::CreateThread
void CreateThread(const THREAD_TYPE &factory_object, const std::string &thread_id, 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:76