SObjectizer  5.8
Loading...
Searching...
No Matches
active_group/pub.hpp
Go to the documentation of this file.
1/*
2 SObjectizer 5.
3*/
4
5/*!
6 \file
7 \brief Functions for creating and binding to the active group dispatcher.
8*/
9
10#pragma once
11
12#include <so_5/declspec.hpp>
13
14#include <so_5/disp_binder.hpp>
15#include <so_5/nonempty_name.hpp>
16
17#include <so_5/disp/mpsc_queue_traits/pub.hpp>
18
19#include <so_5/disp/reuse/work_thread_activity_tracking.hpp>
20#include <so_5/disp/reuse/work_thread_factory_params.hpp>
21
22#include <string>
23#include <string_view>
24
25namespace so_5
26{
27
28namespace disp
29{
30
31namespace active_group
32{
33
34/*!
35 * \brief Alias for namespace with traits of event queue.
36 *
37 * \since
38 * v.5.5.10
39 */
40namespace queue_traits = so_5::disp::mpsc_queue_traits;
41
42//
43// disp_params_t
44//
45/*!
46 * \brief Parameters for active group dispatcher.
47 *
48 * \since
49 * v.5.5.10
50 */
54 {
55 using activity_tracking_mixin_t = so_5::disp::reuse::
57 using thread_factory_mixin_t = so_5::disp::reuse::
59
60 public :
61 //! Default constructor.
62 disp_params_t() = default;
63
64 friend inline void
65 swap( disp_params_t & a, disp_params_t & b ) noexcept
66 {
67 swap(
68 static_cast< activity_tracking_mixin_t & >(a),
69 static_cast< activity_tracking_mixin_t & >(b) );
70
71 swap(
74
75 swap( a.m_queue_params, b.m_queue_params );
76 }
77
78 //! Setter for queue parameters.
81 {
82 m_queue_params = std::move(p);
83 return *this;
84 }
85
86 //! Tuner for queue parameters.
87 /*!
88 * Accepts lambda-function or functional object which tunes
89 * queue parameters.
90 \code
91 so_5::disp::active_group::make_dispatcher( env,
92 "my_active_group_disp",
93 so_5::disp::active_group::disp_params_t{}.tune_queue_params(
94 []( so_5::disp::active_group::queue_traits::queue_params_t & p ) {
95 p.lock_factory( so_5::disp::active_group::queue_traits::simple_lock_factory() );
96 } ) );
97 \endcode
98 */
99 template< typename L >
102 {
104 return *this;
105 }
106
107 //! Getter for queue parameters.
108 const queue_traits::queue_params_t &
110 {
111 return m_queue_params;
112 }
113
114 private :
115 //! Queue parameters.
117 };
118
119namespace impl {
120
122
123//
124// basic_dispatcher_iface_t
125//
126/*!
127 * \brief The very basic interface of %active_group dispatcher.
128 *
129 * This class contains a minimum that is necessary for implementation
130 * of dispatcher_handle class.
131 *
132 * \since
133 * v.5.6.0
134 */
137 {
138 public :
139 virtual ~basic_dispatcher_iface_t() noexcept = default;
140
141 [[nodiscard]]
142 virtual disp_binder_shptr_t
144 };
145
148
150
151} /* namespace impl */
152
153//
154// dispatcher_handle_t
155//
156
157/*!
158 * \since
159 * v.5.6.0
160 *
161 * \brief A handle for %active_group dispatcher.
162 */
163class [[nodiscard]] dispatcher_handle_t
164 {
166
167 //! A reference to actual implementation of a dispatcher.
169
171 impl::basic_dispatcher_iface_shptr_t dispatcher ) noexcept
173 {}
174
175 //! Is this handle empty?
176 bool
177 empty() const noexcept { return !m_dispatcher; }
178
179 public :
180 dispatcher_handle_t() noexcept = default;
181
182 //! Get a binder for that dispatcher.
183 /*!
184 * \attention
185 * An attempt to call this method on empty handle is UB.
186 */
187 [[nodiscard]]
190 //! Name of group for a new agent.
192 {
193 return m_dispatcher->binder( std::move(group_name) );
194 }
195
196 //! Is this handle empty?
197 operator bool() const noexcept { return empty(); }
198
199 //! Does this handle contain a reference to dispatcher?
200 bool
201 operator!() const noexcept { return !empty(); }
202
203 //! Drop the content of handle.
204 void
205 reset() noexcept { m_dispatcher.reset(); }
206 };
207
208/*!
209 * \brief Create an instance of %active_group dispatcher.
210 *
211 * \par Usage sample
212\code
213auto disp = so_5::disp::active_group::make_dispatcher(
214 env,
215 "request_handler",
216 // Additional params with specific options for queue's traits.
217 so_5::disp::active_group::disp_params_t{}.tune_queue_params(
218 []( so_5::disp::active_group::queue_traits::queue_params_t & p ) {
219 p.lock_factory( so_5::disp::active_obj::queue_traits::simple_lock_factory() );
220 } ) );
221auto coop = env.make_coop(
222 // The main dispatcher for that coop will be
223 // this instance of active_group dispatcher.
224 disp.binder( "request_handler" ) );
225\endcode
226 *
227 * \since
228 * v.5.6.0
229 */
232 //! SObjectizer Environment to work in.
233 so_5::environment_t & env,
234 //! Value for creating names of data sources for
235 //! run-time monitoring.
236 const std::string_view data_sources_name_base,
237 //! Parameters for dispatcher.
238 disp_params_t params );
239
240/*!
241 * \brief Create an instance of %active_group dispatcher.
242 *
243 * \par Usage sample
244\code
245auto disp = so_5::disp::active_group::make_dispatcher(
246 env,
247 "long_req_handlers" );
248
249auto coop = env.make_coop(
250 // The main dispatcher for that coop will be
251 // this instance of active_group dispatcher.
252 disp.binder( "passive_objects" ) );
253\endcode
254 *
255 * \since
256 * v.5.6.0
257 */
260 //! SObjectizer Environment to work in.
261 so_5::environment_t & env,
262 //! Value for creating names of data sources for
263 //! run-time monitoring.
264 const std::string_view data_sources_name_base )
265 {
266 return make_dispatcher( env, data_sources_name_base, disp_params_t{} );
267 }
268
269/*!
270 * \brief Create an instance of %active_group dispatcher.
271 *
272 * \par Usage sample
273\code
274auto disp = so_5::disp::active_group::make_dispatcher( env );
275
276auto coop = env.make_coop(
277 // The main dispatcher for that coop will be
278 // this instance of active_group dispatcher.
279 disp.binder( "passive_objects" ) );
280\endcode
281 *
282 * \since
283 * v.5.6.0
284 */
287 //! SObjectizer Environment to work in.
288 so_5::environment_t & env )
289 {
290 return make_dispatcher( env, std::string_view{} );
291 }
292
293} /* namespace active_group */
294
295} /* namespace disp */
296
297} /* namespace so_5 */
A base class for agents.
Definition agent.hpp:673
Alias for namespace with traits of event queue.
const queue_traits::queue_params_t & queue_params() const
Getter for queue parameters.
disp_params_t & tune_queue_params(L tunner)
Tuner for queue parameters.
queue_traits::queue_params_t m_queue_params
Queue parameters.
friend void swap(disp_params_t &a, disp_params_t &b) noexcept
disp_params_t & set_queue_params(queue_traits::queue_params_t p)
Setter for queue parameters.
disp_params_t()=default
Default constructor.
A handle for active_group dispatcher.
dispatcher_handle_t(impl::basic_dispatcher_iface_shptr_t dispatcher) noexcept
impl::basic_dispatcher_iface_shptr_t m_dispatcher
A reference to actual implementation of a dispatcher.
bool empty() const noexcept
Is this handle empty?
void reset() noexcept
Drop the content of handle.
bool operator!() const noexcept
Does this handle contain a reference to dispatcher?
operator bool() const noexcept
Is this handle empty?
disp_binder_shptr_t binder(nonempty_name_t group_name) const
Get a binder for that dispatcher.
const std::string m_group_name
Name of group for new agents.
void preallocate_resources(agent_t &) override
Allocate resources in dispatcher for new agent.
void unbind(agent_t &) noexcept override
Unbind agent from dispatcher.
void bind(agent_t &agent) noexcept override
Bind agent to dispatcher.
actual_dispatcher_iface_shptr_t m_disp
Dispatcher to be used.
void undo_preallocation(agent_t &) noexcept override
Undo resources allocation.
actual_binder_t(actual_dispatcher_iface_shptr_t disp, nonempty_name_t group_name) noexcept
An actual interface of active group dispatcher.
virtual so_5::event_queue_t * query_thread_for_group(const std::string &group_name) noexcept=0
Get the event_queue for the specified active group.
virtual void release_thread_for_group(const std::string &group_name) noexcept=0
Release the thread for the specified active group.
virtual void allocate_thread_for_group(const std::string &group_name)=0
Create a new thread for a group if it necessary.
The very basic interface of active_group dispatcher.
virtual disp_binder_shptr_t binder(nonempty_name_t group_name)=0
static dispatcher_handle_t make(actual_dispatcher_iface_shptr_t disp) noexcept
outliving_reference_t< dispatcher_template_t > m_dispatcher
Dispatcher to work with.
void distribute_value_for_work_thread(const so_5::mbox_t &mbox, const std::string &group_name, const thread_with_refcounter_t &wt)
disp_data_source_t(const std::string_view name_base, outliving_reference_t< dispatcher_template_t > disp)
void distribute(const so_5::mbox_t &mbox) override
Send appropriate notification about the current value.
void release_thread_for_group(const std::string &group_name) noexcept override
Release the thread for the specified active group.
void allocate_thread_for_group(const std::string &group_name) override
Create a new thread for a group if it necessary.
outliving_reference_t< environment_t > m_env
SObjectizer Environment to work in.
so_5::event_queue_t * query_thread_for_group(const std::string &group_name) noexcept override
Get the event_queue for the specified active group.
active_group_map_t m_groups
A map of dispatchers for active groups.
const disp_params_t m_params
Parameters for the dispatcher.
dispatcher_template_t(outliving_reference_t< environment_t > env, const std::string_view name_base, disp_params_t params)
work_thread_shptr_t search_and_try_remove_group_from_map(const std::string &group_name) noexcept
Helper function for searching and erasing agent's thread from map of active threads.
stats::auto_registered_source_holder_t< disp_data_source_t > m_data_source
Data source for run-time monitoring.
disp_binder_shptr_t binder(nonempty_name_t group_name) override
Container for storing parameters for MPSC queue.
Mixin that holds optional work thread factory.
Interface for dispatcher binders.
SObjectizer Environment.
An interface of event queue for agent.
A class for the name which cannot be empty.
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
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)
Active groups dispatcher implemetation details.
Active groups dispatcher.
dispatcher_handle_t make_dispatcher(so_5::environment_t &env, const std::string_view data_sources_name_base)
Create an instance of active_group dispatcher.
dispatcher_handle_t make_dispatcher(so_5::environment_t &env)
Create an instance of active_group 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_group 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