Commit a3cfcbd2 authored by Dominik Charousset's avatar Dominik Charousset

Use the new mailbox type in blocking actors

parent 795f8ad6
...@@ -27,12 +27,6 @@ public: ...@@ -27,12 +27,6 @@ public:
/// @note Only the owning actor is allowed to call this function. /// @note Only the owning actor is allowed to call this function.
virtual void push_front(mailbox_element_ptr ptr) = 0; virtual void push_front(mailbox_element_ptr ptr) = 0;
/// Called by the testing DSL to peek at the next element in the mailbox.
/// @returns A pointer to the next mailbox element or `nullptr` if the
/// mailbox is empty or does not support peeking.
/// @note Only the owning actor is allowed to call this function.
virtual mailbox_element* peek(message_id id = make_message_id()) = 0;
/// Removes the next element from the mailbox. /// Removes the next element from the mailbox.
/// @returns The next element in the mailbox or `nullptr` if the mailbox is /// @returns The next element in the mailbox or `nullptr` if the mailbox is
/// empty. /// empty.
...@@ -52,6 +46,10 @@ public: ...@@ -52,6 +46,10 @@ public:
/// @note Only the owning actor is allowed to call this function. /// @note Only the owning actor is allowed to call this function.
virtual bool try_block() = 0; virtual bool try_block() = 0;
/// Tries to put the mailbox in an empty state from a blocked state.
/// @note Only the owning actor is allowed to call this function.
virtual bool try_unblock() = 0;
/// Closes the mailbox and discards all pending messages. /// Closes the mailbox and discards all pending messages.
/// @returns The number of dropped messages. /// @returns The number of dropped messages.
/// @note Only the owning actor is allowed to call this function. /// @note Only the owning actor is allowed to call this function.
...@@ -60,6 +58,11 @@ public: ...@@ -60,6 +58,11 @@ public:
/// Returns the number of pending messages. /// Returns the number of pending messages.
/// @note Only the owning actor is allowed to call this function. /// @note Only the owning actor is allowed to call this function.
virtual size_t size() = 0; virtual size_t size() = 0;
/// Checks whether the mailbox is empty.
bool empty() {
return size() == 0;
}
}; };
} // namespace caf } // namespace caf
This diff is collapsed.
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#pragma once #pragma once
#include "caf/abstract_mailbox.hpp"
#include "caf/actor_config.hpp" #include "caf/actor_config.hpp"
#include "caf/actor_traits.hpp" #include "caf/actor_traits.hpp"
#include "caf/after.hpp" #include "caf/after.hpp"
...@@ -11,6 +12,7 @@ ...@@ -11,6 +12,7 @@
#include "caf/detail/apply_args.hpp" #include "caf/detail/apply_args.hpp"
#include "caf/detail/blocking_behavior.hpp" #include "caf/detail/blocking_behavior.hpp"
#include "caf/detail/core_export.hpp" #include "caf/detail/core_export.hpp"
#include "caf/detail/default_mailbox.hpp"
#include "caf/detail/type_list.hpp" #include "caf/detail/type_list.hpp"
#include "caf/detail/type_traits.hpp" #include "caf/detail/type_traits.hpp"
#include "caf/extend.hpp" #include "caf/extend.hpp"
...@@ -18,8 +20,7 @@ ...@@ -18,8 +20,7 @@
#include "caf/intrusive/drr_cached_queue.hpp" #include "caf/intrusive/drr_cached_queue.hpp"
#include "caf/intrusive/drr_queue.hpp" #include "caf/intrusive/drr_queue.hpp"
#include "caf/intrusive/fifo_inbox.hpp" #include "caf/intrusive/fifo_inbox.hpp"
#include "caf/intrusive/wdrr_dynamic_multiplexed_queue.hpp" #include "caf/intrusive/stack.hpp"
#include "caf/intrusive/wdrr_fixed_multiplexed_queue.hpp"
#include "caf/is_timeout_or_catch_all.hpp" #include "caf/is_timeout_or_catch_all.hpp"
#include "caf/local_actor.hpp" #include "caf/local_actor.hpp"
#include "caf/mailbox_element.hpp" #include "caf/mailbox_element.hpp"
...@@ -29,8 +30,6 @@ ...@@ -29,8 +30,6 @@
#include "caf/none.hpp" #include "caf/none.hpp"
#include "caf/policy/arg.hpp" #include "caf/policy/arg.hpp"
#include "caf/policy/categorized.hpp" #include "caf/policy/categorized.hpp"
#include "caf/policy/normal_messages.hpp"
#include "caf/policy/urgent_messages.hpp"
#include "caf/send.hpp" #include "caf/send.hpp"
#include "caf/typed_actor.hpp" #include "caf/typed_actor.hpp"
...@@ -64,29 +63,6 @@ public: ...@@ -64,29 +63,6 @@ public:
/// Stores asynchronous messages with high priority. /// Stores asynchronous messages with high priority.
using urgent_queue = intrusive::drr_cached_queue<policy::urgent_messages>; using urgent_queue = intrusive::drr_cached_queue<policy::urgent_messages>;
/// Configures the FIFO inbox with two nested queues:
///
/// 1. Default asynchronous messages
/// 2. High-priority asynchronous messages
struct mailbox_policy {
using deficit_type = size_t;
using mapped_type = mailbox_element;
using unique_pointer = mailbox_element_ptr;
using queue_type
= intrusive::wdrr_fixed_multiplexed_queue<policy::categorized,
normal_queue, urgent_queue>;
static constexpr size_t normal_queue_index = 0;
static constexpr size_t urgent_queue_index = 1;
};
/// A queue optimized for single-reader-many-writers.
using mailbox_type = intrusive::fifo_inbox<mailbox_policy>;
/// Absolute timeout type. /// Absolute timeout type.
using timeout_type = std::chrono::high_resolution_clock::time_point; using timeout_type = std::chrono::high_resolution_clock::time_point;
...@@ -193,23 +169,6 @@ public: ...@@ -193,23 +169,6 @@ public:
} }
}; };
struct mailbox_visitor {
blocking_actor* self;
bool& done;
receive_cond& rcc;
message_id mid;
detail::blocking_behavior& bhvr;
// Dispatches messages with high and normal priority to the same handler.
template <class Queue>
intrusive::task_result operator()(size_t, Queue&, mailbox_element& x) {
return (*this)(x);
}
// Consumes `x`.
intrusive::task_result operator()(mailbox_element& x);
};
// -- constructors and destructors ------------------------------------------- // -- constructors and destructors -------------------------------------------
blocking_actor(actor_config& cfg); blocking_actor(actor_config& cfg);
...@@ -344,7 +303,7 @@ public: ...@@ -344,7 +303,7 @@ public:
virtual mailbox_element_ptr dequeue(); virtual mailbox_element_ptr dequeue();
/// Returns the queue for storing incoming messages. /// Returns the queue for storing incoming messages.
mailbox_type& mailbox() { abstract_mailbox& mailbox() {
return mailbox_; return mailbox_;
} }
/// @cond PRIVATE /// @cond PRIVATE
...@@ -405,6 +364,8 @@ private: ...@@ -405,6 +364,8 @@ private:
size_t attach_functor(const strong_actor_ptr&); size_t attach_functor(const strong_actor_ptr&);
void unstash();
template <class... Ts> template <class... Ts>
size_t attach_functor(const typed_actor<Ts...>& x) { size_t attach_functor(const typed_actor<Ts...>& x) {
return attach_functor(actor_cast<strong_actor_ptr>(x)); return attach_functor(actor_cast<strong_actor_ptr>(x));
...@@ -420,8 +381,11 @@ private: ...@@ -420,8 +381,11 @@ private:
// -- member variables ------------------------------------------------------- // -- member variables -------------------------------------------------------
// used by both event-based and blocking actors /// Stores incoming messages.
mailbox_type mailbox_; detail::default_mailbox mailbox_;
/// Stashes skipped messages until the actor processes the next message.
intrusive::stack<mailbox_element> stash_;
}; };
} // namespace caf } // namespace caf
...@@ -62,6 +62,10 @@ bool default_mailbox::try_block() { ...@@ -62,6 +62,10 @@ bool default_mailbox::try_block() {
return cached() == 0 && inbox_.try_block(); return cached() == 0 && inbox_.try_block();
} }
bool default_mailbox::try_unblock() {
return inbox_.try_unblock();
}
size_t default_mailbox::close(const error& reason) { size_t default_mailbox::close(const error& reason) {
size_t result = 0; size_t result = 0;
detail::sync_request_bouncer bounce{reason}; detail::sync_request_bouncer bounce{reason};
......
...@@ -36,12 +36,12 @@ public: ...@@ -36,12 +36,12 @@ public:
default_mailbox& operator=(const default_mailbox&) = delete; default_mailbox& operator=(const default_mailbox&) = delete;
mailbox_element* peek(message_id id);
intrusive::inbox_result push_back(mailbox_element_ptr ptr) override; intrusive::inbox_result push_back(mailbox_element_ptr ptr) override;
void push_front(mailbox_element_ptr ptr) override; void push_front(mailbox_element_ptr ptr) override;
mailbox_element* peek(message_id id) override;
mailbox_element_ptr pop_front() override; mailbox_element_ptr pop_front() override;
bool closed() override; bool closed() override;
...@@ -50,6 +50,8 @@ public: ...@@ -50,6 +50,8 @@ public:
bool try_block() override; bool try_block() override;
bool try_unblock() override;
size_t close(const error&) override; size_t close(const error&) override;
size_t size() override; size_t size() override;
......
...@@ -181,7 +181,7 @@ bool scheduled_actor::enqueue(mailbox_element_ptr ptr, execution_unit* eu) { ...@@ -181,7 +181,7 @@ bool scheduled_actor::enqueue(mailbox_element_ptr ptr, execution_unit* eu) {
} }
mailbox_element* scheduled_actor::peek_at_next_mailbox_element() { mailbox_element* scheduled_actor::peek_at_next_mailbox_element() {
return mailbox().peek(awaited_responses_.empty() return mailbox_.peek(awaited_responses_.empty()
? make_message_id() ? make_message_id()
: awaited_responses_.begin()->first); : awaited_responses_.begin()->first);
} }
......
...@@ -120,9 +120,6 @@ public: ...@@ -120,9 +120,6 @@ public:
size_t max_items_per_batch; size_t max_items_per_batch;
}; };
/// A queue optimized for single-reader-many-writers.
using mailbox_type = detail::default_mailbox;
/// The message ID of an outstanding response with its callback. /// The message ID of an outstanding response with its callback.
using pending_response = std::pair<const message_id, behavior>; using pending_response = std::pair<const message_id, behavior>;
...@@ -687,7 +684,7 @@ protected: ...@@ -687,7 +684,7 @@ protected:
// -- member variables ------------------------------------------------------- // -- member variables -------------------------------------------------------
/// Stores incoming messages. /// Stores incoming messages.
mailbox_type mailbox_; detail::default_mailbox mailbox_;
/// Stores user-defined callbacks for message handling. /// Stores user-defined callbacks for message handling.
detail::behavior_stack bhvr_stack_; detail::behavior_stack bhvr_stack_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment