SObjectizer  5.8
Loading...
Searching...
No Matches
st_env_stuff.hpp
Go to the documentation of this file.
1/*
2 * SObjectizer-5
3 */
4
5/*!
6 * \file
7 * \brief Various stuff dedicated for single-threaded environments.
8 *
9 * \since
10 * v.5.5.19
11 */
12
13#pragma once
14
15#include <so_5/mbox.hpp>
16
17#include <so_5/ret_code.hpp>
18#include <so_5/outliving.hpp>
19
20namespace so_5 {
21
22namespace stats {
23
24namespace impl {
25
26namespace st_env_stuff {
27
28//
29// next_turn_handler_t
30//
31/*!
32 * \brief An interface for initiation of next turn in stats distribution.
33 *
34 * \since
35 * v.5.5.19
36 */
38 {
39 public :
41 virtual ~next_turn_handler_t() {}
42
43 virtual void
45 //! ID of stats distribution.
46 int run_id ) = 0;
47
48 struct next_turn : public message_t
49 {
50 //! Who must do next turn.
52 //! ID of stats distribution.
54
57 int run_id )
59 , m_run_id( run_id )
60 {}
61 };
62 };
63
64//
65// next_turn_mbox_t
66//
67/*!
68 * \since A special implementation of abstract_message_box for handling
69 * stats distribution in single-threaded environments.
70 *
71 * A call to next_turn_handler_t::on_next_turn is performed directly
72 * in do_deliver_message() method. This is done in assumption that
73 * do_deliver_message() is called on the context on the main environment's
74 * thread.
75 *
76 * \since
77 * v.5.5.19
78 */
79class next_turn_mbox_t final : public abstract_message_box_t
80 {
81 //! Environment for which that mbox is created.
82 /*!
83 * \note
84 * This attribute is necessary for correct implementation of
85 * inherited environment() method.
86 *
87 * \since
88 * v.5.6.0
89 */
91
93
94 public:
95 // NOTE: this method should never be used.
96 mbox_id_t
97 id() const override
98 {
99 return 0;
100 }
101
102 void
104 const std::type_index & /*type_index*/,
105 abstract_message_sink_t & /*subscriber*/ ) override
106 {
107 SO_5_THROW_EXCEPTION( rc_not_implemented,
108 "call to subscribe_event_handler() is illegal for "
109 "next_turn_mbox_t" );
110 }
111
112 void
114 const std::type_index & /*type_index*/,
115 abstract_message_sink_t & /*subscriber*/ ) noexcept override
116 {
117 // Do nothing because it's a noexcept method since v.5.8.0.
118 }
119
120 std::string
121 query_name() const override
122 {
123 return "<next_turn_mbox>";
124 }
125
127 type() const override
128 {
130 }
131
132 void
134 message_delivery_mode_t /*delivery_mode*/,
135 const std::type_index & msg_type,
136 const message_ref_t & message,
137 unsigned int /*redirection_deep*/ ) override
138 {
139 static const auto & next_turn_msg_type =
141
142 if( msg_type != next_turn_msg_type )
143 SO_5_THROW_EXCEPTION( rc_unexpected_error,
144 "only next_turn_handler_t::next_turn expected in "
145 "next_turn_mbox_t::do_deliver_message" );
146
147 const auto & actual_message =
148 dynamic_cast< const next_turn_handler_t::next_turn & >(
149 *message.get() );
150
151 actual_message.m_handler.get().on_next_turn(
152 actual_message.m_run_id );
153 }
154
155 void
157 const std::type_index & /*msg_type*/,
158 const delivery_filter_t & /*filter*/,
159 abstract_message_sink_t & /*subscriber*/ ) override
160 {
161 SO_5_THROW_EXCEPTION( rc_not_implemented,
162 "call to set_delivery_filter() is illegal for "
163 "next_turn_mbox_t" );
164 }
165
166 void
168 const std::type_index & /*msg_type*/,
169 abstract_message_sink_t & /*subscriber*/ ) noexcept override
170 {
171 SO_5_THROW_EXCEPTION( rc_not_implemented,
172 "call to drop_delivery_filter() is illegal for "
173 "next_turn_mbox_t" );
174 }
175
176 /*!
177 * \note
178 * It seems that this method should never be called.
179 * But it is safer to provide an actual implementation for it
180 * than relying on wrong assumption.
181 */
183 environment() const noexcept override
184 {
185 return m_env;
186 }
187
188 //! Helper for simplify creation of that mboxes of that type.
189 static mbox_t
191 {
192 return { new next_turn_mbox_t{env} };
193 }
194 };
195
196} /* namespace st_env_stuff */
197
198} /* namespace impl */
199
200} /* namespace stats */
201
202} /* namespace so_5 */
Interface for message sink.
A base class for agents.
Definition agent.hpp:673
Type of smart handle for a cooperation.
A special type that plays role of unique_ptr for coop.
Definition coop.hpp:1342
An interface of delivery filter object.
Definition mbox.hpp:62
Interface for dispatcher binders.
Default implementation of multithreaded environment infrastructure.
std::shared_ptr< default_dispatcher_t< Activity_Tracker > > m_default_disp
Dispatcher to be used as default dispatcher.
stats::repository_t & stats_repository() noexcept override
Get stats repository for the environment.
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.
reusable::actual_elapsed_timers_collector_t m_timers_collector
A collector for elapsed timers.
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).
void launch(env_init_t init_fn) override
Do actual launch of SObjectizer's Environment.
coop_handle_t register_coop(coop_unique_holder_t coop) override
Register new cooperation.
void stop() noexcept override
Initiate a signal for shutdown of Environment.
so_5::impl::final_dereg_chain_holder_t m_final_dereg_chain
The chain of coops for the final deregistration.
void perform_shutdown_related_actions_if_needed(std::unique_lock< std::mutex > &acquired_lock) noexcept
main_thread_sync_objects_t m_sync_objects
All sync objects to be shared between different parts.
void try_handle_next_demand(std::unique_lock< std::mutex > &acquired_lock) noexcept
timer_thread_stats_t query_timer_thread_stats() override
Query run-time statistics for timer (thread or manager).
void process_final_deregs_if_any(std::unique_lock< std::mutex > &acquired_lock) noexcept
so_5::environment_infrastructure_t::coop_repository_stats_t query_coop_repository_stats() override
Query run-time statistics for cooperation repository.
coop_unique_holder_t make_coop(coop_handle_t parent, disp_binder_shptr_t default_binder) override
Create an instance of a new coop.
bool final_deregister_coop(coop_shptr_t coop) noexcept override
Do final actions of the cooperation deregistration.
event_queue_impl_t m_event_queue
Queue for execution_demands which must be handled on the main thread.
env_infrastructure_t(environment_t &env, timer_manager_factory_t timer_factory, error_logger_shptr_t error_logger, coop_listener_unique_ptr_t coop_listener, mbox_t stats_distribution_mbox)
disp_binder_shptr_t make_default_disp_binder() override
Create a binder for the default dispatcher.
void handle_expired_timers_if_any(std::unique_lock< std::mutex > &acquired_lock) noexcept
stats::controller_t & stats_controller() noexcept override
Get stats controller for the environment.
Activity_Tracker m_activity_tracker
Actual activity tracker for main working thread.
virtual void accept(std::type_index type_index, mbox_t mbox, message_ref_t msg) override
Accept and store info about elapsed timer.
coop_repo_t(outliving_reference_t< environment_t > env, coop_listener_unique_ptr_t coop_listener)
Initializing constructor.
A basic part of implementation of dispatcher to be used in places where default dispatcher is needed.
std::atomic< std::size_t > m_agents_bound
Counter of agents bound to that dispatcher.
outliving_reference_t< Event_Queue_Type > m_event_queue
Event queue for that dispatcher.
default_dispatcher_basis_t(outliving_reference_t< Event_Queue_Type > event_queue)
void bind(agent_t &agent) noexcept override
Bind agent to dispatcher.
void undo_preallocation(agent_t &) noexcept override
Undo resources allocation.
void unbind(agent_t &) noexcept override
Unbind agent from dispatcher.
void preallocate_resources(agent_t &) override
Allocate resources in dispatcher for new agent.
outliving_reference_t< default_dispatcher_t > m_dispatcher
Dispatcher to work with.
void distribute(const mbox_t &mbox) override
Send appropriate notification about the current value.
An implementation of dispatcher to be used in places where default dispatcher is needed.
outliving_reference_t< Activity_Tracker > m_activity_tracker
Activity tracker.
stats::auto_registered_source_holder_t< disp_data_source_t > m_data_source
Data source for speading run-time stats.
default_dispatcher_t(outliving_reference_t< environment_t > env, outliving_reference_t< Event_Queue_Type > event_queue, outliving_reference_t< Activity_Tracker > activity_tracker)
virtual void accept(std::type_index type_index, mbox_t mbox, message_ref_t msg) override
Accept and store info about elapsed timer.
stats::activity_tracking_stuff::stats_collector_t< stats::activity_tracking_stuff::null_lock > m_waiting
stats::activity_tracking_stuff::stats_collector_t< stats::activity_tracking_stuff::null_lock > m_working
const mbox_t m_next_turn_mbox
Mbox for delayed messages for initiation of next turn.
const mbox_t m_distribution_mbox
Mbox for sending messages with run-time statistics.
virtual void add(stats::source_t &what) override
Registration of new data source.
virtual std::chrono::steady_clock::duration set_distribution_period(std::chrono::steady_clock::duration period) override
Set distribution period.
void send_next_message(std::chrono::steady_clock::duration pause, const int run_id)
Helper method for sending next instance of next_turn message.
virtual const mbox_t & mbox() const override
Get the mbox for receiving monitoring information.
stats_controller_t(mbox_t distribution_mbox, mbox_t next_turn_mbox)
Initializing constructor.
std::chrono::steady_clock::duration distribute_current_data()
Actual distribution of the current statistics.
virtual void remove(stats::source_t &what) noexcept override
Deregistration of previously registered data source.
An interface for environment_infrastructure entity.
SObjectizer Environment.
An interface of event queue for agent.
A basic part for various implementations of coop_repository.
Helper class for holding the current chain of coops for the final deregistration.
Helper class for accessing protected members from mbox interface.
void deliver_message_from_timer(const std::type_index &msg_type, const message_ref_t &message)
A base class for agent messages.
Definition message.hpp:47
Helper class for indication of long-lived reference via its type.
Definition outliving.hpp:98
A holder for data-souce that should be automatically registered and deregistered in registry.
A public interface for control SObjectizer monitoring options.
An interface for initiation of next turn in stats distribution.
mbox_id_t id() const override
Unique ID of this mbox.
void subscribe_event_handler(const std::type_index &, abstract_message_sink_t &) override
Add the message handler.
mbox_type_t type() const override
Get the type of message box.
void drop_delivery_filter(const std::type_index &, abstract_message_sink_t &) noexcept override
Removes delivery filter for message type and subscriber.
std::string query_name() const override
Get the mbox name.
environment_t & environment() const noexcept override
void unsubscribe_event_handler(const std::type_index &, abstract_message_sink_t &) noexcept override
Remove all message handlers.
void set_delivery_filter(const std::type_index &, const delivery_filter_t &, abstract_message_sink_t &) override
Set a delivery filter for message type and subscriber.
static mbox_t make(environment_t &env)
Helper for simplify creation of that mboxes of that type.
void do_deliver_message(message_delivery_mode_t, const std::type_index &msg_type, const message_ref_t &message, unsigned int) override
Deliver message for all subscribers with respect to message limits.
environment_t & m_env
Environment for which that mbox is created.
A type for storing prefix of data_source name.
Definition prefix.hpp:32
An interface of data sources repository.
An interface of data source.
An indentificator for the timer.
Definition timers.hpp:82
An interface for collector of elapsed timers.
Definition timers.hpp:452
Timer manager interface.
Definition timers.hpp:425
#define SO_5_FUNC
Definition declspec.hpp:48
#define SO_5_THROW_EXCEPTION(error_code, desc)
Definition exception.hpp:74
auto unlock_do_and_lock_again(std::unique_lock< std::mutex > &acquired_lock, Action &&action) -> decltype(action())
main_thread_status_t
A short name for namespace with run-time stats stuff.
void wakeup_if_waiting(main_thread_sync_objects_t &sync_objects)
Simple single-threaded environment infrastructure with thread safety.
SO_5_FUNC environment_infrastructure_factory_t factory(params_t &&params)
A factory for creation of simple thread-safe single-thread environment infrastructure object.
Various reusable stuff which can be used in implementation of single-threaded environment infrastruct...
shutdown_status_t
A short name for namespace with run-time stats stuff.
@ must_be_started
Shutdown must be started as soon as possible.
@ completed
Shutdown completed and work of environment must be finished.
@ in_progress
Shutdown is initiated but not finished yet.
void send_thread_activity_stats(const so_5::mbox_t &mbox, const stats::prefix_t &prefix, const current_thread_id_t &thread_id, real_activity_tracker_t &activity_tracker)
void send_thread_activity_stats(const mbox_t &, const stats::prefix_t &, const current_thread_id_t &, fake_activity_tracker_t &)
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
message_delivery_mode_t
Possible modes of message/signal delivery.
Definition types.hpp:172
mbox_type_t
Type of the message box.
Definition mbox.hpp:163
A special class for generation of names for dispatcher data sources.
A bunch of sync objects which need to be shared between various parts of env_infrastructure.
std::condition_variable m_wakeup_condition
A condition to sleep on when no activities to handle.
Statistical data for run-time monitoring of coop repository content.
A description of event execution demand.
A special class for cases where lock is not needed at all.
next_turn(outliving_reference_t< next_turn_handler_t > handler, int run_id)
outliving_reference_t< next_turn_handler_t > m_handler
Who must do next turn.
Statistics for run-time monitoring.
Definition timers.hpp:136