SObjectizer  5.8
Loading...
Searching...
No Matches
reuse/work_thread/work_thread.hpp
Go to the documentation of this file.
1/*
2 SObjectizer 5.
3*/
4
5/*!
6 \file
7 \brief Working thread for dispatchers.
8*/
9
10#pragma once
11
12#include <atomic>
13#include <condition_variable>
14#include <deque>
15#include <memory>
16#include <mutex>
17#include <thread>
18#include <utility>
19
20#include <so_5/declspec.hpp>
21#include <so_5/current_thread_id.hpp>
22
23#include <so_5/event_queue.hpp>
24
25#include <so_5/disp/mpsc_queue_traits/pub.hpp>
26
27#include <so_5/stats/work_thread_activity.hpp>
28#include <so_5/stats/impl/activity_tracking.hpp>
29
30#include <so_5/impl/thread_join_stuff.hpp>
31
32#include <so_5/details/rollback_on_exception.hpp>
33#include <so_5/details/invoke_noexcept_code.hpp>
34
35namespace so_5
36{
37
38namespace disp
39{
40
41namespace reuse
42{
43
44namespace work_thread
45{
46
47//! Typedef for demand's container.
49
50namespace queue_traits = so_5::disp::mpsc_queue_traits;
51
52//! Typedef for atomic demands counter.
53/*!
54 * \since v.5.5.7
55 */
57
58/*!
59 * \brief Type for result of demand extraction.
60 */
62{
63 //! Demand has been extracted.
65 //! Demand has not been extracted because of shutdown.
66 shutting_down = 2,
67 //! Demand has not been extracted because the demand queue is empty.
68 no_demands = 3
69};
70
72{
73
74/*!
75 * \brief Common data for all implementations of demand_queue.
76 *
77 * \since v.5.5.18
78 */
80{
81 //! Demand queue.
83
84 //! \name Objects for the thread safety.
85 //! \{
87 //! \}
88
89 //! Service flag.
90 /*!
91 true -- shall do the service, methods push/pop must work.
92 false -- the service is stopped or will be stopped.
93 */
94 bool m_in_service{ false };
95
96 //! Initializing constructor.
98 //! Lock object to be used by queue.
99 queue_traits::lock_unique_ptr_t lock )
100 : m_lock( std::move(lock) )
101 {}
102
104 {
105 m_demands.clear();
106 }
107};
108
109/*!
110 * \brief A part of demand queue implementation for the case
111 * when activity tracking is not used.
112 */
114{
115public :
117 queue_traits::lock_unique_ptr_t lock )
119 {}
120
121protected :
122 void
124
125 void
127};
128
129/*!
130 * \brief A part of demand queue implementation for the case
131 * when activity tracking is used.
132 */
134{
135public :
137 queue_traits::lock_unique_ptr_t lock )
140 {}
141
144 {
145 return m_waiting_stats.take_stats();
146 }
147
148protected :
149 void
151 {
152 m_waiting_stats.start();
153 }
154
155 void
157 {
158 m_waiting_stats.stop();
159 }
160
161private :
167};
168
169//
170// demand_queue_template_t
171//
172
173//! Implementation of demand_queue in form of a template.
174/*!
175 demand_queue_t has shutdown flag inside.
176
177 demand_queue_t is thread safe and is intended to be used by
178 several concurrent threads.
179*/
180template< typename Impl >
181class queue_template_t final
182 : public event_queue_t
183 , public Impl
184{
185public:
187 //! Lock object to be used by queue.
188 queue_traits::lock_unique_ptr_t lock )
189 : Impl( std::move(lock) )
190 {}
191
192 /*!
193 * \name Implementation of event_queue interface.
194 * \{
195 */
196 virtual void
197 push( execution_demand_t demand ) override
198 {
199 queue_traits::lock_guard_t guard{ *(this->m_lock) };
200
201 if( this->m_in_service )
202 {
203 const bool demands_empty_before_service = this->m_demands.empty();
204
205 this->m_demands.push_back( std::move( demand ) );
206
207 if( demands_empty_before_service )
208 {
209 // May be someone is waiting...
210 // It should be informed about new demands.
211 guard.notify_one();
212 }
213 }
214 }
215
216 /*!
217 * \note
218 * Delegates the work to the push() method.
219 */
220 void
222 {
223 this->push( std::move(demand) );
224 }
225
226 /*!
227 * \note
228 * Delegates the work to the push() method.
229 *
230 * \attention
231 * Terminates the whole application if the push() throws.
232 */
233 void
234 push_evt_finish( execution_demand_t demand ) noexcept override
235 {
236 this->push( std::move(demand) );
237 }
238 /*!
239 * \}
240 */
241
242 //! Try to extract demands from the queue.
243 /*!
244 If there is no demands in queue then current thread
245 will sleep until:
246 - the new demand is put in the queue;
247 - a shutdown signal.
248
249 \note Since v.5.5.7 this method also updates external demands
250 counter. This update is performed under queue's lock.
251 It should prevent errors when run-time monitor can get wrong
252 quantity of demands.
253 */
256 /*! Receiver for extracted demands. */
257 demand_container_t & demands,
258 /*! External demands counter to be updated. */
259 demands_counter_t & external_counter )
260 {
261 queue_traits::unique_lock_t lock{ *(this->m_lock) };
262 while( true )
263 {
264 if( this->m_in_service && !this->m_demands.empty() )
265 {
266 swap( demands, this->m_demands );
267
268 // It's time to update external counter.
269 external_counter.store( demands.size(), std::memory_order_release );
270
271 break;
272 }
273 else if( !this->m_in_service )
274 return extraction_result_t::shutting_down;
275 else
276 {
277 // Queue is empty. We should wait for a demand or
278 // a shutdown signal.
279
280 // Since v.5.5.18 we must take care about activity tracking.
281 this->wait_started();
282
283 lock.wait_for_notify();
284
285 this->wait_finished();
286 }
287 }
288
289 return extraction_result_t::demand_extracted;
290 }
291
292 //! Start demands processing.
293 void
295 {
296 queue_traits::lock_guard_t lock{ *(this->m_lock) };
297
298 this->m_in_service = true;
299 }
300
301 //! Stop demands processing.
302 void
304 {
305 queue_traits::lock_guard_t lock{ *(this->m_lock) };
306
307 this->m_in_service = false;
308 // If the demands queue is empty then someone is waiting
309 // for new demands inside pop().
310 if( this->m_demands.empty() )
311 lock.notify_one();
312 }
313
314 //! Clear demands queue.
315 void
317 {
318 queue_traits::lock_guard_t lock{ *(this->m_lock) };
319
320 this->m_demands.clear();
321 }
322
323 /*!
324 * \brief Get the count of demands in the queue.
325 *
326 * \note Since v.5.5.7 this method also uses external demands
327 * counter. Addition of demands quantity inside demands queue and
328 * the value of external counter is performed under the queue lock.
329 *
330 * \since v.5.5.4
331 */
332 std::size_t
334 {
335 queue_traits::lock_guard_t lock{ *(this->m_lock) };
336
337 return this->m_demands.size()
338 + external_counter.load( std::memory_order_acquire );
339 }
340};
341
342} /* namespace demand_queue_details */
343
344/*!
345 * \brief An alias for demand_queue without activity tracking.
346 *
347 * \since v.5.5.18
348 */
349using demand_queue_no_activity_tracking_t =
350 demand_queue_details::queue_template_t<
352
353/*!
354 * \brief An alias for demand_queue with activity tracking.
355 *
356 * \since v.5.5.18
357 */
358using demand_queue_with_activity_tracking_t =
359 demand_queue_details::queue_template_t<
361
362namespace details
363{
364
365//! Thread status flag.
366enum class status_t : int
367{
368 //! 0 - thread execution should be stopped.
369 stopped = 0,
370 //! 1 - thread execution should be continued.
371 working = 1
372};
373
374/*!
375 * \brief Common data for all work thread implementations.
376 *
377 * \since v.5.5.18
378 */
379template< typename Demand_Queue >
381{
382 //! Working thread.
384
385 //! Thread status flag.
387
388 //! Demands queue.
389 Demand_Queue m_queue;
390
391 /*!
392 * \brief ID of working thread.
393 *
394 * \attention Receive the value only after start of thread body.
395 */
397
398 /*!
399 * \brief A counter for calculating count of demands in
400 * the queue.
401 *
402 * \note Will be used for run-time monitoring.
403 */
405
407 work_thread_holder_t thread_holder,
408 queue_traits::lock_factory_t queue_lock_factory )
411 {}
412};
413
414/*!
415 * \brief Part of implementation of work thread without activity tracking.
416 *
417 * \since v.5.5.18
418 */
421{
422public :
424 work_thread_holder_t thread_holder,
425 queue_traits::lock_factory_t queue_lock_factory )
429 }
430 {}
431
432protected :
433 //! Main method for serving block of demands.
434 void
436 //! Bunch of demands to be processed.
437 demand_container_t & demands )
438 {
439 while( !demands.empty() )
440 {
441 auto & demand = demands.front();
442
443 demand.call_handler( this->m_thread_id );
444
445 demands.pop_front();
446 --(this->m_demands_count);
447 }
448 }
449};
450
451/*!
452 * \brief Part of implementation of work thread with activity tracking.
453 *
454 * \since
455 * v.5.5.18
456 */
459{
460 using activity_tracking_traits = so_5::stats::activity_tracking_stuff::traits;
461
462public :
464 work_thread_holder_t thread_holder,
465 queue_traits::lock_factory_t queue_lock_factory )
469 }
470 {}
471
472 /*!
473 * \brief Get the activity stats.
474 */
477 {
478 namespace stats = so_5::stats;
479
480 stats::work_thread_activity_stats_t result;
481 bool is_working{ false };
482 stats::clock_type_t::time_point working_started_at;
483
484 {
485 std::lock_guard< activity_tracking_traits::lock_t > lock{ m_stats_lock };
486 result.m_working_stats = m_activity_stats;
487
488 // Special care to the current activity (if exists).
489 if( m_activity_started_at )
490 {
491 is_working = true;
492 working_started_at = *m_activity_started_at;
493 }
494 }
495
496 if( is_working )
497 {
498 stats::details::update_stats_from_current_time(
499 result.m_working_stats,
500 working_started_at );
501 }
502
503 result.m_waiting_stats = m_queue.take_activity_stats();
504
505 return result;
506 }
507
508protected :
509 //! Main method for serving block of demands.
510 void
512 //! Bunch of demands to be processed.
513 demand_container_t & demands )
514 {
515 auto activity_started_at = so_5::stats::clock_type_t::now();
516
517 {
518 std::lock_guard< activity_tracking_traits::lock_t > lock{ m_stats_lock };
519 m_activity_started_at = &activity_started_at;
520 m_activity_stats.m_count += 1;
521 }
522
523 while( !demands.empty() )
524 {
525 auto & demand = demands.front();
526
527 demand.call_handler( m_thread_id );
528
529 const auto activity_finished_at = so_5::stats::clock_type_t::now();
530
531 demands.pop_front();
532 --m_demands_count;
533
534 {
535 std::lock_guard< activity_tracking_traits::lock_t > lock{ m_stats_lock };
536 so_5::stats::details::update_stats_from_duration(
537 m_activity_stats,
538 activity_finished_at - activity_started_at );
539
540 if( demands.empty() )
541 m_activity_started_at = nullptr;
542 else
543 {
544 activity_started_at = activity_finished_at;
545 m_activity_stats.m_count += 1;
546 }
547 }
548 }
549 }
550
551private :
552 /*!
553 * \brief Lock for manipulation of activity stats.
554 */
555 activity_tracking_traits::lock_t m_stats_lock;
556
557 /*!
558 * \brief Pointer to time_point object related to the current activity.
559 *
560 * Value nullptr means that there is no any event running on
561 * the work thread.
562 */
564
565 /*!
566 * \brief Activity statistics.
567 */
569};
570
571/*!
572 * \brief Implementation of work thread in form of template.
573 *
574 * \since
575 * v.5.5.18
576 */
577template< typename Impl >
578class work_thread_template_t final : public Impl
579{
580public :
582 //! Holder of the work thread.
583 work_thread_holder_t thread_holder,
584 //! Factory for creation of lock object for demand queue.
585 queue_traits::lock_factory_t queue_lock_factory )
586 : Impl( std::move(thread_holder), std::move(queue_lock_factory) )
587 {}
588
589 //! Start the working thread.
590 void
592 {
593 // NOTE: those actions have to be rollbacked if work thread can't
594 // be started.
595 this->m_queue.start_service();
596 this->m_status = status_t::working;
597
598 so_5::details::do_with_rollback_on_exception(
599 [this]() {
600 this->m_thread_holder.unchecked_get().start(
601 [this]() { this->body(); } );
602 },
603 [this]() {
604 // We can't recover if that code throws.
605 so_5::details::invoke_noexcept_code( [this]() {
606 this->m_status = status_t::stopped;
607 this->m_queue.stop_service();
608 } );
609 } );
610 }
611
612 //! Send the shutdown signal to the working thread.
613 void
615 {
616 this->m_status = status_t::stopped;
617 this->m_queue.stop_service();
618 }
619
620 //! Wait the full stop of the working thread.
621 /*!
622 * All non-processed demands from the queue will be destroyed
623 * after a stop of the working thread.
624 */
625 void
627 {
628 so_5::impl::ensure_join_from_different_thread( this->m_thread_id );
629 this->m_thread_holder.unchecked_get().join();
630 this->m_queue.clear();
631 }
632
633 /*!
634 * \brief Get the underlying event_queue object.
635 */
638 {
639 return this->m_queue;
640 }
641
642 /*!
643 * \brief Get a binding information for an agent.
644 */
647 {
648 return &this->event_queue();
649 }
650
651 /*!
652 * \brief Get the count of demands in the queue.
653 */
654 std::size_t
656 {
657 return this->m_queue.demands_count( this->m_demands_count );
658 }
659
660 /*!
661 * \brief Get ID of work thread.
662 *
663 * \note This method returns correct value only after start
664 * of the thread.
665 *
666 * \since
667 * v.5.5.18
668 */
670 thread_id() const
671 {
672 return this->m_thread_id;
673 }
674
675private :
676 //! Main thread body.
677 void
679 {
680 // Store current thread ID to attribute to avoid thread ID
681 // request on every event execution.
682 this->m_thread_id = so_5::query_current_thread_id();
683
684 // Local demands queue.
685 demand_container_t demands;
686
687 auto result = extraction_result_t::no_demands;
688
689 while( status_t::working == this->m_status )
690 {
691 // If the local queue is empty then we should try
692 // to get new demands.
693 if( demands.empty() )
694 result = this->m_queue.pop( demands, this->m_demands_count );
695
696 // Serve demands if any.
697 if( extraction_result_t::demand_extracted == result )
698 this->serve_demands_block( demands );
699 }
700 }
701};
702
703} /* namespace details */
704
705//
706// work_thread_t
707//
708
709/*!
710 * \brief Type of work thread without activity tracking.
711 */
712using work_thread_no_activity_tracking_t =
713 details::work_thread_template_t<
715
716//
717// work_thread_with_activity_tracking_t
718//
719
720//! Working thread with activity tracking.
721/*!
722 * \since
723 * v.5.5.18
724 */
725using work_thread_with_activity_tracking_t =
726 details::work_thread_template_t<
728
729} /* namespace work_thread */
730
731} /* namespace reuse */
732
733} /* namespace disp */
734
735} /* 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.
A part of demand queue implementation for the case when activity tracking is not used.
std::size_t demands_count(const demands_counter_t &external_counter)
Get the count of demands in the queue.
virtual void push(execution_demand_t demand) override
Enqueue new event to the queue.
extraction_result_t pop(demand_container_t &demands, demands_counter_t &external_counter)
Try to extract demands from the queue.
A part of demand queue implementation for the case when activity tracking is used.
so_5::stats::activity_tracking_stuff::stats_collector_t< so_5::stats::activity_tracking_stuff::external_lock< queue_traits::lock_t, so_5::stats::activity_tracking_stuff::no_lock_at_start_stop_policy > > m_waiting_stats
Part of implementation of work thread with activity tracking.
so_5::stats::work_thread_activity_stats_t take_activity_stats()
Get the activity stats.
const so_5::stats::clock_type_t::time_point * m_activity_started_at
Pointer to time_point object related to the current activity.
activity_tracking_impl_t(work_thread_holder_t thread_holder, queue_traits::lock_factory_t queue_lock_factory)
activity_tracking_traits::lock_t m_stats_lock
Lock for manipulation of activity stats.
void serve_demands_block(demand_container_t &demands)
Main method for serving block of demands.
Part of implementation of work thread without activity tracking.
void serve_demands_block(demand_container_t &demands)
Main method for serving block of demands.
no_activity_tracking_impl_t(work_thread_holder_t thread_holder, queue_traits::lock_factory_t queue_lock_factory)
event_queue_t & event_queue()
Get the underlying event_queue object.
work_thread_template_t(work_thread_holder_t thread_holder, queue_traits::lock_factory_t queue_lock_factory)
event_queue_t * get_agent_binding()
Get a binding information for an agent.
so_5::current_thread_id_t thread_id() const
Get ID of work thread.
Mixin that holds optional work thread factory.
An analog of unique_ptr for abstract_work_thread.
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
Base for the case of externals stats lock.
Base for the case of internal stats lock.
so_5::stats::activity_stats_t m_work_activity
A statistics for work activity.
void start_if_not_started()
A helper method for safe start if start method hasn't been called yet.
so_5::stats::clock_type_t::time_point m_work_started_at
A time point when current activity started.
bool m_is_in_working
A flag for indicating work activity.
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
bool operator<(const prefix_t &o) const noexcept
Is less than?
Definition prefix.hpp:123
constexpr prefix_t() noexcept
Default constructor creates empty prefix.
Definition prefix.hpp:40
constexpr bool empty() const noexcept
Is prefix empty?
Definition prefix.hpp:99
static constexpr const std::size_t max_buffer_size
Max size of buffer for prefix value (including 0-symbol at the end).
Definition prefix.hpp:37
constexpr std::string_view as_string_view() const noexcept(noexcept(std::string_view{std::declval< const char * >()}))
Access to prefix value as string_view.
Definition prefix.hpp:91
constexpr const char * c_str() const noexcept
Access to prefix value.
Definition prefix.hpp:80
constexpr prefix_t(const char *value) noexcept
Initializing constructor.
Definition prefix.hpp:54
bool operator!=(const prefix_t &o) const noexcept
Is not equal?
Definition prefix.hpp:115
static constexpr const std::size_t max_length
Max length of prefix (not including 0-symbol at the end).
Definition prefix.hpp:35
char m_value[max_buffer_size]
Actual value.
Definition prefix.hpp:130
prefix_t(const std::string &value) noexcept(noexcept(value.c_str()))
Initializing constructor.
Definition prefix.hpp:73
bool operator==(const prefix_t &o) const noexcept
Is equal?
Definition prefix.hpp:107
An interface of data source.
A type for representing the suffix of data_source name.
Definition prefix.hpp:156
constexpr bool operator<(const suffix_t &o) const noexcept
Compares suffixes by pointer value.
Definition prefix.hpp:203
constexpr bool operator==(const suffix_t &o) const noexcept
Compares suffixes by pointer values.
Definition prefix.hpp:187
constexpr const char * c_str() const noexcept
Access to suffix value.
Definition prefix.hpp:168
const char * m_value
Actual value.
Definition prefix.hpp:210
constexpr std::string_view as_string_view() const noexcept(noexcept(std::string_view{std::declval< const char * >()}))
Access to prefix value as string_view.
Definition prefix.hpp:179
constexpr suffix_t(const char *value) noexcept
Initializing constructor.
Definition prefix.hpp:159
constexpr bool operator!=(const suffix_t &o) const noexcept
Compares suffixes by pointer value.
Definition prefix.hpp:195
#define SO_5_FUNC
Definition declspec.hpp:48
Helpers for manipulation with standard C++ I/O streams.
Some reusable and low-level classes/functions which can be used in public header files.
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.
@ working
1 - thread execution should be continued.
@ stopped
0 - thread execution should be stopped.
Implemetation details of dispatcher's working thread.
extraction_result_t
Type for result of demand extraction.
@ shutting_down
Demand has not been extracted because of shutdown.
@ no_demands
Demand has not been extracted because the demand queue is empty.
Reusable components for dispatchers.
abstract_work_thread_factory_shptr_t actual_work_thread_factory_to_use(const work_thread_factory_mixin_t< Params > &params, const environment_t &env) noexcept
Helper to detect actual work thread factory to be used.
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.
void modify_disp_params(so_5::environment_t &env, Disp_Params_Type &params)
Helper functions to adjust some dispatcher parameters with respect to settings from environment.
so_5::stats::prefix_t make_disp_working_thread_prefix(const so_5::stats::prefix_t &disp_prefix, std::size_t thread_number)
Create prefix for dispatcher's working thread data source.
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.
std::unique_ptr< Common_Disp_Iface_Type > create_appropriate_disp(outliving_reference_t< Env > env, const std::string_view name_base, Disp_Params disp_params, Args &&...args)
Helper function for creation of dispatcher with respect to activity tracking flag in dispatcher param...
void update_stats_from_current_time(activity_stats_t &value_to_update, clock_type_t::time_point activity_started_at)
Helper function for simplification of current stats update.
void update_stats_from_duration(activity_stats_t &value_to_update, clock_type_t::duration last_duration)
Helper function for simplification of current stats update.
duration_t calc_avg_time(std::uint_fast64_t count, duration_t previous, duration_t last)
A function for calculating average value.
All stuff related to run-time monitoring and statistics.
std::ostream & operator<<(std::ostream &to, const prefix_t &what)
Just a helper operator.
Definition prefix.hpp:139
std::ostream & operator<<(std::ostream &to, const suffix_t &what)
Just a helper operator.
Definition prefix.hpp:219
std::ostream & operator<<(std::ostream &to, const activity_stats_t &what)
Helper for printing value of activity_stats.
Private part of message limit implementation.
Definition agent.cpp:33
common_data_t(queue_traits::lock_unique_ptr_t lock)
Initializing constructor.
common_data_t(work_thread_holder_t thread_holder, queue_traits::lock_factory_t queue_lock_factory)
demands_counter_t m_demands_count
A counter for calculating count of demands in the queue.
A description of event execution demand.
Statistics of some activity.
std::optional< duration_t > m_current_activity_time
duration_t m_avg_time
Average time for one event.
duration_t m_total_time
Total time spent for events in that period of time.
std::uint_fast64_t m_count
Count of events in that period of time.
Default locking policy for stats_collector_t.
An analog of std::lock_guard but without actual locking actions.
A special class for cases where lock is not needed at all.
Various traits of activity tracking implementation.
activity_stats_t m_working_stats
Stats for processed events.
activity_stats_t m_waiting_stats
Stats for waiting periods.