libcaf  0.15.0
Public Types | Public Member Functions | Protected Member Functions | List of all members
caf::blocking_actor Class Reference

A thread-mapped or context-switching actor using a blocking receive rather than a behavior-stack based message processing. More...

#include <blocking_actor.hpp>

Inheritance diagram for caf::blocking_actor:
caf::local_actor caf::monitorable_actor caf::resumable caf::abstract_actor caf::abstract_channel

Public Types

using super = extend< local_actor, blocking_actor >::with< mixin::requester, mixin::sender >
 
using behavior_type = behavior
 
using signatures = none_t
 
using timeout_type = std::chrono::high_resolution_clock::time_point
 
- Public Types inherited from caf::local_actor
using mailbox_type = detail::single_reader_queue< mailbox_element, detail::disposer >
 
using default_handler = std::function< result< message >(local_actor *self, const type_erased_tuple *)>
 Function object for handling unmatched messages.
 
using error_handler = std::function< void(local_actor *self, error &)>
 Function object for handling error messages.
 
using down_handler = std::function< void(local_actor *self, down_msg &)>
 Function object for handling down messages.
 
using exit_handler = std::function< void(local_actor *self, exit_msg &)>
 Function object for handling exit messages.
 
- Public Types inherited from caf::resumable
enum  resume_result {
  resume_later,
  awaiting_message,
  done,
  shutdown_execution_unit
}
 Denotes the state in which a resumable returned from its last call to resume. More...
 
enum  subtype_t {
  unspecified,
  scheduled_actor,
  io_actor,
  function_object
}
 Denotes common subtypes of resumable. More...
 

Public Member Functions

 blocking_actor (actor_config &sys)
 
void enqueue (mailbox_element_ptr, execution_unit *) override
 Enqueues a new message wrapped in a mailbox_element to the actor. More...
 
template<class... Ts>
void receive (Ts &&...xs)
 Dequeues the next message from the mailbox that is matched by given behavior. More...
 
template<class... Ts>
void receive_loop (Ts &&...xs)
 Semantically equal to: for (;;) { receive(...); }, but does not cause a temporary behavior object per iteration. More...
 
template<class T >
receive_for_helper< T > receive_for (T &begin, const T &end)
 Receives messages for range [begin, first). More...
 
template<class Statement >
receive_while_helper receive_while (Statement stmt)
 Receives messages as long as stmt returns true. More...
 
template<class... Ts>
do_receive_helper do_receive (Ts &&...xs)
 Receives messages until stmt returns true. More...
 
void await_all_other_actors_done ()
 Blocks this actor until all other actors are done.
 
virtual void act ()
 Implements the actor's behavior.
 
template<class... Ts>
void wait_for (Ts &&...xs)
 Blocks this actor until all xs... have terminated.
 
- Public Member Functions inherited from caf::local_actor
void on_destroy () override
 Cleans up any remaining state before the destructor is called. More...
 
template<class T , spawn_options Os = no_spawn_options, class... Ts>
infer_handle_from_class< T >::type spawn (Ts &&...xs)
 
template<spawn_options Os = no_spawn_options, class F , class... Ts>
infer_handle_from_fun< F >::type spawn (F fun, Ts &&...xs)
 
template<class T , spawn_options Os = no_spawn_options, class Groups , class... Ts>
actor spawn_in_groups (const Groups &gs, Ts &&...xs)
 
template<class T , spawn_options Os = no_spawn_options, class... Ts>
actor spawn_in_groups (std::initializer_list< group > gs, Ts &&...xs)
 
template<class T , spawn_options Os = no_spawn_options, class... Ts>
actor spawn_in_group (const group &grp, Ts &&...xs)
 
template<spawn_options Os = no_spawn_options, class Groups , class F , class... Ts>
actor spawn_in_groups (const Groups &gs, F fun, Ts &&...xs)
 
template<spawn_options Os = no_spawn_options, class F , class... Ts>
actor spawn_in_groups (std::initializer_list< group > gs, F fun, Ts &&...xs)
 
template<spawn_options Os = no_spawn_options, class F , class... Ts>
actor spawn_in_group (const group &grp, F fun, Ts &&...xs)
 
void send_exit (const actor_addr &dest, error reason)
 Sends an exit message to dest.
 
void send_exit (const strong_actor_ptr &dest, error reason)
 
template<class ActorHandle >
void send_exit (const ActorHandle &dest, error reason)
 Sends an exit message to dest.
 
void set_default_handler (default_handler fun)
 Sets a custom handler for unexpected messages.
 
