Commit 656499e5 authored by Dominik Charousset's avatar Dominik Charousset

Simplify calls to CAF_AFTER_PROCESSING

parent e608ad30
...@@ -594,6 +594,14 @@ bool operator==(const logger::field& x, const logger::field& y); ...@@ -594,6 +594,14 @@ bool operator==(const logger::field& x, const logger::field& y);
#define CAF_LOG_FINALIZE_EVENT() \ #define CAF_LOG_FINALIZE_EVENT() \
CAF_LOG_IMPL(CAF_LOG_FLOW_COMPONENT, CAF_LOG_LEVEL_DEBUG, "FINALIZE") CAF_LOG_IMPL(CAF_LOG_FLOW_COMPONENT, CAF_LOG_LEVEL_DEBUG, "FINALIZE")
#define CAF_LOG_SKIP_OR_FINALIZE_EVENT(invoke_result) \
do { \
if (invoke_result == caf::invoke_message_result::skipped) \
CAF_LOG_SKIP_EVENT(); \
else \
CAF_LOG_FINALIZE_EVENT(); \
} while (false)
#define CAF_LOG_TERMINATE_EVENT(thisptr, rsn) \ #define CAF_LOG_TERMINATE_EVENT(thisptr, rsn) \
CAF_LOG_IMPL(CAF_LOG_FLOW_COMPONENT, CAF_LOG_LEVEL_DEBUG, \ CAF_LOG_IMPL(CAF_LOG_FLOW_COMPONENT, CAF_LOG_LEVEL_DEBUG, \
"TERMINATE ; ID =" << thisptr->id() \ "TERMINATE ; ID =" << thisptr->id() \
...@@ -616,6 +624,8 @@ bool operator==(const logger::field& x, const logger::field& y); ...@@ -616,6 +624,8 @@ bool operator==(const logger::field& x, const logger::field& y);
#define CAF_LOG_SKIP_EVENT() CAF_VOID_STMT #define CAF_LOG_SKIP_EVENT() CAF_VOID_STMT
#define CAF_LOG_SKIP_OR_FINALIZE_EVENT(unused) CAF_VOID_STMT
#define CAF_LOG_FINALIZE_EVENT() CAF_VOID_STMT #define CAF_LOG_FINALIZE_EVENT() CAF_VOID_STMT
#define CAF_LOG_TERMINATE_EVENT(thisptr, rsn) CAF_VOID_STMT #define CAF_LOG_TERMINATE_EVENT(thisptr, rsn) CAF_VOID_STMT
......
...@@ -157,10 +157,12 @@ void blocking_actor::fail_state(error err) { ...@@ -157,10 +157,12 @@ void blocking_actor::fail_state(error err) {
} }
intrusive::task_result intrusive::task_result
blocking_actor::mailbox_visitor:: operator()(mailbox_element& x) { blocking_actor::mailbox_visitor::operator()(mailbox_element& x) {
CAF_LOG_TRACE(CAF_ARG(x)); CAF_LOG_TRACE(CAF_ARG(x));
CAF_LOG_RECEIVE_EVENT((&x)); CAF_LOG_RECEIVE_EVENT((&x));
CAF_BEFORE_PROCESSING(self, x); CAF_BEFORE_PROCESSING(self, x);
// Wrap the actual body for the function.
auto body = [this, &x] {
auto check_if_done = [&]() -> intrusive::task_result { auto check_if_done = [&]() -> intrusive::task_result {
// Stop consuming items when reaching the end of the user-defined receive // Stop consuming items when reaching the end of the user-defined receive
// loop either via post or pre condition. // loop either via post or pre condition.
...@@ -172,13 +174,9 @@ blocking_actor::mailbox_visitor:: operator()(mailbox_element& x) { ...@@ -172,13 +174,9 @@ blocking_actor::mailbox_visitor:: operator()(mailbox_element& x) {
// Skip messages that don't match our message ID. // Skip messages that don't match our message ID.
if (mid.is_response()) { if (mid.is_response()) {
if (mid != x.mid) { if (mid != x.mid) {
CAF_AFTER_PROCESSING(self, invoke_message_result::skipped);
CAF_LOG_SKIP_EVENT();
return intrusive::task_result::skip; return intrusive::task_result::skip;
} }
} else if (x.mid.is_response()) { } else if (x.mid.is_response()) {
CAF_AFTER_PROCESSING(self, invoke_message_result::skipped);
CAF_LOG_SKIP_EVENT();
return intrusive::task_result::skip; return intrusive::task_result::skip;
} }
// Automatically unlink from actors after receiving an exit. // Automatically unlink from actors after receiving an exit.
...@@ -187,22 +185,18 @@ blocking_actor::mailbox_visitor:: operator()(mailbox_element& x) { ...@@ -187,22 +185,18 @@ blocking_actor::mailbox_visitor:: operator()(mailbox_element& x) {
// Blocking actors can nest receives => push/pop `current_element_` // Blocking actors can nest receives => push/pop `current_element_`
auto prev_element = self->current_element_; auto prev_element = self->current_element_;
self->current_element_ = &x; self->current_element_ = &x;
auto g = detail::make_scope_guard([&] { auto g = detail::make_scope_guard(
self->current_element_ = prev_element; [&] { self->current_element_ = prev_element; });
});
// Dispatch on x. // Dispatch on x.
detail::default_invoke_result_visitor<blocking_actor> visitor{self}; detail::default_invoke_result_visitor<blocking_actor> visitor{self};
switch (bhvr.nested(visitor, x.content())) { switch (bhvr.nested(visitor, x.content())) {
default: default:
CAF_AFTER_PROCESSING(self, invoke_message_result::consumed);
return check_if_done(); return check_if_done();
case match_case::no_match: case match_case::no_match: { // Blocking actors can have fallback handlers
{ // Blocking actors can have fallback handlers for catch-all rules. // for catch-all rules.
auto sres = bhvr.fallback(*self->current_element_); auto sres = bhvr.fallback(*self->current_element_);
if (sres.flag != rt_skip) { if (sres.flag != rt_skip) {
CAF_AFTER_PROCESSING(self, invoke_message_result::consumed);
visitor.visit(sres); visitor.visit(sres);
CAF_LOG_FINALIZE_EVENT();
return check_if_done(); return check_if_done();
} }
} }
...@@ -215,16 +209,23 @@ blocking_actor::mailbox_visitor:: operator()(mailbox_element& x) { ...@@ -215,16 +209,23 @@ blocking_actor::mailbox_visitor:: operator()(mailbox_element& x) {
std::move(x.stages), err}; std::move(x.stages), err};
self->current_element_ = &tmp; self->current_element_ = &tmp;
bhvr.nested(tmp.content()); bhvr.nested(tmp.content());
CAF_AFTER_PROCESSING(self, invoke_message_result::consumed);
CAF_LOG_FINALIZE_EVENT();
return check_if_done(); return check_if_done();
} }
CAF_ANNOTATE_FALLTHROUGH; CAF_ANNOTATE_FALLTHROUGH;
case match_case::skip: case match_case::skip:
return intrusive::task_result::skip;
}
};
// Post-process the returned value from the function body.
auto result = body();
if (result == intrusive::task_result::skip) {
CAF_AFTER_PROCESSING(self, invoke_message_result::skipped); CAF_AFTER_PROCESSING(self, invoke_message_result::skipped);
CAF_LOG_SKIP_EVENT(); CAF_LOG_SKIP_EVENT();
return intrusive::task_result::skip; } else {
CAF_AFTER_PROCESSING(self, invoke_message_result::consumed);
CAF_LOG_FINALIZE_EVENT();
} }
return result;
} }
void blocking_actor::receive_impl(receive_cond& rcc, void blocking_actor::receive_impl(receive_cond& rcc,
......
...@@ -32,6 +32,8 @@ invoke_message_result raw_event_based_actor::consume(mailbox_element& x) { ...@@ -32,6 +32,8 @@ invoke_message_result raw_event_based_actor::consume(mailbox_element& x) {
current_element_ = &x; current_element_ = &x;
CAF_LOG_RECEIVE_EVENT(current_element_); CAF_LOG_RECEIVE_EVENT(current_element_);
CAF_BEFORE_PROCESSING(this, x); CAF_BEFORE_PROCESSING(this, x);
// Wrap the actual body for the function.
auto body = [this, &x] {
// short-circuit awaited responses // short-circuit awaited responses
if (!awaited_responses_.empty()) { if (!awaited_responses_.empty()) {
auto& pr = awaited_responses_.front(); auto& pr = awaited_responses_.front();
...@@ -45,17 +47,14 @@ invoke_message_result raw_event_based_actor::consume(mailbox_element& x) { ...@@ -45,17 +47,14 @@ invoke_message_result raw_event_based_actor::consume(mailbox_element& x) {
pr.second(msg); pr.second(msg);
} }
awaited_responses_.pop_front(); awaited_responses_.pop_front();
CAF_AFTER_PROCESSING(this, invoke_message_result::consumed);
return invoke_message_result::consumed; return invoke_message_result::consumed;
} }
// handle multiplexed responses // handle multiplexed responses
if (x.mid.is_response()) { if (x.mid.is_response()) {
auto mrh = multiplexed_responses_.find(x.mid); auto mrh = multiplexed_responses_.find(x.mid);
// neither awaited nor multiplexed, probably an expired timeout // neither awaited nor multiplexed, probably an expired timeout
if (mrh == multiplexed_responses_.end()) { if (mrh == multiplexed_responses_.end())
CAF_AFTER_PROCESSING(this, invoke_message_result::dropped);
return invoke_message_result::dropped; return invoke_message_result::dropped;
}
if (!mrh->second(x.content())) { if (!mrh->second(x.content())) {
// try again with error if first attempt failed // try again with error if first attempt failed
auto msg = make_message( auto msg = make_message(
...@@ -63,7 +62,6 @@ invoke_message_result raw_event_based_actor::consume(mailbox_element& x) { ...@@ -63,7 +62,6 @@ invoke_message_result raw_event_based_actor::consume(mailbox_element& x) {
mrh->second(msg); mrh->second(msg);
} }
multiplexed_responses_.erase(mrh); multiplexed_responses_.erase(mrh);
CAF_AFTER_PROCESSING(this, invoke_message_result::consumed);
return invoke_message_result::consumed; return invoke_message_result::consumed;
} }
auto& content = x.content(); auto& content = x.content();
...@@ -74,16 +72,12 @@ invoke_message_result raw_event_based_actor::consume(mailbox_element& x) { ...@@ -74,16 +72,12 @@ invoke_message_result raw_event_based_actor::consume(mailbox_element& x) {
CAF_ASSERT(x.mid.is_async()); CAF_ASSERT(x.mid.is_async());
if (is_active_receive_timeout(tid)) { if (is_active_receive_timeout(tid)) {
CAF_LOG_DEBUG("handle timeout message"); CAF_LOG_DEBUG("handle timeout message");
if (bhvr_stack_.empty()) { if (bhvr_stack_.empty())
CAF_AFTER_PROCESSING(this, invoke_message_result::dropped);
return invoke_message_result::dropped; return invoke_message_result::dropped;
}
bhvr_stack_.back().handle_timeout(); bhvr_stack_.back().handle_timeout();
CAF_AFTER_PROCESSING(this, invoke_message_result::consumed);
return invoke_message_result::consumed; return invoke_message_result::consumed;
} }
CAF_LOG_DEBUG("dropped expired timeout message"); CAF_LOG_DEBUG("dropped expired timeout message");
CAF_AFTER_PROCESSING(this, invoke_message_result::dropped);
return invoke_message_result::dropped; return invoke_message_result::dropped;
} }
// handle everything else as ordinary message // handle everything else as ordinary message
...@@ -112,10 +106,8 @@ invoke_message_result raw_event_based_actor::consume(mailbox_element& x) { ...@@ -112,10 +106,8 @@ invoke_message_result raw_event_based_actor::consume(mailbox_element& x) {
}; };
if (bhvr_stack_.empty()) { if (bhvr_stack_.empty()) {
call_default_handler(); call_default_handler();
auto result = !skipped ? invoke_message_result::consumed return !skipped ? invoke_message_result::consumed
: invoke_message_result::skipped; : invoke_message_result::skipped;
CAF_AFTER_PROCESSING(this, result);
return result;
} }
auto& bhvr = bhvr_stack_.back(); auto& bhvr = bhvr_stack_.back();
switch (bhvr(visitor, x.content())) { switch (bhvr(visitor, x.content())) {
...@@ -127,9 +119,13 @@ invoke_message_result raw_event_based_actor::consume(mailbox_element& x) { ...@@ -127,9 +119,13 @@ invoke_message_result raw_event_based_actor::consume(mailbox_element& x) {
case match_case::no_match: case match_case::no_match:
call_default_handler(); call_default_handler();
} }
auto result = !skipped ? invoke_message_result::consumed return !skipped ? invoke_message_result::consumed
: invoke_message_result::skipped; : invoke_message_result::skipped;
};
// Post-process the returned value from the function body.
auto result = body();
CAF_AFTER_PROCESSING(this, result); CAF_AFTER_PROCESSING(this, result);
CAF_LOG_SKIP_OR_FINALIZE_EVENT(result);
return result; return result;
} }
......
...@@ -630,6 +630,8 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) { ...@@ -630,6 +630,8 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) {
current_element_ = &x; current_element_ = &x;
CAF_LOG_RECEIVE_EVENT(current_element_); CAF_LOG_RECEIVE_EVENT(current_element_);
CAF_BEFORE_PROCESSING(this, x); CAF_BEFORE_PROCESSING(this, x);
// Wrap the actual body for the function.
auto body = [this, &x] {
// Helper function for dispatching a message to a response handler. // Helper function for dispatching a message to a response handler.
using ptr_t = scheduled_actor*; using ptr_t = scheduled_actor*;
using fun_t = bool (*)(ptr_t, behavior&, mailbox_element&); using fun_t = bool (*)(ptr_t, behavior&, mailbox_element&);
...@@ -642,10 +644,8 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) { ...@@ -642,10 +644,8 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) {
auto invoke = select_invoke_fun(); auto invoke = select_invoke_fun();
auto& pr = awaited_responses_.front(); auto& pr = awaited_responses_.front();
// skip all messages until we receive the currently awaited response // skip all messages until we receive the currently awaited response
if (x.mid != pr.first) { if (x.mid != pr.first)
CAF_AFTER_PROCESSING(this, invoke_message_result::skipped);
return invoke_message_result::skipped; return invoke_message_result::skipped;
}
auto f = std::move(pr.second); auto f = std::move(pr.second);
awaited_responses_.pop_front(); awaited_responses_.pop_front();
if (!invoke(this, f, x)) { if (!invoke(this, f, x)) {
...@@ -654,7 +654,6 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) { ...@@ -654,7 +654,6 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) {
make_error(sec::unexpected_response, x.move_content_to_message())); make_error(sec::unexpected_response, x.move_content_to_message()));
f(msg); f(msg);
} }
CAF_AFTER_PROCESSING(this, invoke_message_result::consumed);
return invoke_message_result::consumed; return invoke_message_result::consumed;
} }
// Handle multiplexed responses. // Handle multiplexed responses.
...@@ -662,10 +661,8 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) { ...@@ -662,10 +661,8 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) {
auto invoke = select_invoke_fun(); auto invoke = select_invoke_fun();
auto mrh = multiplexed_responses_.find(x.mid); auto mrh = multiplexed_responses_.find(x.mid);
// neither awaited nor multiplexed, probably an expired timeout // neither awaited nor multiplexed, probably an expired timeout
if (mrh == multiplexed_responses_.end()) { if (mrh == multiplexed_responses_.end())
CAF_AFTER_PROCESSING(this, invoke_message_result::dropped);
return invoke_message_result::dropped; return invoke_message_result::dropped;
}
auto bhvr = std::move(mrh->second); auto bhvr = std::move(mrh->second);
multiplexed_responses_.erase(mrh); multiplexed_responses_.erase(mrh);
if (!invoke(this, bhvr, x)) { if (!invoke(this, bhvr, x)) {
...@@ -674,17 +671,14 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) { ...@@ -674,17 +671,14 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) {
make_error(sec::unexpected_response, x.move_content_to_message())); make_error(sec::unexpected_response, x.move_content_to_message()));
bhvr(msg); bhvr(msg);
} }
CAF_AFTER_PROCESSING(this, invoke_message_result::consumed);
return invoke_message_result::consumed; return invoke_message_result::consumed;
} }
// Dispatch on the content of x. // Dispatch on the content of x.
switch (categorize(x)) { switch (categorize(x)) {
case message_category::skipped: case message_category::skipped:
CAF_AFTER_PROCESSING(this, invoke_message_result::skipped);
return invoke_message_result::skipped; return invoke_message_result::skipped;
case message_category::internal: case message_category::internal:
CAF_LOG_DEBUG("handled system message"); CAF_LOG_DEBUG("handled system message");
CAF_AFTER_PROCESSING(this, invoke_message_result::consumed);
return invoke_message_result::consumed; return invoke_message_result::consumed;
case message_category::ordinary: { case message_category::ordinary: {
detail::default_invoke_result_visitor<scheduled_actor> visitor{this}; detail::default_invoke_result_visitor<scheduled_actor> visitor{this};
...@@ -712,10 +706,8 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) { ...@@ -712,10 +706,8 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) {
}; };
if (bhvr_stack_.empty()) { if (bhvr_stack_.empty()) {
call_default_handler(); call_default_handler();
auto result = !skipped ? invoke_message_result::consumed return !skipped ? invoke_message_result::consumed
: invoke_message_result::skipped; : invoke_message_result::skipped;
CAF_AFTER_PROCESSING(this, result);
return result;
} }
auto& bhvr = bhvr_stack_.back(); auto& bhvr = bhvr_stack_.back();
switch (bhvr(visitor, x.content())) { switch (bhvr(visitor, x.content())) {
...@@ -727,14 +719,18 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) { ...@@ -727,14 +719,18 @@ invoke_message_result scheduled_actor::consume(mailbox_element& x) {
case match_case::no_match: case match_case::no_match:
call_default_handler(); call_default_handler();
} }
auto result = !skipped ? invoke_message_result::consumed return !skipped ? invoke_message_result::consumed
: invoke_message_result::skipped; : invoke_message_result::skipped;
CAF_AFTER_PROCESSING(this, result);
return result;
} }
} }
// Unreachable. // Unreachable.
CAF_CRITICAL("invalid message type"); CAF_CRITICAL("invalid message type");
};
// Post-process the returned value from the function body.
auto result = body();
CAF_AFTER_PROCESSING(this, result);
CAF_LOG_SKIP_OR_FINALIZE_EVENT(result);
return result;
} }
/// Tries to consume `x`. /// Tries to consume `x`.
......
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