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:
/// @note Only the owning actor is allowed to call this function.
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.
/// @returns The next element in the mailbox or `nullptr` if the mailbox is
/// empty.
......@@ -52,6 +46,10 @@ public:
/// @note Only the owning actor is allowed to call this function.
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.
/// @returns The number of dropped messages.
/// @note Only the owning actor is allowed to call this function.
......@@ -60,6 +58,11 @@ public:
/// Returns the number of pending messages.
/// @note Only the owning actor is allowed to call this function.
virtual size_t size() = 0;
/// Checks whether the mailbox is empty.
bool empty() {
return size() == 0;
}
};
} // namespace caf
This diff is collapsed.
......@@ -4,6 +4,7 @@
#pragma once
#include "caf/abstract_mailbox.hpp"
#include "caf/actor_config.hpp"
#include "caf/actor_traits.hpp"
#include "caf/after.hpp"
......@@ -11,6 +12,7 @@
#include "caf/detail/apply_args.hpp"
#include "caf/detail/blocking_behavior.hpp"
#include "caf/detail/core_export.hpp"
#include "caf/detail/default_mailbox.hpp"
#include "caf/detail/type_list.hpp"
#include "caf/detail/type_traits.hpp"
#include "caf/extend.hpp"
......@@ -18,8 +20,7 @@
#include "caf/intrusive/drr_cached_queue.hpp"
#include "caf/intrusive/drr_queue.hpp"
#include "caf/intrusive/fifo_inbox.hpp"
#include "caf/intrusive/wdrr_dynamic_multiplexed_queue.hpp"
#include "caf/intrusive/wdrr_fixed_multiplexed_queue.hpp"
#include "caf/intrusive/stack.hpp"
#include "caf/is_timeout_or_catch_all.hpp"
#include "caf/local_actor.hpp"
#include "caf/mailbox_element.hpp"
......@@ -29,8 +30,6 @@
#include "caf/none.hpp"
#include "caf/policy/arg.hpp"
#include "caf/policy/categorized.hpp"
#include "caf/policy/normal_messages.hpp"
#include "caf/policy/urgent_messages.hpp"
#include "caf/send.hpp"
#include "caf/typed_actor.hpp"
......@@ -64,29 +63,6 @@ public:
/// Stores asynchronous messages with high priority.
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.
using timeout_type = std::chrono::high_resolution_clock::time_point;
......@@ -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 -------------------------------------------
blocking_actor(actor_config& cfg);
......@@ -344,7 +303,7 @@ public:
virtual mailbox_element_ptr dequeue();
/// Returns the queue for storing incoming messages.
mailbox_type& mailbox() {
abstract_mailbox& mailbox() {
return mailbox_;
}
/// @cond PRIVATE
......@@ -405,6 +364,8 @@ private:
size_t attach_functor(const strong_actor_ptr&);
void unstash();
template <class... Ts>
size_t attach_functor(const typed_actor<Ts...>& x) {
return attach_functor(actor_cast<strong_actor_ptr>(x));
......@@ -420,8 +381,11 @@ private:
// -- member variables -------------------------------------------------------
// used by both event-based and blocking actors
mailbox_type mailbox_;
/// Stores incoming messages.
detail::default_mailbox mailbox_;
/// Stashes skipped messages until the actor processes the next message.
intrusive::stack<mailbox_element> stash_;
};
} // namespace caf
......@@ -62,6 +62,10 @@ bool default_mailbox::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 result = 0;
detail::sync_request_bouncer bounce{reason};
......
......@@ -36,12 +36,12 @@ public:
default_mailbox& operator=(const default_mailbox&) = delete;
mailbox_element* peek(message_id id);
intrusive::inbox_result push_back(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;
bool closed() override;
......@@ -50,6 +50,8 @@ public:
bool try_block() override;
bool try_unblock() override;
size_t close(const error&) override;
size_t size() override;
......
......@@ -181,9 +181,9 @@ bool scheduled_actor::enqueue(mailbox_element_ptr ptr, execution_unit* eu) {
}
mailbox_element* scheduled_actor::peek_at_next_mailbox_element() {
return mailbox().peek(awaited_responses_.empty()
? make_message_id()
: awaited_responses_.begin()->first);
return mailbox_.peek(awaited_responses_.empty()
? make_message_id()
: awaited_responses_.begin()->first);
}
// -- overridden functions of local_actor --------------------------------------
......
......@@ -120,9 +120,6 @@ public:
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.
using pending_response = std::pair<const message_id, behavior>;
......@@ -687,7 +684,7 @@ protected:
// -- member variables -------------------------------------------------------
/// Stores incoming messages.
mailbox_type mailbox_;
detail::default_mailbox mailbox_;
/// Stores user-defined callbacks for message handling.
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