void set_error_handler (error_handler fun)
 Sets a custom handler for error messages.
 
template<class T >
auto set_error_handler (T fun) -> decltype(fun(std::declval< error & >()))
 Sets a custom handler for error messages.
 
void set_down_handler (down_handler fun)
 Sets a custom handler for down messages.
 
template<class T >
auto set_down_handler (T fun) -> decltype(fun(std::declval< down_msg & >()))
 Sets a custom handler for down messages.
 
void set_exit_handler (exit_handler fun)
 Sets a custom handler for error messages.
 
template<class T >
auto set_exit_handler (T fun) -> decltype(fun(std::declval< exit_msg & >()))
 Sets a custom handler for exit messages.
 
execution_unitcontext () const
 Returns the execution unit currently used by this actor.
 
void context (execution_unit *x)
 Sets the execution unit for this actor.
 
actor_system & system () const
 Returns the hosting actor system.
 
void join (const group &what)
 Causes this actor to subscribe to the group what. More...
 
void leave (const group &what)
 Causes this actor to leave the group what.
 
void quit (error reason=error{})
 Finishes execution of this actor after any currently running message handler is done. More...
 
strong_actor_ptr current_sender ()
 Returns a pointer to the sender of the current message.
 
template<class Handle >
void monitor (const Handle &whom)
 Adds a unidirectional monitor to whom. More...
 
void demonitor (const actor_addr &whom)
 Removes a monitor from whom.
 
void demonitor (const actor &whom)
 Removes a monitor from whom.
 
virtual void on_exit ()
 Can be overridden to perform cleanup code after an actor finished execution. More...
 
std::vector< group > joined_groups () const
 Returns all joined groups.
 
template<class... Ts>
detail::make_response_promise_helper< Ts...>::type make_response_promise ()
 Creates a typed_response_promise to respond to a request later on. More...
 
response_promise make_response_promise ()
 Creates a response_promise to respond to a request later on.
 
template<class... Ts, class R = typename detail::make_response_promise_helper< typename std::decay<Ts>::type... >::type>
response (Ts &&...xs)
 Creates a typed_response_promise and responds immediately. More...
 
template<class F >
void set_exception_handler (F f)
 Sets a custom exception handler for this actor. More...
 
const char * name () const override
 Returns an implementation-dependent name for logging purposes, which is only valid as long as the actor is running. More...
 
virtual void save_state (serializer &sink, const unsigned int version)
 Serializes the state of this actor to sink. More...
 
virtual void load_state (deserializer &source, const unsigned int version)
 Deserializes the state of this actor from source. More...
 
subtype_t subtype () const override
 Returns a subtype hint for this object. More...
 
resume_result resume (execution_unit *, size_t) override
 Resume any pending computation until it is either finished or needs to be re-scheduled later. More...
 
- Public Member Functions inherited from caf::monitorable_actor
void attach (attachable_ptr ptr) override
 Attaches ptr to this actor. More...
 
size_t detach (const attachable::token &what) override
 Detaches the first attached object that matches what.
 
void link_to (const actor_addr &x)
 Links this actor to x.
 
template<class ActorHandle >
void link_to (const ActorHandle &x)
 Links this actor to x.
 
void unlink_from (const actor_addr &x)
 Unlinks this actor from x.
 
template<class ActorHandle >
void unlink_from (const ActorHandle &x)
 Links this actor to x.
 
- Public Member Functions inherited from caf::abstract_actor
void * operator new (std::size_t, void *ptr)
 
actor_control_blockctrl () const
 
void enqueue (strong_actor_ptr sender, message_id mid, message content, execution_unit *host) override
 Enqueues a new message without forwarding stack to the channel.
 
template<class F >
void attach_functor (F f)
 Convenience function that attaches the functor f to this actor. More...
 
actor_addr address () const
 Returns the logical actor address.
 
bool establish_backlink (abstract_actor *x)
 Establishes a link relation between this actor and x and returns whether the operation succeeded. More...
 
bool remove_backlink (abstract_actor *x)
 Removes the link relation between this actor and x and returns whether the operation succeeded. More...
 
virtual std::set< std::string > message_types () const
 Returns the set of accepted messages types as strings or an empty set if this actor is untyped. More...
 
actor_id id () const noexcept
 Returns the ID of this actor.
 
node_id node () const noexcept
 Returns the node this actor is living on.
 
actor_system & home_system () const noexcept
 Returns the system that created this actor (or proxy).
 
- Public Member Functions inherited from caf::abstract_channel
bool is_abstract_actor () const
 
bool is_abstract_group () const
 
