SObjectizer  5.8
Loading...
Searching...
No Matches
active_obj/pub.hpp
Go to the documentation of this file.
1/*
2 SObjectizer 5.
3*/
4
5/*!
6 \file
7 \brief Dispatcher creation and agent binding functions.
8*/
9
10#pragma once
11
12#include <string>
13
14#include <so_5/declspec.hpp>
15
16#include <so_5/disp_binder.hpp>
17
18#include <so_5/disp/mpsc_queue_traits/pub.hpp>
19
20#include <so_5/disp/reuse/work_thread_activity_tracking.hpp>
21#include <so_5/disp/reuse/work_thread_factory_params.hpp>
22
23namespace so_5
24{
25
26namespace disp
27{
28
29namespace active_obj
30{
31
32/*!
33 * \brief Alias for namespace with traits of event queue.
34 *
35 * \since
36 * v.5.5.10
37 */
38namespace queue_traits = so_5::disp::mpsc_queue_traits;
39
40//
41// disp_params_t
42//
43/*!
44 * \brief Parameters for active object dispatcher.
45 *
46 * \since
47 * v.5.5.10
48 */
52 {
53 using activity_tracking_mixin_t = so_5::disp::reuse::
55 using thread_factory_mixin_t = so_5::disp::reuse::
57
58 public :
59 //! Default constructor.
60 disp_params_t() = default;
61
62 friend inline void
63 swap( disp_params_t & a, disp_params_t & b ) noexcept
64 {
65 swap(
66 static_cast< activity_tracking_mixin_t & >(a),
67 static_cast< activity_tracking_mixin_t & >(b) );
68
69 swap(
72
73 swap( a.m_queue_params, b.m_queue_params );
74 }
75
76 //! Setter for queue parameters.
79 {
80 m_queue_params = std::move(p);
81 return *this;
82 }
83
84 //! Tuner for queue parameters.
85 /*!
86 * Accepts lambda-function or functional object which tunes
87 * queue parameters.
88 \code
89 so_5::disp::active_obj::make_dispatcher( env,
90 "my_active_obj_disp",
91 so_5::disp::active_obj::disp_params_t{}.tune_queue_params(
92 []( so_5::disp::active_obj::queue_traits::queue_params_t & p ) {
93 p.lock_factory( so_5::disp::active_obj::queue_traits::simple_lock_factory() );
94 } ) );
95 \endcode
96 */
97 template< typename L >
100 {
102 return *this;
103 }
104
105 //! Getter for queue parameters.
106 const queue_traits::queue_params_t &
108 {
109 return m_queue_params;
110 }
111
112 private :
113 //! Queue parameters.
115 };
116
117namespace impl
118{
119
121
122} /* namespace impl */
123
124//
125// dispatcher_handle_t
126//
127
128/*!
129 * \since
130 * v.5.6.0
131 *
132 * \brief A handle for %active_obj dispatcher.
133 */
134class [[nodiscard]] dispatcher_handle_t
135 {
137
138 //! Binder for the dispatcher.
140
141 dispatcher_handle_t( disp_binder_shptr_t binder ) noexcept
142 : m_binder{ std::move(binder) }
143 {}
144
145 //! Is this handle empty?
146 bool
147 empty() const noexcept { return !m_binder; }
148
149 public :
150 dispatcher_handle_t() noexcept = default;
151
152 //! Get a binder for that dispatcher.
153 [[nodiscard]]
155 binder() const noexcept
156 {
157 return m_binder;
158 }
159
160 //! Is this handle empty?
161 operator bool() const noexcept { return empty(); }
162
163 //! Does this handle contain a reference to dispatcher?
164 bool
165 operator!() const noexcept { return !empty(); }
166
167 //! Drop the content of handle.
168 void
169 reset() noexcept { m_binder.reset(); }
170 };
171
172/*!
173 * \brief Create an instance of %active_obj dispatcher.
174 *
175 * \par Usage sample
176\code
177auto disp = so_5::disp::active_obj::make_dispatcher(
178 env,
179 "db_handler",
180 // Additional params with specific options for queue's traits.
181 so_5::disp::active_obj::disp_params_t{}.tune_queue_params(
182 []( so_5::disp::active_obj::queue_traits::queue_params_t & p ) {
183 p.lock_factory( so_5::disp::active_obj::queue_traits::simple_lock_factory() );
184 } ) );
185auto coop = env.make_coop(
186 // The main dispatcher for that coop will be
187 // this instance of active_obj dispatcher.
188 disp->binder() );
189\endcode
190 *
191 * \since
192 * v.5.6.0
193 */
196 //! SObjectizer Environment to work in.
197 environment_t & env,
198 //! Value for creating names of data sources for
199 //! run-time monitoring.
200 const std::string_view data_sources_name_base,
201 //! Parameters for dispatcher.
202 disp_params_t params );
203
204/*!
205 * \brief Create an instance of %active_obj dispatcher.
206 *
207 * \par Usage sample
208\code
209auto disp = so_5::disp::active_obj::make_dispatcher(
210 env,
211 "db_handler" );
212
213auto coop = env.make_coop(
214 // The main dispatcher for that coop will be
215 // this instance of active_obj dispatcher.
216 disp.binder() );
217\endcode
218 *
219 * \since
220 * v.5.6.0
221 */
224 //! SObjectizer Environment to work in.
225 environment_t & env,
226 //! Value for creating names of data sources for
227 //! run-time monitoring.
228 const std::string_view data_sources_name_base )
229 {
230 return make_dispatcher( env, data_sources_name_base, disp_params_t{} );
231 }
232
233/*!
234 * \brief Create a private %active_obj dispatcher.
235 *
236 * \par Usage sample
237\code
238auto disp = so_5::disp::active_obj::make_dispatcher( env );
239
240auto coop = env.make_coop(
241 // The main dispatcher for that coop will be
242 // this instance of active_obj dispatcher.
243 disp.binder() );
244\endcode
245 *
246 * \since
247 * v.5.6.0
248 */
251 //! SObjectizer Environment to work in.
252 environment_t & env )
253 {
254 return make_dispatcher( env, std::string_view{} );
255 }
256
257} /* namespace active_obj */
258
259} /* namespace disp */
260
261} /* namespace so_5 */
A base class for agents.
Definition agent.hpp:673
Alias for namespace with traits of event queue.
disp_params_t & tune_queue_params(L tunner)
Tuner for queue parameters.
disp_params_t & set_queue_params(queue_traits::queue_params_t p)
Setter for queue parameters.
queue_traits::queue_params_t m_queue_params
Queue parameters.
const queue_traits::queue_params_t & queue_params() const
Getter for queue parameters.
disp_params_t()=default
Default constructor.
friend void swap(disp_params_t &a, disp_params_t &b) noexcept
A handle for active_obj dispatcher.
bool empty() const noexcept
Is this handle empty?
disp_binder_shptr_t m_binder
Binder for the dispatcher.
bool operator!() const noexcept
Does this handle contain a reference to dispatcher?
dispatcher_handle_t(disp_binder_shptr_t binder) noexcept
disp_binder_shptr_t binder() const noexcept
Get a binder for that dispatcher.
operator bool() const noexcept
Is this handle empty?
void reset() noexcept
Drop the content of handle.
static dispatcher_handle_t make(disp_binder_shptr_t binder) noexcept
void distribute(const mbox_t &mbox) override
Send appropriate notification about the current value.
disp_data_source_t(const std::string_view name_base, outliving_reference_t< dispatcher_template_t > disp)
stats::prefix_t m_base_prefix
Basic prefix for data source names.
void distribute_value_for_work_thread(const mbox_t &mbox, const agent_t *agent, Work_Thread &wt)
outliving_reference_t< dispatcher_template_t > m_dispatcher
Dispatcher to work with.
void undo_preallocation(agent_t &agent) noexcept override
Undo resources allocation.
outliving_reference_t< environment_t > m_env
SObjectizer Environment to work in.
const disp_params_t m_params
Parameters for the dispatcher.
stats::auto_registered_source_holder_t< disp_data_source_t > m_data_source
Data source for run-time monitoring.
dispatcher_template_t(outliving_reference_t< environment_t > env, const std::string_view name_base, disp_params_t params)
void unbind(agent_t &agent) noexcept override
Unbind agent from dispatcher.
agent_thread_map_t m_agent_threads
A map from agents to single thread dispatchers.
void preallocate_resources(agent_t &agent) override
Allocate resources in dispatcher for new agent.
void bind(agent_t &agent) noexcept override
Bind agent to dispatcher.
Container for storing parameters for MPSC queue.
Mixin that holds optional work thread factory.
Interface for dispatcher binders.
SObjectizer Environment.
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 type for storing prefix of data_source name.
Definition prefix.hpp:32
An interface of data source.
#define SO_5_FUNC
Definition declspec.hpp:48
#define SO_5_THROW_EXCEPTION(error_code, desc)
Definition exception.hpp:74
void shutdown_and_wait(T &w)
Just a helper function for consequetive call to shutdown and wait.
void send_thread_activity_stats(const so_5::mbox_t &, const stats::prefix_t &, work_thread::work_thread_no_activity_tracking_t &)
void send_thread_activity_stats(const so_5::mbox_t &mbox, const stats::prefix_t &prefix, work_thread::work_thread_with_activity_tracking_t &wt)
void send_demands_count_stats(const so_5::mbox_t &mbox, const stats::prefix_t &prefix, Work_Thread &wt)
Active objects dispatcher implemetation details.
Active objects dispatcher.
dispatcher_handle_t make_dispatcher(environment_t &env, const std::string_view data_sources_name_base)
Create an instance of active_obj dispatcher.
SO_5_FUNC dispatcher_handle_t make_dispatcher(environment_t &env, const std::string_view data_sources_name_base, disp_params_t params)
Create an instance of active_obj dispatcher.
dispatcher_handle_t make_dispatcher(environment_t &env)
Create a private active_obj dispatcher.
Various stuff related to MPSC event queue implementation and tuning.
Implemetation details of dispatcher's working thread.
Reusable components for dispatchers.
Event dispatchers.
All stuff related to run-time monitoring and statistics.
Private part of message limit implementation.
Definition agent.cpp:33