2
3
6
7
8
9
10
11
13#include <so_5/disp/nef_one_thread/pub.hpp>
15#include <so_5/disp/reuse/actual_work_thread_factory_to_use.hpp>
16#include <so_5/disp/reuse/data_source_prefix_helpers.hpp>
17#include <so_5/disp/reuse/make_actual_dispatcher.hpp>
19#include <so_5/impl/thread_join_stuff.hpp>
21#include <so_5/stats/repository.hpp>
22#include <so_5/stats/messages.hpp>
23#include <so_5/stats/std_names.hpp>
25#include <so_5/send_functions.hpp>
43
44
45
46
51
52
53
54
59
60
61
62
66
67
68
69
70
83
84
85
86
87using demand_unique_ptr_t = std::unique_ptr<
demand_t >;
95 queue_traits::lock_unique_ptr_t
m_lock;
108 std::atomic< std::size_t >
m_size = { 0 };
114 queue_traits::lock_unique_ptr_t lock )
115 :
m_lock{ std::move(lock) }
142 push( demand_unique_ptr_t tail_demand )
150 m_head = tail_demand.release();
167 demand_unique_ptr_t result;
189 return m_size.load( std::memory_order_acquire );
197 demand_unique_ptr_t to_be_deleted{
m_head };
199 to_be_deleted->
m_next =
nullptr;
203 return to_be_deleted;
211
212
213
214
225 demand_unique_ptr_t evt_start_demand,
226 demand_unique_ptr_t evt_finish_demand )
244 std::make_unique<
demand_t >( std::move(demand) )
);
274
275
276
277
288
289
290
296 queue_traits::lock_unique_ptr_t lock,
308
309
310
311
317 queue_traits::lock_unique_ptr_t lock,
322 std::move(thread_holder)
344
345
346
347
353 queue_traits::lock_unique_ptr_t lock,
358 std::move(thread_holder)
403
404
405
406
407template<
typename Work_Thread >
410 using base_type_t = Work_Thread;
416 queue_traits::lock_unique_ptr_t lock,
421 std::move(thread_holder)
428 this->m_thread_holder.unchecked_get().start( [
this]() {
body(); } );
434 this->m_queue.stop();
441 this->m_thread_holder.unchecked_get().join();
445 so_5::current_thread_id_t
448 return this->m_thread_id;
455 return this->m_queue;
464 bool demand_extracted =
true;
467 auto d =
this->pop_demand();
470 this->call_handler( d->m_execution_demand );
474 demand_extracted =
false;
476 while( demand_extracted );
483 this->wait_started();
484 auto wait_meter_stopper =
so_5::
details::at_scope_exit(
485 [
this] {
this->wait_finished(); } );
487 return this->m_queue.pop();
493 this->work_started();
494 auto work_meter_stopper =
so_5::
details::at_scope_exit(
495 [
this] {
this->work_finished(); } );
504using work_thread_no_activity_tracking_t =
510using work_thread_with_activity_tracking_t =
518 const so_5::mbox_t &,
520 work_thread_no_activity_tracking_t & )
526send_thread_activity_stats(
527 const so_5::mbox_t & mbox,
529 work_thread_with_activity_tracking_t & wt )
543
544
545
546
547
548
549template<
typename Work_Thread >
552 friend class disp_data_source_t;
557 const std::string_view name_base,
566 outliving_mutable(*
this)
583 auto evt_start_demand = std::make_unique<
demand_t >();
584 auto evt_finish_demand = std::make_unique<
demand_t >();
586 auto queue = std::make_unique< agent_queue_t >(
588 std::move(evt_start_demand),
589 std::move(evt_finish_demand)
596 std::addressof(agent),
602 agent_t & agent )
noexcept override
606 m_agents.erase( std::addressof(agent) );
611 agent_t & agent )
noexcept override
617 auto it =
m_agents.find( std::addressof(agent) );
625 "nef_one_thread dispatcher has no info about an agent "
626 "in bind() method" );
628 return *(it->second);
636 agent_t & agent )
noexcept override
639 this->undo_preallocation( agent );
644
645
646
647
658 const std::string_view name_base,
673 const std::size_t agents_count = [&disp]() {
674 std::lock_guard< std::mutex > lock{ disp.m_agent_map_lock };
675 return disp.m_agents.size();
688 disp.m_work_thread.demand_queue().size() );
690 send_thread_activity_stats(
693 disp.m_work_thread );
698 using agent_map_t = std::map<
agent_t *, std::unique_ptr<agent_queue_t> >;
721 make( disp_binder_shptr_t binder )
noexcept
723 return { std::move( binder )
};
735 const std::string_view data_sources_name_base,
740 using dispatcher_no_activity_tracking_t =
741 impl::dispatcher_template_t<
742 impl::work_thread_no_activity_tracking_t >;
744 using dispatcher_with_activity_tracking_t =
745 impl::dispatcher_template_t<
746 impl::work_thread_with_activity_tracking_t >;
750 dispatcher_no_activity_tracking_t
,
751 dispatcher_with_activity_tracking_t
>(
753 data_sources_name_base
,
void so_bind_to_dispatcher(event_queue_t &queue) noexcept
Binding agent to the dispatcher.
An analog of std::lock_guard for MPSC queue lock.
void notify_one() noexcept
lock_guard_t(lock_t &lock)
const lock_factory_t & lock_factory() const
Getter for lock factory.
An analog of std::unique_lock for MPSC queue lock.
void wait_for_notify() noexcept
unique_lock_t(lock_t &lock)
Alias for namespace with traits of event queue.
const queue_traits::queue_params_t & queue_params() const noexcept
Getter for queue parameters.
A handle for nef_one_thread dispatcher.
dispatcher_handle_t(disp_binder_shptr_t binder) noexcept
agent_queue_t(const agent_queue_t &)=delete
agent_queue_t(agent_queue_t &&o)=delete
void push_evt_finish(execution_demand_t demand) noexcept override
Enqueue a demand for evt_finish event.
void push_evt_start(execution_demand_t demand) override
Enqueue a demand for evt_start event.
std::reference_wrapper< demand_queue_t > m_dest_queue
demand_unique_ptr_t m_evt_start_demand
void push(execution_demand_t demand) override
Enqueue new event to the queue.
agent_queue_t & operator=(const agent_queue_t &)=delete
demand_unique_ptr_t m_evt_finish_demand
agent_queue_t(demand_queue_t &dest_queue, demand_unique_ptr_t evt_start_demand, demand_unique_ptr_t evt_finish_demand)
agent_queue_t & operator=(agent_queue_t &&o)=delete
demand_unique_ptr_t remove_head() noexcept
Helper method for deleting queue's head object.
std::size_t size() const
Get the current size of the queue.
~demand_queue_t() noexcept
void push(demand_unique_ptr_t tail_demand)
demand_t * m_head
Head of the queue.
std::atomic< std::size_t > m_size
Current size of the queue.
demand_queue_t(queue_traits::lock_unique_ptr_t lock)
Initializing constructor.
demand_t * m_tail
Tail of the queue.
void stop()
Set the shutdown signal.
demand_unique_ptr_t pop()
bool m_shutdown
Shutdown flag.
queue_traits::lock_unique_ptr_t m_lock
Queue lock.
static dispatcher_handle_t make(disp_binder_shptr_t binder) noexcept
Data source for run-time monitoring of whole dispatcher.
stats::prefix_t m_base_prefix
Basic prefix for data sources.
outliving_reference_t< dispatcher_template_t > m_dispatcher
Dispatcher to work with.
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 bind(agent_t &agent) noexcept override
Bind agent to dispatcher.
std::mutex m_agent_map_lock
Lock for agent_map protection.
~dispatcher_template_t() noexcept override
void undo_preallocation(agent_t &agent) noexcept override
Undo resources allocation.
Work_Thread m_work_thread
Worker thread for the dispatcher.
void preallocate_resources(agent_t &agent) override
Allocate resources in dispatcher for new agent.
stats::auto_registered_source_holder_t< disp_data_source_t > m_data_source
Data source for run-time monitoring.
agent_map_t m_agents
Agents for those resources are allocated by the dispatcher.
dispatcher_template_t(outliving_reference_t< environment_t > env, const std::string_view name_base, disp_params_t params)
void unbind(agent_t &agent) noexcept override
Unbind agent from dispatcher.
A part of implementation of work thread without activity tracking.
no_activity_tracking_impl_t(queue_traits::lock_unique_ptr_t lock, work_thread_holder_t thread_holder)
A part of implementation of work thread with activity tracking.
so_5::stats::work_thread_activity_stats_t take_activity_stats()
so_5::stats::activity_tracking_stuff::stats_collector_t< so_5::stats::activity_tracking_stuff::internal_lock > m_waiting_stats
Statictics for wait activity.
with_activity_tracking_impl_t(queue_traits::lock_unique_ptr_t lock, work_thread_holder_t thread_holder)
so_5::stats::activity_tracking_stuff::stats_collector_t< so_5::stats::activity_tracking_stuff::internal_lock > m_working_stats
Statictics for work activity.
A worker thread for nef_one_thread dispatcher.
void call_handler(so_5::execution_demand_t &demand)
demand_queue_t & demand_queue() noexcept
demand_unique_ptr_t pop_demand()
so_5::current_thread_id_t thread_id() const
work_thread_template_t(queue_traits::lock_unique_ptr_t lock, work_thread_holder_t thread_holder)
Initializing constructor.
An analog of unique_ptr for abstract_work_thread.
work_thread_holder_t(work_thread_holder_t &&o) noexcept
Interface for dispatcher binders.
stats::repository_t & stats_repository()
Access to repository of data sources for run-time monitoring.
An interface of event queue for agent.
Helper class for indication of long-lived reference via its type.
Base for the case of internal stats lock.
Helper for collecting activity stats.
so_5::stats::activity_stats_t take_stats()
A holder for data-souce that should be automatically registered and deregistered in registry.
A type for storing prefix of data_source name.
An interface of data source.
#define SO_5_THROW_EXCEPTION(error_code, desc)
Some reusable and low-level classes/functions which can be used in public header files.
Various stuff related to MPSC event queue implementation and tuning.
void send_thread_activity_stats(const so_5::mbox_t &, const stats::prefix_t &, work_thread_no_activity_tracking_t &)
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 nef_one_thread dispatcher.
Reusable components for dispatchers.
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.
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...
Details of SObjectizer run-time implementations.
void ensure_join_from_different_thread(current_thread_id_t thread_to_be_joined)
Ensures that join will be called from different thread.
Declarations of messages used by run-time monitoring and statistics.
Predefined suffixes of data-sources.
SO_5_FUNC suffix_t agent_count()
Suffix for data source with count of agents bound to some entity.
SO_5_FUNC suffix_t work_thread_queue_size()
Suffix for data source with count of demands in a working thread event queue.
SO_5_FUNC suffix_t work_thread_activity()
Suffix for data source with work thread activity statistics.
All stuff related to run-time monitoring and statistics.
Private part of message limit implementation.
const int rc_no_preallocated_resources_for_agent
There are no resources that must have been in place for an agent in advance.
void send(Target &&to, Args &&... args)
A utility function for creating and delivering a message or a signal.
current_thread_id_t query_current_thread_id()
Get the ID of the current thread.
outliving_reference_t< T > outliving_mutable(T &r)
Make outliving_reference wrapper for mutable reference.
A single execution demand.
demand_t * m_next
Next demand in the queue.
demand_t()=default
Default constructor.
execution_demand_t m_execution_demand
Execution demand to be used.
demand_t(execution_demand_t &&source)
Initializing constructor.
A common data for all work thread implementations.
demand_queue_t m_queue
Demands queue to work for.
common_data_t(queue_traits::lock_unique_ptr_t lock, work_thread_holder_t thread_holder)
Initializing constructor.
so_5::current_thread_id_t m_thread_id
ID of the work thread.
work_thread_holder_t m_thread_holder
Thread object.
A description of event execution demand.
void call_handler(current_thread_id_t thread_id)
Helper method to simplify demand execution.
A message with value of some quantity.
Information about one work thread activity.
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.