SObjectizer  5.8
Loading...
Searching...
No Matches
spinlocks.hpp
Go to the documentation of this file.
1/*
2 * SObjectizer-5
3 */
4
5/*!
6 * \since
7 * v.5.4.0
8 *
9 * \file
10 * \brief Definition of various types of spinlocks.
11 */
12
13#pragma once
14
15#include <atomic>
16#include <thread>
17#include <cstdint>
18
19#if defined(_MSC_VER) && defined(__SSE2__)
20 #define SO_5_ARCH_MSC_WITH_SSE2
21
22 #include <intrin.h>
23#endif
24
25namespace so_5
26{
27
28//
29// yield_backoff_t
30//
31/*!
32 * \since
33 * v.5.4.0
34 *
35 * \brief An implementation of backoff object with usage of std::yield.
36 */
38 {
39 public :
40 inline void
42 {
43 std::this_thread::yield();
44 }
45 };
46
47//
48// pause_backoff_t
49//
50/*!
51 * \since
52 * v.5.5.22.2
53 *
54 * \brief An implementation of backoff object using assembly instruction.
55 *
56 * \note
57 * This implementation is provided by Pavel Begunkov.
58 */
60 {
61 public :
62 inline void
64 {
65#if (defined(__GNUC__) || defined(__clang__)) &&
66 (defined(_M_X64) || defined(_M_AMD64) || defined(__amd64__) ||
67 defined(__amd64) ||
68 defined(_M_IX86) || defined(__i386__) || defined(__i386))
69 asm( "pause;" );
70#elif defined(SO_5_ARCH_MSC_WITH_SSE2)
71 _mm_pause();
72#else
73 ;
74#endif
75 }
76 };
77
78//
79// spinlock_t
80//
81/*!
82 * \since
83 * v.5.4.0
84 *
85 * \brief A simple spinlock (analog of std::mutex).
86 *
87 * \note
88 * Since v.5.5.22.2 a TATAS spinlock implementation is used.
89 * Implementation is provided by Pavel Begunkov.
90 */
91template< class Backoff >
93 {
94 public :
96 {
98 }
99 spinlock_t( const spinlock_t & ) = delete;
100 spinlock_t( spinlock_t && ) = delete;
101
102 spinlock_t & operator=( const spinlock_t & ) = delete;
103 spinlock_t & operator=( spinlock_t && ) = delete;
104
105 //! Lock object.
106 void
108 {
110
111 do
112 {
114 backoff();
115 }
116 while( m_flag.exchange( true, std::memory_order_acquire ) );
117 }
118
119 //! Unlock object.
120 void
122 {
124 }
125
126 private :
127 //! Atomic flag which is used as actual lock.
129 };
130
131//
132// default_spinlock_t
133//
134using default_spinlock_t = spinlock_t< pause_backoff_t >;
135
136//
137// rw_spinlock_t
138//
139/*!
140 * \since
141 * v.5.4.0
142 *
143 * \brief A simple multi-readers/single-writer spinlock
144 * (analog of std::shared_mutex).
145 *
146 * This implementation is based on Dmitry Vyukov implementation
147 * from LLVM code base:
148 * http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_mutex.cc?revision=210345&view=markup
149 */
150template< class Backoff >
152 {
153 private :
155
156 static constexpr const std::uint_fast32_t unlocked = 0;
157 static constexpr const std::uint_fast32_t write_lock = 1;
158 static constexpr const std::uint_fast32_t read_lock = 2;
159
160 public :
165 rw_spinlock_t( const rw_spinlock_t & ) = delete;
166
167 rw_spinlock_t & operator=( const rw_spinlock_t & ) = delete;
168
169 //! Lock object in shared mode.
170 inline void
185
186 //! Unlock object locked in shared mode.
187 inline void
192
193 //! Lock object in exclusive mode.
194 inline void
196 {
199
204 return;
205
207
208 while( true )
209 {
211 {
213
218 break;
219 }
220
221 backoff();
222 }
223 }
224
225 //! Unlock object locked in exclusive mode.
226 inline void
231 };
232
233using default_rw_spinlock_t = rw_spinlock_t< pause_backoff_t >;
234
235//
236// read_lock_guard_t
237//
238/*!
239 * \since
240 * v.5.4.0
241 *
242 * \brief Scoped guard for shared locks.
243 */
244template< class Lock >
246 {
247 private :
248 Lock & m_lock;
249
250 public :
251 read_lock_guard_t( Lock & l ) : m_lock( l )
252 {
254 }
259
262 operator=( const read_lock_guard_t & ) = delete;
263 };
264
265} /* namespace so_5 */
Interface for message sink.
A context for agent construction and tuning.
agent_tuning_options_t & options()
Read-Write access to agent options.
environment_t & env() const
Access to SObjectizer Environment.
Helper class for holding agent's identity (name or pointer).
agent_identity_t(const agent_t *pointer) noexcept
Initializing constructor for case when agent has no user specified name.
Interface of the agent state listener.
Type for holding information necessary for handling time limits for agent states.
Definition agent.hpp:3019
bool is_defined() const noexcept
Is the data for handling time limits defined?
Definition agent.cpp:692
A base class for agents.
Definition agent.hpp:673
void so_subscribe_deadletter_handler(const so_5::mbox_t &mbox, Event_Handler &&handler, thread_safety_t thread_safety=thread_safety_t::unsafe)
Create a subscription for deadletter handler for a specific message from a specific mbox.
Definition agent.hpp:2125
static demand_handler_pfn_t get_demand_handler_on_start_ptr() noexcept
Definition agent.cpp:1461
impl::state_listener_controller_t m_state_listener_controller
State listeners controller.
Definition agent.hpp:2854
decltype(auto) so_low_level_exec_as_event_handler(Lambda &&lambda) noexcept(noexcept(lambda()))
Helper method that allows to run a block of code as non-thread-safe event handler.
Definition agent.hpp:2675
static void process_enveloped_msg(current_thread_id_t working_thread_id, execution_demand_t &d, const impl::event_handler_data_t *handler_data)
Actual implementation of enveloped message handling.
Definition agent.cpp:1591
std::unique_ptr< impl::sinks_storage_t > m_message_sinks
Holder of message sinks for that agent.
Definition agent.hpp:2894
const state_t st_default
Definition agent.hpp:2819
void do_change_agent_state(const state_t &state_to_be_set)
Perform actual operations related to state switch.
Definition agent.cpp:1791
void so_drop_delivery_filter(const mbox_t &mbox) noexcept
Drop a delivery filter.
Definition agent.hpp:2576
void so_initiate_agent_definition()
A correct initiation of so_define_agent method call.
Definition agent.cpp:963
const agent_t * self_ptr() const
Get the raw pointer of itself.
Definition agent.hpp:834
void evt_state_time_limit(mhood_t< so_5::details::msg_state_timeout >)
Special event handler to process state time limits.
Definition agent.cpp:1945
so_5::current_thread_id_t m_working_thread_id
Working thread id.
Definition agent.hpp:2950
default_rw_spinlock_t m_event_queue_lock
Event queue operation protector.
Definition agent.hpp:2918
const name_for_agent_t m_name
Optional name for the agent.
Definition agent.hpp:2999
static const impl::event_handler_data_t * find_deadletter_handler(execution_demand_t &demand)
Search for event handler between deadletter handlers.
Definition agent.cpp:1781
static demand_handler_pfn_t get_demand_handler_on_message_ptr() noexcept
Definition agent.cpp:1526
bool is_agent_deactivated() const noexcept
Is agent already deactivated.
Definition agent.cpp:1907
void so_switch_to_awaiting_deregistration_state()
Switching agent to special state in case of unhandled exception.
Definition agent.cpp:881
const state_t & so_current_state() const
Access to the current agent state.
Definition agent.hpp:967
static void process_message(current_thread_id_t working_thread_id, execution_demand_t &d, thread_safety_t thread_safety, event_handler_method_t method)
Actual implementation of message handling.
Definition agent.cpp:1550
agent_ref_t create_ref()
Make an agent reference.
Definition agent.cpp:1150
void so_drop_deadletter_handler(const so_5::mbox_t &mbox)
Drops the subscription for deadletter handler.
Definition agent.hpp:2174
bool so_is_active_state(const state_t &state_to_check) const noexcept
Is a state activated?
Definition agent.cpp:844
void ensure_operation_is_on_working_thread(const char *operation_name) const
Enables operation only if it is performed on agent's working thread.
Definition agent.cpp:1639
static constexpr const state_t::history_t deep_history
Short alias for so_5::state_t::history_t::deep.
Definition agent.hpp:737
bool so_was_defined() const
Is method define_agent already called?
Definition agent.cpp:981
agent_status_t
Enumeration of possible agent statuses.
Definition agent.hpp:2834
@ shutdown_with_skipping_pending_demands
Agent was shutdown and all pending demands have to be skipped.
@ defined
Agent is defined.
@ state_switch_in_progress
State switch operation is in progress.
void destroy_all_subscriptions_and_filters() noexcept
Destroy all agent's subscriptions.
Definition agent.cpp:1143
void shutdown_agent() noexcept
Agent shutdown deriver.
Definition agent.cpp:1163
void ensure_binding_finished()
Ensures that all agents from cooperation are bound to dispatchers.
Definition agent.cpp:1452
coop_t * m_agent_coop
Agent is belong to this cooperation.
Definition agent.hpp:2953
disp_binder_shptr_t so_this_agent_disp_binder() const
Returns the dispatcher binder that is used for binding this agent.
Definition agent.hpp:2716
agent_status_t m_current_status
Current agent status.
Definition agent.hpp:2851
event_queue_t * m_event_queue
A pointer to event_queue.
Definition agent.hpp:2933
void so_set_delivery_filter(const mbox_t &mbox, Lambda &&lambda)
Set a delivery filter.
Definition agent.hpp:3577
virtual void so_define_agent()
Hook on define agent for SObjectizer.
Definition agent.cpp:975
void drop_all_delivery_filters() noexcept
Drops all delivery filters.
Definition agent.cpp:1664
disp_binder_shptr_t m_disp_binder
Binder for this agent.
Definition agent.hpp:2987
bool do_check_subscription_presence(const mbox_t &mbox, const std::type_index &msg_type, const state_t &target_state) const noexcept
Check the presence of a subscription.
Definition agent.cpp:1337
void do_state_switch(const state_t &state_to_be_set) noexcept
Actual action for switching agent state.
Definition agent.cpp:1832
std::unique_ptr< impl::delivery_filter_storage_t > m_delivery_filters
Delivery filters for that agents.
Definition agent.hpp:2962
void return_to_default_state_if_possible() noexcept
Return agent to the default state.
Definition agent.cpp:1895
subscription_bind_t so_subscribe_self()
Initiate subscription to agent's direct mbox.
Definition agent.hpp:1461
agent_t(environment_t &env)
Constructor.
Definition agent.cpp:775
void do_set_delivery_filter(const mbox_t &mbox, const std::type_index &msg_type, delivery_filter_unique_ptr_t filter)
Set a delivery filter.
Definition agent.cpp:1674
static void call_push_event(agent_t &agent, const message_limit::control_block_t *limit, mbox_id_t mbox_id, const std::type_index &msg_type, const message_ref_t &message)
Push an event to the agent's event queue.
Definition agent.hpp:1070
mbox_t so_make_new_direct_mbox()
Create a new direct mbox for that agent.
Definition agent.cpp:893
static execution_hint_t so_create_execution_hint(execution_demand_t &demand)
Create execution hint for the specified demand.
Definition agent.cpp:1035
void so_change_state(const state_t &new_state)
Change the current state of the agent.
Definition agent.cpp:936
void push_event(const message_limit::control_block_t *limit, mbox_id_t mbox_id, const std::type_index &msg_type, const message_ref_t &message)
Push event into the event queue.
Definition agent.cpp:1403
static custom_direct_mbox_factory_t custom_direct_mbox_factory(Lambda &&lambda)
Helper for creation a custom direct mbox factory.
Definition agent.hpp:1168
coop_handle_t so_coop() const
Get a handle of agent's coop.
Definition agent.cpp:994
void so_deregister_agent_coop_normally()
A helper method for deregistering agent's coop in case of normal deregistration.
Definition agent.cpp:1116
void do_drop_delivery_filter(const mbox_t &mbox, const std::type_index &msg_type) noexcept
Drop a delivery filter.
Definition agent.cpp:1702
virtual exception_reaction_t so_exception_reaction() const noexcept
A reaction from SObjectizer to an exception from agent's event.
Definition agent.cpp:871
void so_deactivate_agent()
Deactivate the agent.
Definition agent.cpp:945
disp_binder_shptr_t so_this_coop_disp_binder() const
Returns the dispatcher binder that is used as the default binder for the agent's coop.
Definition agent.cpp:1122
abstract_message_sink_t & detect_sink_for_message_type(const std::type_index &msg_type)
Helper function that returns a message sink to be used for subscriptions for specified message type.
Definition agent.cpp:1291
const demands_handling_on_dereg_t m_demands_handling_on_dereg
What to do with pending demands on deregistration.
Definition agent.hpp:3085
static void demand_handler_on_message(current_thread_id_t working_thread_id, execution_demand_t &d)
Calls event handler for message.
Definition agent.cpp:1509
static constexpr const state_t::history_t shallow_history
Short alias for so_5::state_t::history_t::shallow.
Definition agent.hpp:730
void so_drop_all_subscriptions_and_filters()
Dropping all agents subscriptions and filters.
Definition agent.cpp:954
virtual void so_evt_finish()
Hook of agent finish in SObjectizer.
Definition agent.cpp:838
static demand_handler_pfn_t get_demand_handler_on_finish_ptr() noexcept
Definition agent.cpp:1503
void so_add_nondestroyable_listener(agent_state_listener_t &state_listener)
Add a state listener to the agent.
Definition agent.cpp:853
static const impl::event_handler_data_t * handler_finder_msg_tracing_disabled(execution_demand_t &demand, const char *context_marker)
Handler finder for the case when message delivery tracing is disabled.
Definition agent.cpp:1713
const state_t * m_current_state_ptr
Current agent state.
Definition agent.hpp:2826
const mbox_t m_direct_mbox
A direct mbox for the agent.
Definition agent.hpp:2940
bool so_has_deadletter_handler(const so_5::mbox_t &mbox) const noexcept
Checks the presence of deadletter handler for a message of a specific type from a specific mbox.
Definition agent.hpp:2215
agent_t * self_ptr()
Definition agent.hpp:840
const priority_t m_priority
Priority of the agent.
Definition agent.hpp:2969
agent_identity_t so_agent_name() const noexcept
Get an optional name of the agent.
Definition agent.cpp:1134
static const impl::event_handler_data_t * handler_finder_msg_tracing_enabled(execution_demand_t &demand, const char *context_marker)
Handler finder for the case when message delivery tracing is enabled.
Definition agent.cpp:1726
void bind_to_coop(coop_t &coop)
Bind agent to the cooperation.
Definition agent.cpp:1157
static const impl::event_handler_data_t * find_event_handler_for_current_state(execution_demand_t &demand)
Actual search for event handler with respect to parent-child relationship between agent states.
Definition agent.cpp:1760
static void demand_handler_on_start(current_thread_id_t working_thread_id, execution_demand_t &d)
Calls so_evt_start method for agent.
Definition agent.cpp:1425
const mbox_t & so_direct_mbox() const
Get the agent's direct mbox.
Definition agent.cpp:887
impl::subscription_storage_unique_ptr_t m_subscriptions
All agent's subscriptions.
Definition agent.hpp:2881
void so_destroy_deadletter_subscription(const mbox_t &mbox, const std::type_index &msg_type)
Destroy a subscription for a deadletter handler.
Definition agent.cpp:1277
static void demand_handler_on_finish(current_thread_id_t working_thread_id, execution_demand_t &d)
Calls so_evt_finish method for agent.
Definition agent.cpp:1467
bool do_check_deadletter_presence(const mbox_t &mbox, const std::type_index &msg_type) const noexcept
Check the presence of a deadletter handler.
Definition agent.cpp:1347
void do_drop_subscription_for_all_states(const mbox_t &mbox, const std::type_index &msg_type)
Remove subscription for all states.
Definition agent.cpp:1322
agent_t(context_t ctx)
Constructor which simplifies agent construction with or without agent's tuning options.
Definition agent.cpp:788
static agent_tuning_options_t tuning_options()
Create tuning options object with default values.
Definition agent.hpp:1136
void so_add_destroyable_listener(agent_state_listener_unique_ptr_t state_listener)
Add a state listener to the agent.
Definition agent.cpp:862
environment_t & so_environment() const noexcept
Access to the SObjectizer Environment which this agent is belong.
Definition agent.cpp:987
priority_t so_priority() const noexcept
Get the priority of the agent.
Definition agent.hpp:2600
const state_t & so_default_state() const
Access to the agent's default state.
Definition agent.cpp:900
subscription_bind_t so_subscribe(const mbox_t &mbox_ref)
Initiate subscription.
Definition agent.hpp:1404
virtual ~agent_t()
Definition agent.cpp:824
state_time_limit_handling_data_t m_state_time_limit_handling_data
Data that is necessary for handling time limits of agent's states.
Definition agent.hpp:3077
void so_bind_to_dispatcher(event_queue_t &queue) noexcept
Binding agent to the dispatcher.
Definition agent.cpp:1006
void so_destroy_event_subscription(const mbox_t &mbox, const std::type_index &subscription_type, const state_t &target_state)
Destroy event subscription.
Definition agent.hpp:1506
void so_set_delivery_filter_for_mutable_msg(const mbox_t &mbox, Lambda &&lambda)
Set a delivery filter for a mutable message.
Definition agent.hpp:3600
environment_t & m_env
SObjectizer Environment for which the agent is belong.
Definition agent.hpp:2897
void so_deregister_agent_coop(int dereg_reason)
A helper method for deregistering agent's coop.
Definition agent.cpp:1109
void do_drop_subscription(const mbox_t &mbox, const std::type_index &msg_type, const state_t &target_state)
Remove subscription for the state specified.
Definition agent.cpp:1307
agent_t(environment_t &env, agent_tuning_options_t tuning_options)
Constructor which allows specification of agent's tuning options.
Definition agent.cpp:781
handler_finder_t m_handler_finder
Function for searching event handler.
Definition agent.hpp:2874
void define_state_time_limit_handling_data_if_needed()
Initialize data for handling time limit of agent's states.
Definition agent.cpp:1913
virtual void so_evt_start()
Hook on agent start inside SObjectizer.
Definition agent.cpp:832
static demand_handler_pfn_t get_demand_handler_on_enveloped_msg_ptr() noexcept
Definition agent.cpp:1544
void so_create_event_subscription(const mbox_t &mbox_ref, std::type_index type_index, const state_t &target_state, const event_handler_method_t &method, thread_safety_t thread_safety, event_handler_kind_t handler_kind)
Create a subscription for an event.
Definition agent.cpp:1221
void so_set_delivery_filter(const mbox_t &mbox, delivery_filter_unique_ptr_t filter)
Set a delivery filter.
Definition agent.hpp:2471
static void demand_handler_on_enveloped_msg(current_thread_id_t working_thread_id, execution_demand_t &d)
Handles the enveloped message.
Definition agent.cpp:1532
A collector for agent tuning options.
so_5::priority_t query_priority() const noexcept
Get priority value.
bool is_user_provided_subscription_storage_factory() const noexcept
Does a user provide a specific subscription_storage_factory?
demands_handling_on_dereg_t demands_handling_on_dereg() const noexcept
The base class for the object with a reference counting.
atomic_refcounted_t(const atomic_refcounted_t &)=delete
unsigned long dec_ref_count() noexcept
Decrement reference count.
atomic_refcounted_t() noexcept
Default constructor.
atomic_counter_t m_ref_counter
Object reference count.
void inc_ref_count() noexcept
Increments reference count.
~atomic_refcounted_t() noexcept=default
Destructor.
atomic_refcounted_t & operator=(const atomic_refcounted_t &)=delete
Type of smart handle for a cooperation.
Agent cooperation.
Definition coop.hpp:389
exception_reaction_t exception_reaction() const noexcept
Get the current exception rection flag for that cooperation.
Definition coop.hpp:758
coop_handle_t handle() noexcept
Get handle for this coop.
Definition coop.hpp:432
An implementation of handler_invoker interface.
SObjectizer Environment.
void deregister_coop(coop_handle_t coop, int reason) noexcept
Deregister the cooperation.
An interface of event queue for agent.
A hint for a dispatcher for execution of event for the concrete execution_demand.
static execution_hint_t create_empty_execution_hint(execution_demand_t &demand)
A special class for accessing private members of agent_coop.
static void decrement_usage_count(coop_t &coop)
static void increment_usage_count(coop_t &coop) noexcept
A helper class for accessing the functionality of environment-class which is specific for SObjectizer...
event_queue_t * event_queue_on_bind(agent_t *agent, event_queue_t *original_queue) noexcept
Call the event_queue_hook when an agent is being bound to a particular event_queue.
void event_queue_on_unbind(agent_t *agent, event_queue_t *queue) noexcept
Call the event_queue_hook when an agent is being unbound from its event_queue.
bool is_msg_tracing_enabled() const
Is message delivery tracing enabled?
internal_env_iface_t(environment_t &env)
Initializing constructor.
mbox_t create_limitless_mpsc_mbox(agent_t &single_consumer)
Create multi-producer/single-consumer mbox that ignores message limits.
agent_t::agent_status_t m_previous_status
Definition agent.cpp:910
state_switch_guard_t(agent_t &agent)
Definition agent.cpp:913
Template class for smart reference wrapper on the atomic_refcounted_t.
bool operator==(const intrusive_ptr_t &o) const
intrusive_ptr_t(T *obj) noexcept
Constructor for a raw pointer.
T * m_obj
Object controlled by a smart reference.
void dismiss_object() noexcept
Decrement reference count to object and delete it if needed.
void reset() noexcept
Drop controlled object.
intrusive_ptr_t(const intrusive_ptr_t< Y > &o) noexcept
Constructor from another smart reference.
intrusive_ptr_t(std::unique_ptr< Y > o) noexcept
Constructor from unique_ptr instance.
intrusive_ptr_t(intrusive_ptr_t &&o) noexcept
Move constructor.
bool operator<(const intrusive_ptr_t &o) const
T * operator->() const noexcept
intrusive_ptr_t(const intrusive_ptr_t &o) noexcept
Copy constructor.
intrusive_ptr_t & operator=(const intrusive_ptr_t &o) noexcept
Copy operator.
intrusive_ptr_t & operator=(intrusive_ptr_t &&o) noexcept
Move operator.
intrusive_ptr_t< Y > make_reference() const noexcept
Make reference with casing to different type.
T & operator*() const noexcept
void take_object() noexcept
Increment reference count to object if it's not null.
friend void swap(intrusive_ptr_t &a, intrusive_ptr_t &b) noexcept
Swap values.
intrusive_ptr_t() noexcept
Default constructor.
T * get() const noexcept
~intrusive_ptr_t() noexcept
Destructor.
operator bool() const noexcept
Is this a null reference?
A base class for agent messages.
Definition message.hpp:47
A message wrapped to be used as type of argument for event handlers.
Definition mhood.hpp:570
Type for holding agent name.
unsigned int m_length
Name length.
name_for_agent_t & operator=(name_for_agent_t &&other) noexcept
Definition agent.cpp:142
name_for_agent_t(const name_for_agent_t &)
Definition agent.cpp:115
name_for_agent_t(name_for_agent_t &&other) noexcept
Definition agent.cpp:136
bool has_value() const noexcept
Does this object have a value?
Definition agent.cpp:169
name_for_agent_t & operator=(const name_for_agent_t &)
Definition agent.cpp:129
std::string_view as_string_view() const
Get the value as a string_view.
Definition agent.cpp:160
name_for_agent_t()
Default constructor makes an null value.
Definition agent.cpp:104
name_for_agent_t(std::string_view value)
Initializing constructor.
Definition agent.cpp:108
Wrapper around a pointer to partially constructed agent.
An implementation of backoff object using assembly instruction.
Definition spinlocks.hpp:60
Scoped guard for shared locks.
read_lock_guard_t(const read_lock_guard_t &)=delete
read_lock_guard_t & operator=(const read_lock_guard_t &)=delete
A simple multi-readers/single-writer spinlock (analog of std::shared_mutex).
void lock()
Lock object in exclusive mode.
static constexpr const std::uint_fast32_t unlocked
void unlock()
Unlock object locked in exclusive mode.
void unlock_shared()
Unlock object locked in shared mode.
std::atomic_uint_fast32_t m_counters
void lock_shared()
Lock object in shared mode.
static constexpr const std::uint_fast32_t write_lock
static constexpr const std::uint_fast32_t read_lock
rw_spinlock_t & operator=(const rw_spinlock_t &)=delete
rw_spinlock_t(const rw_spinlock_t &)=delete
A simple spinlock (analog of std::mutex).
Definition spinlocks.hpp:93
void lock()
Lock object.
void unlock()
Unlock object.
spinlock_t(const spinlock_t &)=delete
std::atomic_bool m_flag
Atomic flag which is used as actual lock.
spinlock_t & operator=(spinlock_t &&)=delete
spinlock_t & operator=(const spinlock_t &)=delete
spinlock_t(spinlock_t &&)=delete
Helper class for simplify iteration on state's path.
Definition state.hpp:1841
state_path_t(const state_t &state) noexcept
Initializing constructor.
Definition state.hpp:1853
Information of time_limit for a state.
Definition agent.cpp:199
void change(duration_t limit, const state_t &state_to_switch) noexcept
Definition agent.cpp:224
const state_t & state_to_switch() const noexcept
Definition agent.cpp:314
void on_state_activation(const agent_t::state_time_limit_handling_data_t &info) noexcept
Definition agent.cpp:243
bool is_limit_exceeded(const steady_clock::time_point current_time) const noexcept
Definition agent.cpp:300
std::optional< activation_data_t > m_activation_data
Definition agent.cpp:355
std::reference_wrapper< const state_t > m_state_to_switch
The target state to switch after the timeout.
Definition agent.cpp:350
void activate(const agent_t::state_time_limit_handling_data_t &info)
Definition agent.cpp:268
time_limit_t(duration_t limit, const state_t &state_to_switch) noexcept
Initializing constructor.
Definition agent.cpp:208
void on_state_deactivation() noexcept
Definition agent.cpp:254
bool is_target(const agent_t *agent) const noexcept
Is agent owner of this state?
Definition agent.cpp:552
state_t & time_limit(duration_t timeout, const state_t &state_to_switch)
Set up a time limit for the state.
Definition agent.cpp:569
std::enable_if< details::is_agent_method_pointer< details::method_arity::nullary, Method_Pointer >::value, state_t & >::type on_enter(Method_Pointer pfn)
Set on enter handler.
Definition agent.hpp:4042
state_t & drop_time_limit()
Drop time limit for the state if defined.
Definition agent.cpp:618
history_t m_state_history
Type of state history.
Definition state.hpp:1643
const state_t * parent_state() const noexcept
Get a parent state if exists.
Definition state.hpp:1719
state_t(initial_substate_of parent, std::string state_name, history_t state_history)
Constructor for the case when state is the initial substate of some parent state.
Definition agent.cpp:427
state_t(substate_of parent)
Constructor for the case when state is a substate of some parent state.
Definition agent.cpp:447
bool is_active() const noexcept
Is this state or any of its substates activated?
Definition agent.hpp:3918
state_t(state_t &&other)
Move constructor.
Definition agent.cpp:470
state_t(agent_t *target_agent, std::string state_name, state_t *parent_state, std::size_t nested_level, history_t state_history)
Fully initializing constructor.
Definition agent.cpp:362
state_t(agent_t *agent, std::string state_name, history_t state_history)
Definition agent.cpp:409
state_t * m_parent_state
Parent state.
Definition state.hpp:1624
state_t(substate_of parent, std::string state_name, history_t state_history)
Constructor for the case when state is a substate of some parent state.
Definition agent.cpp:458
const state_t * actual_state_to_enter() const
Find actual state to be activated for agent.
Definition agent.cpp:626
state_t(initial_substate_of parent)
Constructor for the case when state is the initial substate of some parent state.
Definition agent.cpp:416
const state_t * m_initial_substate
The initial substate.
Definition state.hpp:1636
bool operator==(const state_t &state) const noexcept
Definition agent.cpp:492
state_t(initial_substate_of parent, std::string state_name)
Constructor for the case when state is the initial substate of some parent state.
Definition agent.cpp:421
size_t m_substate_count
Number of substates.
Definition state.hpp:1673
state_t(agent_t *agent, history_t state_history)
Definition agent.cpp:396
bool operator!=(const state_t &state) const noexcept
Definition state.hpp:360
agent_t *const m_target_agent
Owner of this state.
Definition state.hpp:1605
std::string query_name() const
Get textual name of the state.
Definition agent.cpp:498
state_t(substate_of parent, std::string state_name)
Constructor for the case when state is a substate of some parent state.
Definition agent.cpp:452
const state_t * m_last_active_substate
Last active substate.
Definition state.hpp:1654
void handle_time_limit_on_enter() const
A special handler of time limit to be used on entering into state.
Definition agent.cpp:670
state_t(agent_t *agent, std::string state_name)
Definition agent.cpp:403
void activate() const
Switch agent to that state.
Definition agent.cpp:563
history_t
Type of history for state.
Definition state.hpp:173
@ none
State has no history.
@ deep
State has deep history.
@ shallow
State has shallow history.
void update_history_in_parent_states() const
A helper method which is used during state change for update state with history.
Definition agent.cpp:649
void handle_time_limit_on_exit() const
A special handler of time limit to be used on exiting from state.
Definition agent.cpp:677
state_t(agent_t *agent)
Definition agent.cpp:390
A class for creating a subscription to messages from the mbox.
Definition agent.hpp:174
std::vector< const state_t * > state_vector_t
Type of vector of states.
Definition agent.hpp:414
mbox_t m_mbox_ref
Mbox for messages to subscribe.
Definition agent.hpp:407
subscription_bind_t & just_switch_to(const state_t &target_state)
Define handler which only switches agent to the specified state.
Definition agent.hpp:3856
void create_subscription_for_states(const std::type_index &msg_type, const event_handler_method_t &method, thread_safety_t thread_safety, event_handler_kind_t handler_kind) const
Create subscription of event for all states.
Definition agent.hpp:3879
subscription_bind_t(agent_t &agent, const mbox_t &mbox_ref)
Definition agent.hpp:3627
subscription_bind_t & in(const state_t &state)
Set up a state in which events are allowed be processed.
Definition agent.hpp:3636
subscription_bind_t & suppress()
Suppress processing of event in this state.
Definition agent.hpp:3832
subscription_bind_t & transfer_to_state(const state_t &target_state)
An instruction for switching agent to the specified state and transfering event proceessing to new st...
Definition agent.hpp:3699
state_vector_t m_states
States of agents the event to be subscribed in.
Definition agent.hpp:421
void ensure_handler_can_be_used_with_mbox(const so_5::details::msg_type_and_handler_pair_t &handler) const
Additional check for subscription to a mutable message from MPMC mbox.
Definition agent.hpp:3906
agent_t * m_agent
Agent to which we are subscribing.
Definition agent.hpp:405
An indentificator for the timer.
Definition timers.hpp:82
An implementation of backoff object with usage of std::yield.
Definition spinlocks.hpp:38
#define SO_5_EXPORT
Definition declspec.hpp:26
#define SO_5_TYPE
Definition declspec.hpp:46
#define SO_5_IMPORT
Definition declspec.hpp:27
#define SO_5_FUNC
Definition declspec.hpp:48
#define SO_5_LOG_ERROR(logger, var_name)
A special macro for helping error logging.
#define SO_5_THROW_EXCEPTION(error_code, desc)
Definition exception.hpp:74
demand_handler_pfn_t select_demand_handler_for_message(const agent_t &agent, const message_ref_t &msg)
A helper function to select actual demand handler in dependency of message kind.
Definition agent.cpp:1365
mbox_t make_direct_mbox_with_respect_to_custom_factory(partially_constructed_agent_ptr_t agent_ptr, const agent_tuning_options_t &tuning_options, mbox_t standard_mbox)
Helper for creation of the direct mbox for an agent.
Definition agent.cpp:722
unsigned int ensure_valid_agent_name_length(std::size_t length)
Definition agent.cpp:83
std::string create_anonymous_state_name(const agent_t *agent, const state_t *st)
Definition agent.cpp:183
const state_t deadletter_state(nullptr, "<DEADLETTER_STATE>")
A special object to be used as state for make subscriptions for deadletter handlers.
subscription_storage_factory_t detect_subscription_storage_factory_to_use(environment_t &env, const agent_tuning_options_t &tuning_options)
Helper for selection of subscription storage factory.
Definition agent.cpp:759
const state_t awaiting_deregistration_state(nullptr, "<AWAITING_DEREGISTRATION_AFTER_UNHANDLED_EXCEPTION>")
A special object for the state in which agent is awaiting for deregistration after unhandled exceptio...
Enumeration of cooperation deregistration reasons.
Definition coop.hpp:39
const int normal
Normal deregistration.
Definition coop.hpp:46
Some reusable and low-level classes/functions which can be used in public header files.
Internal namespace with details of agent_t implementation.
Definition agent.hpp:462
Various helpers for message delivery tracing mechanism.
void safe_trace_state_leaving(const agent_t &state_owner, const state_t &state)
Helper for tracing the fact of leaving a state.
void safe_trace_state_entering(const agent_t &state_owner, const state_t &state)
Helper for tracing the fact of entering into a state.
void trace_deadletter_handler_search_result(const execution_demand_t &demand, const char *context_marker, const event_handler_data_t *search_result)
Helper for tracing the result of search of deadletter handler.
void trace_event_handler_search_result(const execution_demand_t &demand, const char *context_marker, const event_handler_data_t *search_result)
Helper for tracing the result of event handler search.
Details of SObjectizer run-time implementations.
Definition agent.cpp:905
All stuff related to message limits.
Definition message.hpp:862
Private part of message limit implementation.
Definition agent.cpp:33
message_delivery_mode_t
Possible modes of message/signal delivery.
Definition types.hpp:172
exception_reaction_t
A reaction of SObjectizer to an exception from agent event.
Definition agent.hpp:65
@ abort_on_exception
Execution of application must be aborted immediatelly.
Definition agent.hpp:67
@ inherit_exception_reaction
Exception reaction should be inherited from SO Environment.
Definition agent.hpp:81
@ ignore_exception
Exception should be ignored and agent should continue its work.
Definition agent.hpp:75
@ deregister_coop_on_exception
Definition agent.hpp:73
@ shutdown_sobjectizer_on_exception
Definition agent.hpp:70
demands_handling_on_dereg_t
How pending demands should be handled on deregistration.
@ skip
Pending demands have to be skipped.
current_thread_id_t null_current_thread_id()
Get NULL thread id.
const thread_safety_t not_thread_safe
Shorthand for thread unsafety indicator.
Definition types.hpp:62
priority_t
Definition of supported priorities.
Definition priority.hpp:28
const thread_safety_t thread_safe
Shorthand for thread safety indicator.
Definition types.hpp:69
message_mutability_t
A enum with variants of message mutability or immutability.
Definition types.hpp:94
std::thread::id raw_id_from_current_thread_id(const current_thread_id_t &w)
Get the raw thread id from current_thread_id.
thread_safety_t
Thread safety indicator.
Definition types.hpp:50
@ unsafe
Not thread safe.
@ safe
Thread safe.
intrusive_ptr_t< Derived > make_agent_ref(Derived *agent)
Helper function template for the creation of smart pointer to an agent.
Definition agent.hpp:3567
message_kind_t
A enum with variants of message kinds.
Definition types.hpp:109
@ user_type_message
Message is an user type message.
@ enveloped_msg
Message is an envelope with some other message inside.
SO_5_FUNC void swap(name_for_agent_t &a, name_for_agent_t &b) noexcept
Definition agent.cpp:150
mbox_id_t null_mbox_id()
Default value for null mbox_id.
Definition types.hpp:39
work_thread_activity_tracking_t
Values for dispatcher's work thread activity tracking.
Definition types.hpp:75
@ unspecified
Tracking mode is specified elsewhere.
intrusive_ptr_t(std::unique_ptr< T >) -> intrusive_ptr_t< T >
event_handler_kind_t
Kind of an event handler.
Definition types.hpp:154
current_thread_id_t query_current_thread_id()
Get the ID of the current thread.
Type for case when agent has no user-provided name.
SO_5_FUNC std::array< char, c_string_size > make_c_string() const noexcept
Make a c-string with text representation of a value.
Definition agent.cpp:39
A description of event execution demand.
mbox_id_t m_mbox_id
ID of mbox.
agent_t * m_receiver
Receiver of demand.
const message_limit::control_block_t * m_limit
Optional message limit for that message.
A helper class for temporary setting and then dropping the ID of the current working thread.
Definition agent.hpp:474
working_thread_id_sentinel_t(so_5::current_thread_id_t &id_var, so_5::current_thread_id_t value_to_set)
Definition agent.hpp:477
Information about event_handler and its properties.
thread_safety_t m_thread_safety
Is event handler thread safe or not.
event_handler_kind_t m_kind
Kind of this event handler.
Helper for marking initial substate of composite state.
Definition state.hpp:57
A control block for one message limit.
Definition message.hpp:976
static void decrement(const control_block_t *limit)
Definition message.hpp:1028
A mixin with message limit definition methods.
Helper type with method to be mixed into agent class.
activation_data_t(timer_id_t timer, steady_clock::time_point expiration_point)
Definition agent.cpp:336
steady_clock::time_point m_expiration_point
Timeout of timeout expiration.
Definition agent.cpp:334
timer_id_t m_timer
ID of delayed timeout signal.
Definition agent.cpp:331
Helper for marking a substate of composite state.
Definition state.hpp:89
#define SO_5_VERSION_PATCH
Definition version.hpp:45
#define SO_5_VERSION_MAJOR
Definition version.hpp:24
#define SO_5_VERSION_MAKE(major, minor, patch)
Definition version.hpp:58
#define SO_5_VERSION_MINOR
Definition version.hpp:34