SObjectizer 5.8
Loading...
Searching...
No Matches
adv_thread_pool/pub.cpp
Go to the documentation of this file.
1/*
2 * SObjectizer-5
3 */
4
5/*!
6 * \file
7 * \brief Public interface of advanced thread pool dispatcher.
8 * \since
9 * v.5.4.0
10 */
11
12#include <so_5/disp/adv_thread_pool/pub.hpp>
13
14#include <so_5/disp/adv_thread_pool/impl/disp.hpp>
15
16#include <so_5/disp/reuse/make_actual_dispatcher.hpp>
17
18#include <so_5/ret_code.hpp>
19
20#include <so_5/disp_binder.hpp>
21#include <so_5/environment.hpp>
22
23namespace so_5
24{
25
26namespace disp
27{
28
30{
31
32namespace impl
33{
34
35//
36// actual_dispatcher_iface_t
37//
38/*!
39 * \brief An actual interface of thread-pool dispatcher.
40 *
41 * This interface defines a set of methods necessary for binder.
42 *
43 * \since v.5.6.0
44 */
46 {
47 public :
48 //! Preallocate all necessary resources for a new agent.
49 virtual void
51 agent_t & agent,
52 const bind_params_t & params ) = 0;
53
54 //! Undo preallocation of resources for a new agent.
55 virtual void
57 agent_t & agent ) noexcept = 0;
58
59 //! Get resources allocated for an agent.
60 [[nodiscard]]
61 virtual event_queue_t *
62 query_resources_for_agent( agent_t & agent ) noexcept = 0;
63
64 //! Unbind agent from the dispatcher.
65 virtual void
66 unbind_agent( agent_t & agent ) noexcept = 0;
67 };
68
69//
70// actual_dispatcher_iface_shptr_t
71//
72using actual_dispatcher_iface_shptr_t =
73 std::shared_ptr< actual_dispatcher_iface_t >;
74
75//
76// actual_binder_t
77//
78/*!
79 * \brief Actual implementation of binder for %adv_thread_pool dispatcher.
80 *
81 * \since
82 * v.5.6.0
83 */
84class actual_binder_t final : public disp_binder_t
85 {
86 //! Dispatcher to be used.
87 actual_dispatcher_iface_shptr_t m_disp;
88 //! Binding parameters.
90
91 public :
93 actual_dispatcher_iface_shptr_t disp,
94 bind_params_t params ) noexcept
95 : m_disp{ std::move(disp) }
96 , m_params{ params }
97 {}
98
99 void
101 agent_t & agent ) override
102 {
104 }
105
106 void
108 agent_t & agent ) noexcept override
109 {
111 }
112
113 void
115 agent_t & agent ) noexcept override
116 {
117 auto queue = m_disp->query_resources_for_agent( agent );
118 agent.so_bind_to_dispatcher( *queue );
119 }
120
121 void
123 agent_t & agent ) noexcept override
124 {
125 m_disp->unbind_agent( agent );
126 }
127 };
128
129//
130// actual_dispatcher_implementation_t
131//
132/*!
133 * \brief Actual implementation of %adv_thread_pool dispatcher.
134 *
135 * \since
136 * v.5.6.0
137 */
138template< typename Work_Thread >
139class actual_dispatcher_implementation_t final
141 {
142 //! Real dispatcher.
143 dispatcher_template_t< Work_Thread > m_impl;
144
145 public :
147 //! SObjectizer Environment to work in.
149 //! Base part of data sources names.
150 const std::string_view name_base,
151 //! Dispatcher's parameters.
152 disp_params_t params )
153 : m_impl{
154 env.get(),
155 params,
156 name_base,
157 params.thread_count(),
158 params.queue_params()
159 }
160 {
161 m_impl.start( env.get() );
162 }
163
165 {
166 m_impl.shutdown_then_wait();
167 }
168
169 disp_binder_shptr_t
170 binder( bind_params_t params ) override
171 {
172 return std::make_shared< actual_binder_t >(
173 this->shared_from_this(),
174 params );
175 }
176
177 void
179 agent_t & agent,
180 const bind_params_t & params ) override
181 {
182 m_impl.preallocate_resources_for_agent( agent, params );
183 }
184
185 void
187 agent_t & agent ) noexcept override
188 {
189 m_impl.undo_preallocation_for_agent( agent );
190 }
191
193 query_resources_for_agent( agent_t & agent ) noexcept override
194 {
195 return m_impl.query_resources_for_agent( agent );
196 }
197
198 void
199 unbind_agent( agent_t & agent ) noexcept override
200 {
201 m_impl.unbind_agent( agent );
202 }
203 };
204
205//
206// dispatcher_handle_maker_t
207//
209 {
210 public :
212 make( actual_dispatcher_iface_shptr_t disp ) noexcept
213 {
214 return { std::move( disp ) };
215 }
216 };
217
218} /* namespace impl */
219
220namespace
221{
222
223/*!
224 * \brief Sets the thread count to default value if used do not
225 * specify actual thread count.
226 *
227 * \since
228 * v.5.5.11
229 */
230inline void
236
237} /* namespace anonymous */
238
239//
240// make_dispatcher
241//
244 environment_t & env,
245 const std::string_view data_sources_name_base,
246 disp_params_t params )
247 {
248 using namespace so_5::disp::reuse;
249
250 adjust_thread_count( params );
251
252 using dispatcher_no_activity_tracking_t =
253 impl::actual_dispatcher_implementation_t<
254 impl::work_thread_no_activity_tracking_t >;
255
256 using dispatcher_with_activity_tracking_t =
257 impl::actual_dispatcher_implementation_t<
258 impl::work_thread_with_activity_tracking_t >;
259
262 dispatcher_no_activity_tracking_t,
263 dispatcher_with_activity_tracking_t >(
265 data_sources_name_base,
266 std::move(params) );
267
268 return impl::dispatcher_handle_maker_t::make( std::move(binder) );
269 }
270
271} /* namespace adv_thread_pool */
272
273} /* namespace disp */
274
275} /* namespace so_5 */
A base class for agents.
Definition agent.hpp:673
void so_bind_to_dispatcher(event_queue_t &queue) noexcept
Binding agent to the dispatcher.
Definition agent.cpp:872
Parameters for binding agents to adv_thread_pool dispatcher.
Alias for namespace with traits of event queue.
std::size_t thread_count() const
Getter for thread count.
disp_params_t & thread_count(std::size_t count)
Setter for thread count.
const queue_traits::queue_params_t & queue_params() const
Getter for queue parameters.
A handle for adv_thread_pool dispatcher.
dispatcher_handle_t(impl::basic_dispatcher_iface_shptr_t dispatcher) noexcept
void bind(agent_t &agent) noexcept override
Bind agent to dispatcher.
const bind_params_t m_params
Binding parameters.
void unbind(agent_t &agent) noexcept override
Unbind agent from dispatcher.
actual_binder_t(actual_dispatcher_iface_shptr_t disp, bind_params_t params) noexcept
actual_dispatcher_iface_shptr_t m_disp
Dispatcher to be used.
void preallocate_resources(agent_t &agent) override
Allocate resources in dispatcher for new agent.
void undo_preallocation(agent_t &agent) noexcept override
Undo resources allocation.
virtual event_queue_t * query_resources_for_agent(agent_t &agent) noexcept=0
Get resources allocated for an agent.
virtual void undo_preallocation_for_agent(agent_t &agent) noexcept=0
Undo preallocation of resources for a new agent.
virtual void unbind_agent(agent_t &agent) noexcept=0
Unbind agent from the dispatcher.
virtual void preallocate_resources_for_agent(agent_t &agent, const bind_params_t &params)=0
Preallocate all necessary resources for a new agent.
event_queue_t * query_resources_for_agent(agent_t &agent) noexcept override
Get resources allocated for an agent.
void unbind_agent(agent_t &agent) noexcept override
Unbind agent from the dispatcher.
void undo_preallocation_for_agent(agent_t &agent) noexcept override
Undo preallocation of resources for a new agent.
actual_dispatcher_implementation_t(outliving_reference_t< environment_t > env, const std::string_view name_base, disp_params_t params)
dispatcher_template_t< Work_Thread > m_impl
Real dispatcher.
void preallocate_resources_for_agent(agent_t &agent, const bind_params_t &params) override
Preallocate all necessary resources for a new agent.
The very basic interface of adv_thread_pool dispatcher.
static dispatcher_handle_t make(actual_dispatcher_iface_shptr_t disp) noexcept
Interface for dispatcher binders.
SObjectizer Environment.
An interface of event queue for agent.
Helper class for indication of long-lived reference via its type.
Definition outliving.hpp:98
T & get() const noexcept
#define SO_5_FUNC
Definition declspec.hpp:48
void adjust_thread_count(disp_params_t &params)
Sets the thread count to default value if used do not specify actual thread count.
Internal implementation details of advanced thread pool dispatcher.
Advanced thread pool 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 adv_thread_pool dispatcher.
Reusable components for dispatchers.
std::size_t default_thread_pool_size()
A helper function for detecting default thread count for thread pool.
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.
Private part of message limit implementation.
Definition agent.cpp:33
outliving_reference_t< T > outliving_mutable(T &r)
Make outliving_reference wrapper for mutable reference.