From f45b96039600ffe32da7fc2b3474bb2362062f16 Mon Sep 17 00:00:00 2001 From: Radoslaw Zarzynski Date: Thu, 24 Oct 2019 16:32:19 +0200 Subject: [PATCH] future, core: convert need_preempt() into a macro. Signed-off-by: Radoslaw Zarzynski --- include/seastar/core/future-util.hh | 14 +++++++------- include/seastar/core/future.hh | 6 +++--- include/seastar/core/preempt.hh | 26 +++++++++----------------- 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/include/seastar/core/future-util.hh b/include/seastar/core/future-util.hh index a362bffdf0c..874932ffd73 100644 --- a/include/seastar/core/future-util.hh +++ b/include/seastar/core/future-util.hh @@ -224,7 +224,7 @@ public: _promise.set_value(); return; } - } while (!__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED)); + } while (!need_preempt()); } catch (...) { _promise.set_exception(std::current_exception()); return; @@ -297,7 +297,7 @@ future<> repeat(AsyncAction action) { if (f.get0() == stop_iteration::yes) { return make_ready_future<>(); } - } while (!__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED)); + } while (!need_preempt()); auto repeater = std::make_unique>(stop_iteration::no, std::move(futurized_action)); auto ret = repeater->get_future(); @@ -370,7 +370,7 @@ public: _promise.set_value(std::make_tuple(std::move(*ret))); return; } - } while (!__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED)); + } while (!need_preempt()); } catch (...) { _promise.set_exception(std::current_exception()); return; @@ -427,7 +427,7 @@ repeat_until_value(AsyncAction action) { if (optional) { return make_ready_future(std::move(optional.value())); } - } while (!__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED)); + } while (!need_preempt()); try { auto state = std::make_unique>(compat::nullopt, std::move(futurized_action)); @@ -473,7 +473,7 @@ public: f.forward_to(std::move(_promise)); return; } - } while (!__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED)); + } while (!need_preempt()); } catch (...) { _promise.set_exception(std::current_exception()); return; @@ -517,7 +517,7 @@ future<> do_until(StopCondition stop_cond, AsyncAction action) { if (f.failed()) { return f; } - } while (!__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED)); + } while (!need_preempt()); auto task = std::make_unique>(std::move(stop_cond), std::move(action)); auto f = task->get_future(); @@ -570,7 +570,7 @@ future<> do_for_each(Iterator begin, Iterator end, AsyncAction action) { if (begin == end) { return f; } - if (!f.available() || __builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED)) { + if (!f.available() || need_preempt()) { return std::move(f).then([action = std::move(action), begin = std::move(begin), end = std::move(end)] () mutable { return do_for_each(std::move(begin), std::move(end), std::move(action)); diff --git a/include/seastar/core/future.hh b/include/seastar/core/future.hh index 2cb816ed028..a8882e37056 100644 --- a/include/seastar/core/future.hh +++ b/include/seastar/core/future.hh @@ -962,7 +962,7 @@ private: Result then_impl(Func&& func) noexcept { using futurator = futurize>; - if (available() && !__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED)) { + if (available() && !need_preempt()) { if (failed()) { return futurator::make_exception_future(get_available_state().get_exception()); } else { @@ -1023,7 +1023,7 @@ private: Result then_wrapped_impl(Func&& func) noexcept { using futurator = futurize>; - if (available() && !__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED)) { + if (available() && !need_preempt()) { return futurator::apply(std::forward(func), future(get_available_state())); } typename futurator::promise_type pr; @@ -1262,7 +1262,7 @@ inline void promise::make_ready() noexcept { if (_task) { _state = nullptr; - if (Urgent == urgent::yes && !__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED)) { + if (Urgent == urgent::yes && !need_preempt()) { ::seastar::schedule_urgent(std::move(_task)); } else { ::seastar::schedule(std::move(_task)); diff --git a/include/seastar/core/preempt.hh b/include/seastar/core/preempt.hh index 7dcd6542c1e..79563e76449 100644 --- a/include/seastar/core/preempt.hh +++ b/include/seastar/core/preempt.hh @@ -22,15 +22,7 @@ #pragma once #include -#ifndef SEASTAR_DEBUG -#define NEED_PREEMPT_EXPECTED false -#else -#define NEED_PREEMPT_EXPECTED true -#endif - -namespace seastar { - -namespace internal { +namespace seastar::internal { struct preemption_monitor { // We preempt when head != tail @@ -40,12 +32,10 @@ struct preemption_monitor { std::atomic tail; }; -} - -extern __thread const internal::preemption_monitor* g_need_preempt; +extern __thread const preemption_monitor* g_need_preempt -inline bool need_preempt() { #ifndef SEASTAR_DEBUG +inline bool _need_preempt() { // prevent compiler from eliminating loads in a loop std::atomic_signal_fence(std::memory_order_seq_cst); auto np = g_need_preempt; @@ -56,9 +46,11 @@ inline bool need_preempt() { // Possible optimization: read head and tail in a single 64-bit load, // and find a funky way to compare the two 32-bit halves. return __builtin_expect(head != tail, false); -#else - return true; -#endif } -} +} // namespace seastar::internal + +#define need_preempt() __builtin_expect(::seastar::internal::_need_preempt(), false) +#else +#define need_preempt() true +#endif // not SEASTAR_DEBUG