2
3
6
7
8
13#include <condition_variable>
20#include <so_5/declspec.hpp>
21#include <so_5/current_thread_id.hpp>
23#include <so_5/event_queue.hpp>
25#include <so_5/disp/mpsc_queue_traits/pub.hpp>
27#include <so_5/stats/work_thread_activity.hpp>
28#include <so_5/stats/impl/activity_tracking.hpp>
30#include <so_5/impl/thread_join_stuff.hpp>
32#include <so_5/details/rollback_on_exception.hpp>
33#include <so_5/details/invoke_noexcept_code.hpp>
54
55
59
60
75
76
77
78
91
92
93
99 queue_traits::lock_unique_ptr_t lock )
110
111
112
117 queue_traits::lock_unique_ptr_t lock )
130
131
132
137 queue_traits::lock_unique_ptr_t lock )
145 return m_waiting_stats.take_stats();
152 m_waiting_stats.start();
158 m_waiting_stats.stop();
175
176
177
178
179
180template<
typename Impl >
181class queue_template_t
final
188 queue_traits::lock_unique_ptr_t lock )
189 : Impl( std::move(lock) )
193
194
195
199 queue_traits::lock_guard_t guard{ *(
this->m_lock) };
201 if(
this->m_in_service )
203 const bool demands_empty_before_service =
this->m_demands.empty();
205 this->m_demands.push_back( std::move( demand ) );
207 if( demands_empty_before_service )
217
218
219
223 this->push( std::move(demand) );
227
228
229
230
231
232
236 this->push( std::move(demand) );
239
240
244
245
246
247
248
249
250
251
252
253
257 demand_container_t & demands,
259 demands_counter_t & external_counter )
261 queue_traits::unique_lock_t lock{ *(
this->m_lock) };
264 if(
this->m_in_service && !
this->m_demands.empty() )
266 swap( demands,
this->m_demands );
269 external_counter.store( demands.size(), std::memory_order_release );
273 else if( !
this->m_in_service )
274 return extraction_result_t::shutting_down;
281 this->wait_started();
283 lock.wait_for_notify();
285 this->wait_finished();
289 return extraction_result_t::demand_extracted;
296 queue_traits::lock_guard_t lock{ *(
this->m_lock) };
298 this->m_in_service =
true;
305 queue_traits::lock_guard_t lock{ *(
this->m_lock) };
307 this->m_in_service =
false;
310 if(
this->m_demands.empty() )
318 queue_traits::lock_guard_t lock{ *(
this->m_lock) };
320 this->m_demands.clear();
324
325
326
327
328
329
330
331
335 queue_traits::lock_guard_t lock{ *(
this->m_lock) };
337 return this->m_demands.size()
338 + external_counter.load( std::memory_order_acquire );
345
346
347
348
349using demand_queue_no_activity_tracking_t =
354
355
356
357
358using demand_queue_with_activity_tracking_t =
375
376
377
378
379template<
typename Demand_Queue >
392
393
394
395
399
400
401
402
403
408 queue_traits::lock_factory_t queue_lock_factory )
415
416
417
418
425 queue_traits::lock_factory_t queue_lock_factory )
437 demand_container_t & demands )
439 while( !demands.empty() )
441 auto & demand = demands.front();
443 demand.call_handler(
this->m_thread_id );
446 --(
this->m_demands_count);
452
453
454
455
456
465 queue_traits::lock_factory_t queue_lock_factory )
473
474
481 bool is_working{
false };
482 stats::clock_type_t::time_point working_started_at;
485 std::lock_guard< activity_tracking_traits::lock_t > lock{ m_stats_lock };
486 result.m_working_stats = m_activity_stats;
489 if( m_activity_started_at )
492 working_started_at = *m_activity_started_at;
498 stats::details::update_stats_from_current_time(
499 result.m_working_stats,
500 working_started_at );
503 result.m_waiting_stats = m_queue.take_activity_stats();
513 demand_container_t & demands )
515 auto activity_started_at = so_5::stats::clock_type_t::now();
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;
523 while( !demands.empty() )
525 auto & demand = demands.front();
527 demand.call_handler( m_thread_id );
529 const auto activity_finished_at = so_5::stats::clock_type_t::now();
535 std::lock_guard< activity_tracking_traits::lock_t > lock{ m_stats_lock };
536 so_5::stats::details::update_stats_from_duration(
538 activity_finished_at - activity_started_at );
540 if( demands.empty() )
541 m_activity_started_at =
nullptr;
544 activity_started_at = activity_finished_at;
545 m_activity_stats.m_count += 1;
553
554
558
559
560
561
562
566
567
572
573
574
575
576
577template<
typename Impl >
578class work_thread_template_t
final :
public Impl
585 queue_traits::lock_factory_t queue_lock_factory )
586 : Impl( std::move(thread_holder), std::move(queue_lock_factory) )
595 this->m_queue.start_service();
596 this->m_status = status_t::working;
598 so_5::details::do_with_rollback_on_exception(
600 this->m_thread_holder.unchecked_get().start(
601 [
this]() {
this->body(); } );
605 so_5::details::invoke_noexcept_code( [
this]() {
606 this->m_status = status_t::stopped;
607 this->m_queue.stop_service();
616 this->m_status = status_t::stopped;
617 this->m_queue.stop_service();
622
623
624
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();
634
635
639 return this->m_queue;
643
644
648 return &
this->event_queue();
652
653
657 return this->m_queue.demands_count(
this->m_demands_count );
661
662
663
664
665
666
667
668
672 return this->m_thread_id;
682 this->m_thread_id = so_5::query_current_thread_id();
685 demand_container_t demands;
687 auto result = extraction_result_t::no_demands;
689 while( status_t::working ==
this->m_status )
693 if( demands.empty() )
694 result =
this->m_queue.pop( demands,
this->m_demands_count );
697 if( extraction_result_t::demand_extracted == result )
698 this->serve_demands_block( demands );
710
711
712using work_thread_no_activity_tracking_t =
713 details::work_thread_template_t<
722
723
724
725using work_thread_with_activity_tracking_t =
726 details::work_thread_template_t<
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
dispatcher_handle_t() noexcept=default
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 ~basic_dispatcher_iface_t() noexcept=default
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)
stats::prefix_t m_base_prefix
Basic prefix for data sources.
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.
std::mutex m_lock
This object lock.
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)
~dispatcher_template_t() noexcept override
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.
no_activity_tracking_impl_t(queue_traits::lock_unique_ptr_t lock)
std::size_t demands_count(const demands_counter_t &external_counter)
Get the count of demands in the queue.
void clear()
Clear demands queue.
void stop_service()
Stop demands processing.
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.
void push_evt_finish(execution_demand_t demand) noexcept override
void start_service()
Start demands processing.
void push_evt_start(execution_demand_t demand) override
queue_template_t(queue_traits::lock_unique_ptr_t lock)
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
so_5::stats::activity_stats_t take_activity_stats()
with_activity_tracking_impl_t(queue_traits::lock_unique_ptr_t lock)
Part of implementation of work thread with activity tracking.
so_5::stats::work_thread_activity_stats_t take_activity_stats()
Get the activity stats.
so_5::stats::activity_stats_t m_activity_stats
Activity statistics.
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)
void shutdown()
Send the shutdown signal to the working thread.
event_queue_t & event_queue()
Get the underlying event_queue object.
std::size_t demands_count()
Get the count of demands in the queue.
void body()
Main thread body.
void wait()
Wait the full stop of the working thread.
void start()
Start the working thread.
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 with thread activity tracking flag.
Mixin that holds optional work thread factory.
An analog of unique_ptr for abstract_work_thread.
Interface for dispatcher binders.
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.
Base for the case of externals stats lock.
external_lock(Lock_Type &lock)
Base for the case of internal stats lock.
Helper for collecting activity stats.
so_5::stats::activity_stats_t take_stats()
so_5::stats::activity_stats_t m_work_activity
A statistics for work activity.
Lock_Holder & lock_holder()
stats_collector_t(Args &&...args)
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.
bool operator<(const prefix_t &o) const noexcept
Is less than?
constexpr prefix_t() noexcept
Default constructor creates empty prefix.
constexpr bool empty() const noexcept
Is prefix empty?
static constexpr const std::size_t max_buffer_size
Max size of buffer for prefix value (including 0-symbol at the end).
constexpr std::string_view as_string_view() const noexcept(noexcept(std::string_view{std::declval< const char * >()}))
Access to prefix value as string_view.
constexpr const char * c_str() const noexcept
Access to prefix value.
constexpr prefix_t(const char *value) noexcept
Initializing constructor.
bool operator!=(const prefix_t &o) const noexcept
Is not equal?
static constexpr const std::size_t max_length
Max length of prefix (not including 0-symbol at the end).
char m_value[max_buffer_size]
Actual value.
prefix_t(const std::string &value) noexcept(noexcept(value.c_str()))
Initializing constructor.
bool operator==(const prefix_t &o) const noexcept
Is equal?
An interface of data source.
A type for representing the suffix of data_source name.
constexpr bool operator<(const suffix_t &o) const noexcept
Compares suffixes by pointer value.
constexpr bool operator==(const suffix_t &o) const noexcept
Compares suffixes by pointer values.
constexpr const char * c_str() const noexcept
Access to suffix value.
const char * m_value
Actual value.
constexpr std::string_view as_string_view() const noexcept(noexcept(std::string_view{std::declval< const char * >()}))
Access to prefix value as string_view.
constexpr suffix_t(const char *value) noexcept
Initializing constructor.
constexpr bool operator!=(const suffix_t &o) const noexcept
Compares suffixes by pointer value.
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.
status_t
Thread status flag.
@ 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.
@ demand_extracted
Demand has been extracted.
@ 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 > ¶ms, 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 > ¶ms, 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 ¶ms)
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...
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.
std::ostream & operator<<(std::ostream &to, const suffix_t &what)
Just a helper operator.
std::ostream & operator<<(std::ostream &to, const activity_stats_t &what)
Helper for printing value of activity_stats.
Private part of message limit implementation.
Auxiliary class for the working agent counting.
work_thread_shptr_t m_thread
Common data for all implementations of demand_queue.
queue_traits::lock_unique_ptr_t m_lock
demand_container_t m_demands
Demand queue.
common_data_t(queue_traits::lock_unique_ptr_t lock)
Initializing constructor.
bool m_in_service
Service flag.
Common data for all work thread implementations.
std::atomic< status_t > m_status
Thread status flag.
common_data_t(work_thread_holder_t thread_holder, queue_traits::lock_factory_t queue_lock_factory)
Demand_Queue m_queue
Demands queue.
demands_counter_t m_demands_count
A counter for calculating count of demands in the queue.
work_thread_holder_t m_thread_holder
Working thread.
so_5::current_thread_id_t m_thread_id
ID of working thread.
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 custom locking policy for stats_collector_t.
A special class for cases where lock is not needed at all.
Various traits of activity tracking implementation.
Stats for a work thread activity.
activity_stats_t m_working_stats
Stats for processed events.
activity_stats_t m_waiting_stats
Stats for waiting periods.