ifw-ccf 6.0.0
 
Loading...
Searching...
No Matches
manager.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <ifw/core/utils/system/system.hpp>
10
13
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 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
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 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
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 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
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}
void StartThreads()
Execute all threads registered.
Definition manager.cpp:31
void StopThreads()
Request threads to end execution.
Definition manager.cpp:70
MessageBus & MsgBus()
Get reference to Message Bus.
Definition manager.cpp:77
~Manager()
Definition manager.cpp:18
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:82
void PauseThreads()
Pause the threads.
Definition manager.cpp:63
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:13
int16_t NbOfThreadsRunning() const
Returns the number of threads running; this includes the ones that may be paused.
Definition manager.cpp:54
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:38
void Reset()
Reset the object. This includes stopping the possible running threads and deleting all thread objects...
Definition manager.cpp:23
IFW CTD Multiprocessing Toolkit Message Bus.
Definition messageBus.hpp:92
MessageBus & RegisterThread(const std::string &thread_name)
Register thread which will send/receive messages on the Message Bus.
Definition messageBus.cpp:65
Definition logger.hpp:13
log4cplus::Logger & Logger()
Definition logger.cpp:11