SObjectizer  5.8
Loading...
Searching...
No Matches
mt_env_infrastructure.hpp
Go to the documentation of this file.
1/*
2 * SObjectizer-5
3 */
4
5/*!
6 * \file
7 * \brief Default implementation of multithreaded environment infrastructure.
8 *
9 * \since v.5.5.19
10 */
11
12#pragma once
13
14#include <so_5/environment_infrastructure.hpp>
15#include <so_5/mchain.hpp>
16
17#include <so_5/disp/one_thread/pub.hpp>
18#include <so_5/disp/nef_one_thread/pub.hpp>
19
20#include <so_5/impl/coop_repository_basis.hpp>
21#include <so_5/impl/final_dereg_chain_helpers.hpp>
22
23#include <so_5/stats/impl/std_controller.hpp>
24
25#include <so_5/timers.hpp>
26
27#include <variant>
28
29namespace so_5 {
30
31namespace env_infrastructures {
32
33namespace default_mt {
34
35namespace impl {
36
37//
38// coop_repo_t
39//
40/*!
41 * \brief Implementation of coop_repository for
42 * multithreaded environment infrastructure.
43 *
44 * \since v.5.5.19
45 */
46class coop_repo_t final : protected ::so_5::impl::coop_repository_basis_t
47 {
48 public :
49 //! Initializing constructor.
51 //! SObjectizer Environment.
53 //! Cooperation action listener.
54 coop_listener_unique_ptr_t coop_listener );
55
56 //! Do initialization.
57 void
58 start();
59
60 //! Finish work.
61 /*!
62 * Initiates deregistration of all agents. Waits for complete
63 * deregistration for all of them. Waits for termination of
64 * cooperation deregistration thread.
65 */
66 void
67 finish();
68
69 using coop_repository_basis_t::make_coop;
70
71 using coop_repository_basis_t::register_coop;
72
73 //! Notification about readiness of the cooperation deregistration.
74 void
76 coop_shptr_t coop );
77
78 //! Do final actions of the cooperation deregistration.
79 /*!
80 * \retval true there are some live cooperations.
81 * \retval false there is no more live cooperations.
82 */
83 bool
85 //! Cooperation to be deregistered.
86 coop_shptr_t coop ) noexcept;
87
88 //! Initiate start of the cooperation deregistration.
89 void
91
92 //! Wait for a signal about start of the cooperation deregistration.
93 void
95
96 //! Wait for end of all cooperations deregistration.
97 void
99
100 /*!
101 * \brief Get the current statistic for run-time monitoring.
102 *
103 * \since v.5.5.4
104 */
106 query_stats();
107
108 private :
109 //! Condition variable for the deregistration start indication.
111
112 //! Condition variable for the deregistration finish indication.
114
115 /*!
116 * \name Stuff for final coop deregistration.
117 * \{
118 */
119 /*!
120 * \brief Lock object for thread-safety of the chain of coops
121 * ready for the final deregistration.
122 *
123 * \since v.5.8.0
124 */
126
127 /*!
128 * \brief Notification object to inform that the chain of
129 * coops for the final deregistration isn't empty anymore.
130 *
131 * \attention
132 * It has to be used only when m_final_dereg_chain_lock is acquired.
133 *
134 * \since v.5.8.0
135 */
137
138 /*!
139 * \brief The chain of coops for the final deregistration.
140 *
141 * \since v.5.8.0
142 */
144
145 /*!
146 * \brief The flag for shutting down the final deregistration thread.
147 *
148 * Value `true` means that the final deregistration thread has to
149 * be finished.
150 *
151 * \attention
152 * This value should be set/checked only when m_final_dereg_chain_lock
153 * is acquired.
154 *
155 * \since v.5.8.0
156 */
158
159 /*!
160 * \brief A separate thread for doing the final deregistration.
161 *
162 * \note Actual thread is started inside start() method.
163 *
164 * \since v.5.5.13
165 */
167 /*!
168 * \}
169 */
170
171 /*!
172 * \brief Method that implements the body of final deregistration thread.
173 *
174 * \since v.5.8.0
175 */
176 void
178
179 /*!
180 * \brief Method that performs the final deregistration for
181 * coops in the m_final_dereg_chain.
182 *
183 * \attention
184 * It's expected that m_final_dereg_chain isn't empty.
185 *
186 * \since v.5.8.0
187 */
188 void
190 //! Lock object for acquired m_final_dereg_chain_lock.
191 std::unique_lock< std::mutex > & lck ) noexcept;
192 };
193
194//
195// mt_env_infrastructure_t
196//
197/*!
198 * \brief Default implementation of multithreaded environment infrastructure.
199 *
200 * \since v.5.5.19
201 */
204 {
205 public :
206 //! Type for a holder of actual default dispatcher handle.
207 /*!
208 * \note
209 * This type has to be declared as public type. Otherwise it is
210 * necessary to add several helper classes as friends to
211 * mt_env_infrastructure_t.
212 */
216 >;
217
218 //! Initializing constructor.
220 //! Environment to work in.
221 environment_t & env,
222 //! Parameters for the default dispatcher,
223 environment_params_t::default_disp_params_t default_disp_params,
224 //! Timer thread to be used by environment.
225 timer_thread_unique_ptr_t timer_thread,
226 //! Cooperation action listener.
227 coop_listener_unique_ptr_t coop_listener,
228 //! Run-time stats distribution mbox.
229 mbox_t stats_distribution_mbox );
230
231 void
232 launch( env_init_t init_fn ) override;
233
234 void
235 stop() noexcept override;
236
237 [[nodiscard]]
239 make_coop(
240 coop_handle_t parent,
241 disp_binder_shptr_t default_binder ) override;
242
245 coop_unique_holder_t coop ) override;
246
247 void
249 coop_shptr_t coop ) noexcept override;
250
251 bool
253 coop_shptr_t coop_name ) noexcept override;
254
257 const std::type_index & type_wrapper,
258 const message_ref_t & msg,
259 const mbox_t & mbox,
260 std::chrono::steady_clock::duration pause,
261 std::chrono::steady_clock::duration period ) override;
262
263 void
265 const std::type_index & type_wrapper,
266 const message_ref_t & msg,
267 const mbox_t & mbox,
268 std::chrono::steady_clock::duration pause ) override;
269
271 stats_controller() noexcept override;
272
274 stats_repository() noexcept override;
275
278
280 query_timer_thread_stats() override;
281
283 make_default_disp_binder() override;
284
285 private :
286 //! SOEnv for that this infrastructure was created.
288
289 //! Parameters for the default dispatcher.
290 /*!
291 * \note
292 * There wasn't such attribute in previous versions of SObjectizer-5
293 * because creation and running of the default dispatcher was separated.
294 * In v.5.6.0 the default dispatcher is created inside lauch() and
295 * we have to store parameters for the default dispatcher somewhere.
296 *
297 * \note
298 * Since v.5.8.0 it main contain parameters for one_thread-dispatcher
299 * or nef_one_thread-dispatcher.
300 *
301 * \since v.5.6.0
302 */
304
305 //! Default dispatcher.
306 /*!
307 * \attention
308 * The actual value is created only in
309 * run_default_dispatcher_and_go_further() function. And this
310 * value is dropped after return from that function.
311 * It means that default dispatcher exists only while
312 * lauch() is running.
313 */
315
316 //! Timer thread to be used by the environment.
318
319 //! Repository of registered cooperations.
320 coop_repo_t m_coop_repo;
321
322 //! Run-time stats controller to be used by the environment.
323 ::so_5::stats::impl::std_controller_t m_stats_controller;
324
325 void
326 run_default_dispatcher_and_go_further( env_init_t init_fn );
327
328 void
329 run_timer_thread_and_go_further( env_init_t init_fn );
330
331 void
332 run_agent_core_and_go_further( env_init_t init_fn );
333
334 void
335 run_user_supplied_init_and_wait_for_stop( env_init_t init_fn );
336 };
337
338} /* namespace impl */
339
340} /* namespace default_mt */
341
342} /* namespace env_infrastructures */
343
344} /* namespace so_5 */
Type of smart handle for a cooperation.
A special type that plays role of unique_ptr for coop.
Definition coop.hpp:1342
void start_deregistration()
Initiate start of the cooperation deregistration.
void ready_to_deregister_notify(coop_shptr_t coop)
Notification about readiness of the cooperation deregistration.
std::condition_variable m_final_dereg_chain_cond
Notification object to inform that the chain of coops for the final deregistration isn't empty anymor...
bool final_deregister_coop(coop_shptr_t coop) noexcept
Do final actions of the cooperation deregistration.
void process_current_final_dereg_chain(std::unique_lock< std::mutex > &lck) noexcept
Method that performs the final deregistration for coops in the m_final_dereg_chain.
so_5::impl::final_dereg_chain_holder_t m_final_dereg_chain
The chain of coops for the final deregistration.
std::mutex m_final_dereg_chain_lock
Lock object for thread-safety of the chain of coops ready for the final deregistration.
coop_repo_t(outliving_reference_t< environment_t > env, coop_listener_unique_ptr_t coop_listener)
Initializing constructor.
void wait_all_coop_to_deregister()
Wait for end of all cooperations deregistration.
void final_dereg_thread_body()
Method that implements the body of final deregistration thread.
std::condition_variable m_deregistration_started_cond
Condition variable for the deregistration start indication.
bool m_final_dereg_thread_shutdown_flag
The flag for shutting down the final deregistration thread.
std::condition_variable m_deregistration_finished_cond
Condition variable for the deregistration finish indication.
environment_infrastructure_t::coop_repository_stats_t query_stats()
Get the current statistic for run-time monitoring.
std::thread m_final_dereg_thread
A separate thread for doing the final deregistration.
void wait_for_start_deregistration()
Wait for a signal about start of the cooperation deregistration.
Default implementation of multithreaded environment infrastructure.
so_5::timer_id_t schedule_timer(const std::type_index &type_wrapper, const message_ref_t &msg, const mbox_t &mbox, std::chrono::steady_clock::duration pause, std::chrono::steady_clock::duration period) override
Initiate a timer (delayed or periodic message).
::so_5::stats::repository_t & stats_repository() noexcept override
Get stats repository for the environment.
const environment_params_t::default_disp_params_t m_default_dispatcher_params
Parameters for the default dispatcher.
timer_thread_stats_t query_timer_thread_stats() override
Query run-time statistics for timer (thread or manager).
void single_timer(const std::type_index &type_wrapper, const message_ref_t &msg, const mbox_t &mbox, std::chrono::steady_clock::duration pause) override
Initiate a delayed message.
void stop() noexcept override
Initiate a signal for shutdown of Environment.
coop_handle_t register_coop(coop_unique_holder_t coop) override
Register new cooperation.
::so_5::stats::controller_t & stats_controller() noexcept override
Get stats controller for the environment.
timer_thread_unique_ptr_t m_timer_thread
Timer thread to be used by the environment.
bool final_deregister_coop(coop_shptr_t coop_name) noexcept override
Do final actions of the cooperation deregistration.
void launch(env_init_t init_fn) override
Do actual launch of SObjectizer's Environment.
mt_env_infrastructure_t(environment_t &env, environment_params_t::default_disp_params_t default_disp_params, timer_thread_unique_ptr_t timer_thread, coop_listener_unique_ptr_t coop_listener, mbox_t stats_distribution_mbox)
Initializing constructor.
coop_unique_holder_t make_coop(coop_handle_t parent, disp_binder_shptr_t default_binder) override
Create an instance of a new coop.
coop_repository_stats_t query_coop_repository_stats() override
Query run-time statistics for cooperation repository.
::so_5::stats::impl::std_controller_t m_stats_controller
Run-time stats controller to be used by the environment.
environment_t & m_env
SOEnv for that this infrastructure was created.
disp_binder_shptr_t make_default_disp_binder() override
Create a binder for the default dispatcher.
An interface for environment_infrastructure entity.
SObjectizer Environment.
A basic part for various implementations of coop_repository.
void deregister_all_coop() noexcept
Deregisted all cooperations.
environment_infrastructure_t::coop_repository_stats_t query_stats()
Get the current statistic for run-time monitoring.
try_switch_to_shutdown_result_t try_switch_to_shutdown() noexcept
Try to switch repository to shutdown state.
Helper class for holding the current chain of coops for the final deregistration.
Helper class for indication of long-lived reference via its type.
Definition outliving.hpp:98
A public interface for control SObjectizer monitoring options.
An interface of data sources repository.
An indentificator for the timer.
Definition timers.hpp:82
#define SO_5_FUNC
Definition declspec.hpp:48
Default multi-threaded environment infrastructure.
SO_5_FUNC environment_infrastructure_factory_t factory()
A factory for creation the default multitheading environment infrastructure.
Various implementations of environment_infrastructure.
Details of SObjectizer run-time implementations.
Definition agent.cpp:905
Internal implementation of run-time monitoring and statistics related stuff.
All stuff related to run-time monitoring and statistics.
Private part of message limit implementation.
Definition agent.cpp:33
Statistical data for run-time monitoring of coop repository content.
Statistics for run-time monitoring.
Definition timers.hpp:136