ifw-ccf 5.0.2
Loading...
Searching...
No Matches
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 <ifw/core/utils/system/system.hpp>
9
12
13// TODO: Make own logger for this library. Remove all RAD_LOG_TRACE etc.
14
15namespace ifw::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 = ifw::core::utils::base::NO_TIMEOUT);
43
45 int16_t NbOfThreadsRunning() const;
46
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 ifw::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 StartThreads()
Execute all threads registered.
Definition manager.cpp:30
void StopThreads()
Request threads to end execution.
Definition manager.cpp:69
MessageBus & MsgBus()
Get reference to Message Bus.
Definition manager.cpp:76
~Manager()
Definition manager.cpp:17
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
void GetThreadNames(std::list< std::string > &thread_names) const
Return list with names of threads registered.
Definition manager.cpp:81
void PauseThreads()
Pause the threads.
Definition manager.cpp:62
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
Manager()
Definition manager.cpp:12
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
std::string ToString() const
Generate ASCII output providing a status of the object.
int16_t WaitForThreadTerm(const double time_out=ifw::core::utils::base::NO_TIMEOUT)
Wait for threads to terminate execution. Returns the number of threads running.
Definition manager.cpp:37
void Reset()
Reset the object. This includes stopping the possible running threads and deleting all thread objects...
Definition manager.cpp:22
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