SObjectizer  5.8
Loading...
Searching...
No Matches
so-5.5.24 event_queue_hook

What Is It?

SObjectizer-5.5.24 contains another feature for customization of SObjectizer Run-Time behaviour. It is an event_queue_hook.

Since v.5.5.24 there is a single instance of event_queue_hook in SObjectizer Environment. By default it is no-op hook (nothing is happened when this no-op hook is invoked). But user can set his/her own instance can in environment_params_t before launching of SObjectizer Environment.

Since v.5.5.24 every agent calls event_queue_hook two times:

  • the first call to event_queue_hook is performed when agent is being bound to a particular event queue. Agent receives a pointer to event_queue provided by corresponding dispatcher and passes that pointer to event_queue_hook_t::on_bind method. As result agent receives a new pointer and this new pointer will be used by agent for enqueuing new demands;
  • the second call to event_queue_hook is performed when agent is being unbound from the dispatcher. Agent passes a pointer to event_queue returned from event_queue_hook_t::on_bind to event_queue_hook_t::on_unbind method.

The event_queue_hook mechanism allows to make specialized wrappers around actual event queues. These wrappers can be used for different tasks. For example for tracing purposes or for gathering some run-time stats.

Please note that this is a low-level mechanism intended to be used for very specific tasks. Because its low-level nature the details and behavior of that mechanism can be changed dramatically in future versions of SObjectizer without any prior notice.

A Simple Example

As a very simple example we provide a rather trivial implementation of event_queue_hook. This implementation wraps every event_queue in a special proxy object:

class demo_event_queue_hook final : public so_5::event_queue_hook_t {
logger & sink_;
public:
demo_event_queue_hook(logger & sink) : sink_{sink} {}
SO_5_NODISCARD
so_5::agent_t * /*agent*/,
so_5::event_queue_t * original_queue ) SO_5_NOEXCEPT override {
return new event_queue_logging_proxy{original_queue, sink_};
}
void on_unbind(
so_5::agent_t * /*agent*/,
so_5::event_queue_t * queue ) SO_5_NOEXCEPT override {
delete queue;
}
};
A base class for agents.
Definition agent.hpp:673
Interface of event_queue_hook object.
virtual event_queue_t * on_bind(agent_t *agent, event_queue_t *original_queue) noexcept=0
A reaction to binding of an agent to some event_queue.
virtual void on_unbind(agent_t *agent, event_queue_t *queue) noexcept=0
A reaction to unbinding of an agent from some event_queue.
An interface of event queue for agent.

Where event_queue_logging_proxy can looks like:

class event_queue_logging_proxy final : public so_5::event_queue_t {
so_5::event_queue_t * actual_queue_;
logger & sink_;
public:
event_queue_logging_proxy(
so_5::event_queue_t * actual_queue,
logger & sink)
: actual_queue_{actual_queue}
, sink_{sink}
{
sink_.log("logging_proxy=", this, " created");
}
~event_queue_logging_proxy() override {
sink_.log("logging_proxy=", this, " destroyed");
}
void push(so_5::execution_demand_t demand) override {
sink_.log("logging_proxy=", this,
", receiver_ptr=", demand.m_receiver,
", mbox_id=", demand.m_mbox_id,
", msg_type=", demand.m_msg_type.name(),
", limit_block=", demand.m_limit,
", msg_ptr=", demand.m_message_ref.get());
actual_queue_->push(std::move(demand));
}
};
virtual void push(execution_demand_t demand)=0
Enqueue new event to the queue.
T * get() const noexcept
A description of event execution demand.
mbox_id_t m_mbox_id
ID of mbox.
agent_t * m_receiver
Receiver of demand.
message_ref_t m_message_ref
Event incident.
const message_limit::control_block_t * m_limit
Optional message limit for that message.
std::type_index m_msg_type
Type of the message.

This event_queue_hook can be passed to SObjectizer Environment this way:

int main() {
logger sink("_event_queue_trace.log");
demo_event_queue_hook hook{sink};
env.introduce_coop([](so_5::coop_t & coop) {
coop.make_agent<parent_agent>();
});
},
params.event_queue_hook(
&hook,
});
}
Agent cooperation.
Definition coop.hpp:389
Agent * make_agent(Args &&... args)
Helper method for simplification of agents creation.
Definition coop.hpp:792
Parameters for the SObjectizer Environment initialization.
SObjectizer Environment.
decltype(auto) introduce_coop(Args &&... args)
Helper method for simplification of cooperation creation and registration.
static void noop_deleter(event_queue_hook_t *) noexcept
An implementation of no-op deleter.
void launch(Init_Routine &&init_routine)
Launch a SObjectizer Environment with default parameters.
Definition api.hpp:142
std::unique_ptr< event_queue_hook_t, event_queue_hook_deleter_fnptr_t > event_queue_hook_unique_ptr_t
Alias for unique pointer to event_queue_hook.

In that case instance of demo_event_queue_hook is created on stack and because of that noop_deleter will be used with event_queue_hook_unique_ptr_t.

We can also create an instance of demo_event_queue_hook as dynamic object. In that case we can write:

int main() {
logger sink("_event_queue_trace.log");
env.introduce_coop([](so_5::coop_t & coop) {
coop.make_agent<parent_agent>();
});
},
params.event_queue_hook(
sink));
});
}
static void default_deleter(event_queue_hook_t *what) noexcept
An implementation of deleter that use operator delete for destroying object of type event_queue_hook.
event_queue_hook_unique_ptr_t make_event_queue_hook(event_queue_hook_deleter_fnptr_t deleter, Args &&...args)
Helper function for simplify creation of event_queue_hook object.