bool is_actor_decorator () const
 
- Public Member Functions inherited from caf::resumable
virtual void intrusive_ptr_add_ref_impl ()=0
 Add a strong reference count to this object.
 
virtual void intrusive_ptr_release_impl ()=0
 Remove a strong reference count from this object.
 

Protected Member Functions

std::function< void(behavior &)> make_dequeue_callback ()
 
- Protected Member Functions inherited from caf::monitorable_actor
virtual void on_cleanup ()
 Allows subclasses to add additional cleanup code to the critical secion in cleanup. More...
 
void bounce (mailbox_element_ptr &what)
 Sends a response message if what is a request.
 
void bounce (mailbox_element_ptr &what, const error &err)
 Sends a response message if what is a request.
 
 monitorable_actor (actor_config &cfg)
 Creates a new actor instance.
 
bool link_impl (linking_operation op, abstract_actor *x) override
 
bool establish_link_impl (abstract_actor *other)
 
bool remove_link_impl (abstract_actor *other)
 
bool establish_backlink_impl (abstract_actor *other)
 
bool remove_backlink_impl (abstract_actor *other)
 
optional< exit_reasonhandle (const std::exception_ptr &eptr)
 
void attach_impl (attachable_ptr &ptr)
 
bool handle_system_message (mailbox_element &node, execution_unit *context, bool trap_exit)
 
template<class F >
bool handle_system_message (mailbox_element &x, execution_unit *context, bool trap_exit, F &down_msg_handler)
 
template<class F >
auto exclusive_critical_section (F fun) -> decltype(fun())
 
template<class F >
auto shared_critical_section (F fun) -> decltype(fun())
 
- Protected Member Functions inherited from caf::abstract_actor
 abstract_actor (actor_config &cfg)
 Creates a new actor instance.
 
- Protected Member Functions inherited from caf::abstract_channel
int flags () const
 
void flags (int new_value)
 

Additional Inherited Members

- Static Public Attributes inherited from caf::abstract_channel
static constexpr int is_abstract_actor_flag = 0x01000000
 
static constexpr int is_abstract_group_flag = 0x02000000
 
static constexpr int is_actor_bind_decorator_flag = 0x04000000
 
static constexpr int is_actor_dot_decorator_flag = 0x08000000
 
static constexpr int is_actor_decorator_mask = 0x0C000000
 
- Static Protected Member Functions inherited from caf::monitorable_actor
static size_t detach_impl (const attachable::token &what, attachable_ptr &ptr, bool stop_on_first_hit=false, bool dry_run=false)
 
- Protected Attributes inherited from caf::monitorable_actor
error fail_state_
 
std::mutex mtx_
 
std::condition_variable cv_
 
attachable_ptr attachables_head_
 

Detailed Description

A thread-mapped or context-switching actor using a blocking receive rather than a behavior-stack based message processing.

Member Function Documentation

template<class... Ts>
do_receive_helper caf::blocking_actor::do_receive ( Ts &&...  xs)

Receives messages until stmt returns true.

Semantically equal to: do { receive(...); } while (stmt() == false);

Usage example:

int i = 0;
(
on<int>() >> int_fun,
on<float>() >> float_fun
)
.until([&]() { return (++i >= 10); };
void caf::blocking_actor::enqueue ( mailbox_element_ptr  what,
execution_unit host 
)
overridevirtual

Enqueues a new message wrapped in a mailbox_element to the actor.

This enqueue variant allows to define forwarding chains.

Implements caf::abstract_actor.

template<class... Ts>
void caf::blocking_actor::receive ( Ts &&...  xs)

Dequeues the next message from the mailbox that is matched by given behavior.

template<class T >
receive_for_helper<T> caf::blocking_actor::receive_for ( T &  begin,
const T &  end 
)

Receives messages for range [begin, first).

Semantically equal to: for ( ; begin != end; ++begin) { receive(...); }.

Usage example:

int i = 0;
receive_for(i, 10) (
[&](get_atom) {
return i;
}
);
template<class... Ts>
void caf::blocking_actor::receive_loop ( Ts &&...  xs)

Semantically equal to: for (;;) { receive(...); }, but does not cause a temporary behavior object per iteration.

template<class Statement >
receive_while_helper caf::blocking_actor::receive_while ( Statement  stmt)

Receives messages as long as stmt returns true.

Semantically equal to: while (stmt()) { receive(...); }.

Usage example:

int i = 0;
receive_while([&]() { return (++i <= 10); })
(
on<int>() >> int_fun,
on<float>() >> float_fun
);

The documentation for this class was generated from the following file: