SObjectizer 5.8
Loading...
Searching...
No Matches
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 thread pool dispatcher.
8 *
9 * \since v.5.4.0
10 */
11
12#include <so_5/disp/thread_pool/pub.hpp>
13
14#include <so_5/disp/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
29namespace thread_pool
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 dispatcher binder for %thread_pool dispatcher.
80 *
81 * \since v.5.6.0
82 */
83class actual_binder_t final : public disp_binder_t
84 {
85 //! Dispatcher to be used.
86 actual_dispatcher_iface_shptr_t m_disp;
87 //! Binding parameters.
89
90 public :
92 actual_dispatcher_iface_shptr_t disp,
93 bind_params_t params ) noexcept
94 : m_disp{ std::move(disp) }
95 , m_params{ params }
96 {}
97
98 void
100 agent_t & agent ) override
101 {
103 }
104
105 void
107 agent_t & agent ) noexcept override
108 {
110 }
111
112 void
114 agent_t & agent ) noexcept override
115 {
116 auto queue = m_disp->query_resources_for_agent( agent );
117 agent.so_bind_to_dispatcher( *queue );
118 }
119
120 void
122 agent_t & agent ) noexcept override
123 {
124 m_disp->unbind_agent( agent );
125 }
126 };
127
128//
129// actual_dispatcher_implementation_t
130//
131/*!
132 * \brief Actual implementation of binder for %thread_pool dispatcher.
133 *
134 * \since v.5.6.0
135 */
136template< typename Work_Thread >
137class actual_dispatcher_implementation_t final
139 {
140 //! Real dispatcher.
141 dispatcher_template_t< Work_Thread > m_impl;
142
143 public :
145 //! SObjectizer Environment to work in.
147 //! Base part of data sources names.
148 const std::string_view name_base,
149 //! Dispatcher's parameters.
150 disp_params_t params )
151 : m_impl{
152 env.get(),
153 params,
154 name_base,
155 params.thread_count(),
156 params.queue_params()
157 }
158 {
159 m_impl.start( env.get() );
160 }
161
163 {
164 m_impl.shutdown_then_wait();
165 }
166
167 [[nodiscard]]
168 disp_binder_shptr_t
169 binder( bind_params_t params ) override
170 {
171 return std::make_shared< actual_binder_t >(
172 this->shared_from_this(),
173 params );
174 }
175
176 void
178 agent_t & agent,
179 const bind_params_t & params ) override
180 {
181 m_impl.preallocate_resources_for_agent( agent, params );
182 }
183
184 void
186 agent_t & agent ) noexcept override
187 {
188 m_impl.undo_preallocation_for_agent( agent );
189 }
190
192 query_resources_for_agent( agent_t & agent ) noexcept override
193 {
194 return m_impl.query_resources_for_agent( agent );
195 }
196
197 void
198 unbind_agent( agent_t & agent ) noexcept override
199 {
200 m_impl.unbind_agent( agent );
201 }
202 };
203
204//
205// dispatcher_handle_maker_t
206//
208 {
209 public :
211 make( actual_dispatcher_iface_shptr_t disp ) noexcept
212 {
213 return { std::move( disp ) };
214 }
215 };
216
217} /* namespace impl */
218
219namespace
220{
221
222using namespace so_5::disp::thread_pool::impl;
223
224/*!
225 * \brief Sets the thread count to default value if used do not
226 * specify actual thread count.
227 *
228 * \since 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 impl::dispatcher_queue_t
256 >
257 >;
258
259 using dispatcher_with_activity_tracking_t =
260 impl::actual_dispatcher_implementation_t<
261 impl::work_thread_with_activity_tracking_t<
262 impl::dispatcher_queue_t
263 >
264 >;
265
268 dispatcher_no_activity_tracking_t,
269 dispatcher_with_activity_tracking_t >(
271 data_sources_name_base,
272 std::move(params) );
273
274 return impl::dispatcher_handle_maker_t::make( std::move(binder) );
275 }
276
277} /* namespace thread_pool */
278
279} /* namespace disp */
280
281} /* 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 thread_pool dispatcher.
Alias for namespace with traits of event queue.
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.
std::size_t thread_count() const
Getter for thread count.
A handle for thread_pool dispatcher.
dispatcher_handle_t(impl::basic_dispatcher_iface_shptr_t dispatcher) noexcept
void unbind(agent_t &agent) noexcept override
Unbind agent from dispatcher.
void undo_preallocation(agent_t &agent) noexcept override
Undo resources allocation.
void bind(agent_t &agent) noexcept override
Bind agent to dispatcher.
void preallocate_resources(agent_t &agent) override
Allocate resources in dispatcher for new agent.
const bind_params_t m_params
Binding parameters.
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.
An actual interface of thread-pool dispatcher.
virtual void undo_preallocation_for_agent(agent_t &agent) noexcept=0
Undo preallocation of resources for a new agent.
virtual event_queue_t * query_resources_for_agent(agent_t &agent) noexcept=0
Get resources allocated for an agent.
virtual void preallocate_resources_for_agent(agent_t &agent, const bind_params_t &params)=0
Preallocate all necessary resources for a new agent.
virtual void unbind_agent(agent_t &agent) noexcept=0
Unbind agent from the dispatcher.
void undo_preallocation_for_agent(agent_t &agent) noexcept override
Undo preallocation of resources for a new agent.
dispatcher_template_t< Work_Thread > m_impl
Real dispatcher.
disp_binder_shptr_t binder(bind_params_t params) override
void preallocate_resources_for_agent(agent_t &agent, const bind_params_t &params) override
Preallocate all necessary resources for a new agent.
void unbind_agent(agent_t &agent) noexcept override
Unbind agent from the dispatcher.
actual_dispatcher_implementation_t(outliving_reference_t< environment_t > env, const std::string_view name_base, disp_params_t params)
event_queue_t * query_resources_for_agent(agent_t &agent) noexcept override
Get resources allocated for an agent.
The very basic interface of 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
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...
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 thread pool dispatcher.
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 thread_pool dispatcher.
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.