SObjectizer 5.8
Loading...
Searching...
No Matches
prio_dedicated_threads/one_per_prio/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 of the dispatcher with
8 * dedicated threads per priority.
9 *
10 * \since
11 * v.5.5.8
12 */
13
14#pragma once
15
16#include <so_5/declspec.hpp>
17
18#include <so_5/disp_binder.hpp>
19
20#include <so_5/priority.hpp>
21
22#include <so_5/disp/mpsc_queue_traits/pub.hpp>
23
24#include <so_5/disp/reuse/work_thread_activity_tracking.hpp>
25#include <so_5/disp/reuse/work_thread_factory_params.hpp>
26
27#include <string>
28
29namespace so_5 {
30
31namespace disp {
32
33namespace prio_dedicated_threads {
34
35namespace one_per_prio {
36
37/*!
38 * \brief Alias for namespace with traits of event queue.
39 *
40 * \since
41 * v.5.5.10
42 */
43namespace queue_traits = so_5::disp::mpsc_queue_traits;
44
45//
46// disp_params_t
47//
48/*!
49 * \brief Parameters for a dispatcher.
50 *
51 * \since
52 * v.5.5.10
53 */
57 {
58 using activity_tracking_mixin_t = so_5::disp::reuse::
60 using thread_factory_mixin_t = so_5::disp::reuse::
62
63 public :
64 //! Default constructor.
65 disp_params_t() = default;
66
67 friend inline void
68 swap( disp_params_t & a, disp_params_t & b ) noexcept
69 {
70 swap(
71 static_cast< activity_tracking_mixin_t & >(a),
72 static_cast< activity_tracking_mixin_t & >(b) );
73
74 swap(
75 static_cast< work_thread_factory_mixin_t & >(a),
76 static_cast< work_thread_factory_mixin_t & >(b) );
77
79 }
80
81 //! Setter for queue parameters.
84 {
85 m_queue_params = std::move(p);
86 return *this;
87 }
88
89 //! Tuner for queue parameters.
90 /*!
91 * Accepts lambda-function or functional object which tunes
92 * queue parameters.
93 \code
94 namespace prio_disp = so_5::disp::prio_dedicated_threads::one_per_prio;
95 auto disp = prio_disp::make_dispatcher( env,
96 "my_prio_disp",
97 prio_disp::disp_params_t{}.tune_queue_params(
98 []( prio_disp::queue_traits::queue_params_t & p ) {
99 p.lock_factory( prio_disp::queue_traits::simple_lock_factory() );
100 } ) );
101 \endcode
102 */
103 template< typename L >
106 {
108 return *this;
109 }
110
111 //! Getter for queue parameters.
112 const queue_traits::queue_params_t &
114 {
115 return m_queue_params;
116 }
117
118 private :
119 //! Queue parameters.
121 };
122
123namespace impl
124{
125
127
128} /* namespace impl */
129
130//
131// dispatcher_handle_t
132//
133
134/*!
135 * \since
136 * v.5.6.0
137 *
138 * \brief A handle for %prio_dedicated_threads::one_per_prio dispatcher.
139 */
140class [[nodiscard]] dispatcher_handle_t
141 {
143
144 //! Binder for the dispatcher.
145 disp_binder_shptr_t m_binder;
146
147 dispatcher_handle_t( disp_binder_shptr_t binder ) noexcept
148 : m_binder{ std::move(binder) }
149 {}
150
151 //! Is this handle empty?
152 bool
153 empty() const noexcept { return !m_binder; }
154
155 public :
156 dispatcher_handle_t() noexcept = default;
157
158 //! Get a binder for that dispatcher.
159 [[nodiscard]]
160 disp_binder_shptr_t
161 binder() const noexcept
162 {
163 return m_binder;
164 }
165
166 //! Is this handle empty?
167 operator bool() const noexcept { return empty(); }
168
169 //! Does this handle contain a reference to dispatcher?
170 bool
171 operator!() const noexcept { return !empty(); }
172
173 //! Drop the content of handle.
174 void
175 reset() noexcept { m_binder.reset(); }
176 };
177
178//
179// make_dispatcher
180//
181/*!
182 * \brief Create an instance of %one_per_prio dispatcher.
183 *
184 * \par Usage sample
185\code
186using namespace so_5::disp::prio_dedicated_threads::one_per_prio;
187auto common_thread_disp = make_dispatcher(
188 env,
189 "request_processor"
190 disp_params_t{}.tune_queue_params(
191 []( queue_traits::queue_params_t & p ) {
192 p.lock_factory( queue_traits::simple_lock_factory() );
193 } ) );
194auto coop = env.make_coop(
195 // The main dispatcher for that coop will be
196 // private strictly_ordered dispatcher.
197 common_thread_disp.binder() );
198\endcode
199 *
200 * \since
201 * v.5.6.0
202 */
205 //! SObjectizer Environment to work in.
206 environment_t & env,
207 //! Value for creating names of data sources for
208 //! run-time monitoring.
209 const std::string_view data_sources_name_base,
210 //! Parameters for the dispatcher.
211 disp_params_t params );
212
213//
214// make_dispatcher
215//
216/*!
217 * \brief Create an instance of %one_per_prio dispatcher.
218 *
219 * \par Usage sample
220\code
221using namespace so_5::disp::prio_dedicated_threads::one_per_prio;
222auto common_thread_disp = make_dispatcher( env, "request_processor" );
223
224auto coop = env.make_coop(
225 // The main dispatcher for that coop will be
226 // private strictly_ordered dispatcher.
227 common_thread_disp.binder() );
228\endcode
229 *
230 * \since
231 * v.5.5.8
232 */
235 //! SObjectizer Environment to work in.
236 environment_t & env,
237 //! Value for creating names of data sources for
238 //! run-time monitoring.
239 const std::string_view data_sources_name_base )
240 {
241 return make_dispatcher( env, data_sources_name_base, disp_params_t{} );
242 }
243
244//
245// make_dispatcher
246//
247/*!
248 * \brief Create an instance of %one_per_prio dispatcher.
249 *
250 * \par Usage sample
251\code
252using namespace so_5::disp::prio_dedicated_threads::one_per_prio;
253auto common_thread_disp = make_dispatcher( env );
254
255auto coop = env.make_coop(
256 // The main dispatcher for that coop will be
257 // private strictly_ordered dispatcher.
258 common_thread_disp.binder() );
259\endcode
260 *
261 * \since
262 * v.5.6.0
263 */
266 {
267 return make_dispatcher( env, std::string_view{} );
268 }
269
270} /* namespace one_per_prio */
271
272} /* namespace prio_dedicated_threads */
273
274} /* namespace disp */
275
276} /* namespace so_5 */
A base class for agents.
Definition agent.hpp:673
priority_t so_priority() const noexcept
Get the priority of the agent.
Definition agent.hpp:2555
void so_bind_to_dispatcher(event_queue_t &queue) noexcept
Binding agent to the dispatcher.
Definition agent.cpp:872
Container for storing parameters for MPSC queue.
const lock_factory_t & lock_factory() const
Getter for lock factory.
queue_params_t & operator=(queue_params_t &&o) noexcept
Move operator.
friend void swap(queue_params_t &a, queue_params_t &b) noexcept
disp_params_t & set_queue_params(queue_traits::queue_params_t p)
Setter for queue parameters.
const queue_traits::queue_params_t & queue_params() const
Getter for queue parameters.
bool operator!() const noexcept
Does this handle contain a reference to dispatcher?
disp_binder_shptr_t binder() const noexcept
Get a binder for that dispatcher.
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)
void distribute_value_for_work_thread(const mbox_t &mbox, priority_t priority, std::size_t agents_count, Work_Thread &wt)
stats::auto_registered_source_holder_t< disp_data_source_t > m_data_source
Data source for run-time monitoring.
std::vector< std::unique_ptr< Work_Thread > > m_threads
Working threads for every priority.
dispatcher_template_t(outliving_reference_t< environment_t > env, const std::string_view name_base, disp_params_t params)
std::array< std::atomic< std::size_t >, so_5::prio::total_priorities_count > m_agents_per_priority
Counters for agent count for every priority.
void allocate_work_threads(environment_t &env, const disp_params_t &params)
Allocate work threads for dispatcher.
void preallocate_resources(agent_t &) override
Allocate resources in dispatcher for new agent.
so_5::stats::work_thread_activity_stats_t take_activity_stats()
Get the activity stats.
so_5::current_thread_id_t thread_id() const
Get ID of work thread.
friend void swap(work_thread_activity_tracking_flag_mixin_t &a, work_thread_activity_tracking_flag_mixin_t &b) noexcept
Mixin that holds optional work thread factory.
friend void swap(work_thread_factory_mixin_t &a, work_thread_factory_mixin_t &b) noexcept
Interface for dispatcher binders.
SObjectizer Environment.
stats::repository_t & stats_repository()
Access to repository of data sources for run-time monitoring.
Helper class for indication of long-lived reference via its type.
Definition outliving.hpp:98
T & get() const noexcept
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
constexpr const char * c_str() const noexcept
Access to prefix value.
Definition prefix.hpp:80
prefix_t(const std::string &value) noexcept(noexcept(value.c_str()))
Initializing constructor.
Definition prefix.hpp:73
An interface of data source.
#define SO_5_FUNC
Definition declspec.hpp:48
Some reusable and low-level classes/functions which can be used in public header files.
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)
Various stuff related to MPSC event queue implementation and tuning.
Implementation details for dispatcher with one thread per priority.
Dispatcher which creates exactly one thread per priority.
dispatcher_handle_t make_dispatcher(environment_t &env, const std::string_view data_sources_name_base)
Create an instance of one_per_prio 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 one_per_prio dispatcher.
dispatcher_handle_t make_dispatcher(environment_t &env)
Create an instance of one_per_prio dispatcher.
Dispatchers with dedicated threads for every priority.
Implemetation details of dispatcher's working thread.
Reusable components for dispatchers.
work_thread_holder_t acquire_work_thread(const work_thread_factory_mixin_t< Params > &params, environment_t &env)
Helper function for acquiring a new worker thread from an appropriate work thread factory.
so_5::stats::prefix_t make_disp_prefix(const std::string_view disp_type, const std::string_view data_sources_name_base, const void *disp_this_pointer)
Create basic prefix for dispatcher data source names.
std::unique_ptr< Disp_Iface_Type > make_actual_dispatcher(outliving_reference_t< environment_t > env, const std::string_view name_base, Disp_Params_Type disp_params, Args &&...args)
Helper function for creation of dispatcher instance with respect to work thread activity tracking fla...
Event dispatchers.
Helpers for working with priorities.
Definition priority.hpp:73
const unsigned int total_priorities_count
Total count of priorities.
Definition priority.hpp:105
Declarations of messages used by run-time monitoring and statistics.
Definition messages.hpp:36
Predefined suffixes of data-sources.
Definition std_names.hpp:55
SO_5_FUNC suffix_t agent_count()
Suffix for data source with count of agents bound to some entity.
Definition std_names.cpp:66
SO_5_FUNC suffix_t work_thread_queue_size()
Suffix for data source with count of demands in a working thread event queue.
Definition std_names.cpp:78
SO_5_FUNC suffix_t work_thread_activity()
Suffix for data source with work thread activity statistics.
Definition std_names.cpp:84
All stuff related to run-time monitoring and statistics.
Private part of message limit implementation.
Definition agent.cpp:33
std::size_t to_size_t(priority_t priority)
Helper function for conversion from priority to size_t.
Definition priority.hpp:48
priority_t
Definition of supported priorities.
Definition priority.hpp:28
void send(Target &&to, Args &&... args)
A utility function for creating and delivering a message or a signal.
outliving_reference_t< T > outliving_mutable(T &r)
Make outliving_reference wrapper for mutable reference.
A message with value of some quantity.
Definition messages.hpp:60
Information about one work thread activity.
Definition messages.hpp:108