SObjectizer  5.8
Loading...
Searching...
No Matches
mt_env_infrastructure.cpp
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#include <so_5/impl/mt_env_infrastructure.hpp>
13
14#include <so_5/impl/run_stage.hpp>
15#include <so_5/impl/internal_env_iface.hpp>
16
17#include <so_5/environment.hpp>
18#include <so_5/send_functions.hpp>
19
20#include <so_5/disp/one_thread/pub.hpp>
21
22namespace so_5 {
23
24namespace env_infrastructures {
25
26namespace default_mt {
27
28namespace impl {
29
30//
31// coop_repo_t
32//
33coop_repo_t::coop_repo_t(
35 coop_listener_unique_ptr_t coop_listener )
38 {}
39
40void
41coop_repo_t::start()
42{
43 // A separate thread for doing the final dereg must be started.
44 m_final_dereg_thread = std::thread{ [this] { final_dereg_thread_body(); } };
45}
46
47void
48coop_repo_t::finish()
49{
50 // Deregistration of all cooperations should be initiated.
52
53 // Deregistration of all cooperations should be finished.
55
56 // Notify a dedicated thread and wait while it will be stopped.
57 {
58 std::lock_guard< std::mutex > lock{ m_final_dereg_chain_lock };
60 m_final_dereg_chain_cond.notify_one();
61 }
62 m_final_dereg_thread.join();
63}
64
65void
67 coop_shptr_t coop )
68{
69 std::lock_guard< std::mutex > lck{ m_final_dereg_chain_lock };
70
71 // Update the final_dereg_chain.
72 m_final_dereg_chain.append( std::move(coop) );
73
74 if( 1u == m_final_dereg_chain.size() )
75 {
76 // Final deregistration thread may wait, have to wake it up.
77 m_final_dereg_chain_cond.notify_one();
78 }
79}
80
81bool
83 coop_shptr_t coop ) noexcept
84{
85 const auto result =
86 coop_repository_basis_t::final_deregister_coop( std::move(coop) );
87
88 if( result.m_total_deregistration_completed )
89 m_deregistration_finished_cond.notify_one();
90
91 return result.m_has_live_coop;
92}
93
94void
96{
98
99 if( coop_repository_basis_t::try_switch_to_shutdown_result_t
100 ::switched == result )
101 m_deregistration_started_cond.notify_one();
102}
103
104void
106{
107 std::unique_lock< std::mutex > lck{ m_lock };
108
109 m_deregistration_started_cond.wait( lck,
110 [this] { return status_t::normal != m_status; } );
111}
112
113void
115{
116 std::unique_lock< std::mutex > lck{ m_lock };
117
118 // Must wait for a signal is there are cooperations in
119 // the deregistration process.
120 m_deregistration_finished_cond.wait( lck,
121 [this] { return 0u == m_total_coops; } );
122}
123
125coop_repo_t::query_stats()
126{
127 const auto final_dereg_coops = [this]() {
128 std::lock_guard< std::mutex > lck{ m_final_dereg_chain_lock };
129 return m_final_dereg_chain.size();
130 }();
131
132 const auto basis_stats = coop_repository_basis_t::query_stats();
133
134 return {
135 basis_stats.m_total_coop_count,
136 basis_stats.m_total_agent_count,
137 final_dereg_coops
138 };
139}
140
141void
143{
144 std::unique_lock< std::mutex > lck{ m_final_dereg_chain_lock };
145
146 for( bool should_continue = true; should_continue; )
147 {
148 bool should_wait = true;
149
150 // If there are some waiting coops they have to be processed even
151 // if m_final_dereg_thread_shutdown_flag is set.
152 if( !m_final_dereg_chain.empty() )
153 {
154 // There are some coops to be deregistered.
155 process_current_final_dereg_chain( lck );
156
157 // Because the processing takes some time there is no need
158 // to sleep before new check for m_final_dereg_chain.
159 should_wait = false;
160 }
161
163 {
164 // It's time to finish the work.
165 should_continue = false;
166 }
167 else if( should_wait )
168 {
169 // No coops to deregister. Have to wait.
170 m_final_dereg_chain_cond.wait( lck );
171 }
172 }
173}
174
175void
176coop_repo_t::process_current_final_dereg_chain(
177 std::unique_lock< std::mutex > & lck ) noexcept
178{
179 //
180 // NOTE: don't expect exceptions here.
181 //
182
183 // There are some coops to be deregistered.
184 // Have to extract the current value of final dereg chain from
185 // the coop_repo instance.
187
188 // All following actions has to be performed on unlocked mutex.
189 lck.unlock();
190
191 // Do final_deregister_coop for every item in the chain
192 // one by one.
194
195 // Have to reacquire the lock back.
196 lck.lock();
197}
198
199//
200// mt_env_infrastructure_t
201//
215
216void
221
222void
224 {
225 // Sends shutdown signal for all agents.
227 }
228
229[[nodiscard]]
237
244
245void
251
252bool
258
274
275void
289
295
301
307
313
314namespace {
315
316//! Helper class to be used with std::visit.
318 {
319 [[nodiscard]]
322 {
323 return handle.binder();
324 }
325
326 [[nodiscard]]
329 {
330 return handle.binder();
331 }
332 };
333
334} /* namespace anonymous */
335
341
342namespace {
343
344//! Helper class to be used with std::visit.
346 {
348
350 {}
351
352 [[nodiscard]]
355 {
356 return {
358 m_env,
359 std::string{ "DEFAULT" },
360 params )
361 };
362 }
363
364 [[nodiscard]]
367 {
368 return {
370 m_env,
371 std::string{ "DEFAULT" },
372 params )
373 };
374 }
375 };
376
377//! Helper class to be used with std::visit.
379 {
380 void
382 {
383 handle.reset();
384 }
385
386 void
391 };
392
393} /* namespace anonymous */
394
395void
398 {
399 ::so_5::impl::run_stage(
400 "run_default_dispatcher",
401 [this] {
402 // Default dispatcher should be created.
406 },
407 [this] {
408 // Default dispatcher is no more needed.
410 },
411 [this, init_fn] {
413 } );
414 }
415
416void
419 {
420 ::so_5::impl::run_stage(
421 "run_timer",
422 [this] { m_timer_thread->start(); },
423 [this] { m_timer_thread->finish(); },
424 [this, init_fn] {
426 } );
427 }
428
429void
432 {
433 ::so_5::impl::run_stage(
434 "run_agent_core",
435 [this] { m_coop_repo.start(); },
436 [this] { m_coop_repo.finish(); },
437 [this, init_fn] {
439 } );
440 }
441
442void
458
459} /* namespace impl */
460
461//
462// factory
463//
466 {
467 return [](
468 environment_t & env,
469 environment_params_t & params,
470 mbox_t stats_distribution_mbox )
471 {
472 // Timer thread is necessary for that environment.
473 auto timer =
474 so_5::internal_timer_helpers::create_appropriate_timer_thread(
475 params.so5_error_logger(),
476 params.so5_giveout_timer_thread_factory() );
477
478 // Now the environment object can be created.
479 auto obj = new impl::mt_env_infrastructure_t(
480 env,
481 params.default_disp_params(),
482 std::move(timer),
483 params.so5_giveout_coop_listener(),
484 std::move(stats_distribution_mbox) );
485
486 return environment_infrastructure_unique_ptr_t(
487 obj,
488 environment_infrastructure_t::default_deleter() );
489 };
490 }
491
492} /* namespace default_mt */
493
494} /* namespace env_infrastructures */
495
496} /* namespace so_5 */
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.
bool final_deregister_coop(coop_shptr_t coop) noexcept
Do final actions of the cooperation 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.
bool m_final_dereg_thread_shutdown_flag
The flag for shutting down the final deregistration thread.
environment_infrastructure_t::coop_repository_stats_t query_stats()
Get the current statistic for run-time monitoring.
void wait_for_start_deregistration()
Wait for a signal about start of the cooperation deregistration.
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 indication of long-lived reference via its type.
Definition outliving.hpp:98
#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.
Private part of message limit implementation.
Definition agent.cpp:33
Statistical data for run-time monitoring of coop repository content.