Skip to content

Commit

Permalink
future, core: convert need_preempt() into a macro.
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslaw Zarzynski <[email protected]>
  • Loading branch information
rzarzynski committed Oct 29, 2019
1 parent 9939aef commit f45b960
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
14 changes: 7 additions & 7 deletions include/seastar/core/future-util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<internal::repeater<futurized_action_type>>(stop_iteration::no, std::move(futurized_action));
auto ret = repeater->get_future();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -427,7 +427,7 @@ repeat_until_value(AsyncAction action) {
if (optional) {
return make_ready_future<value_type>(std::move(optional.value()));
}
} while (!__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED));
} while (!need_preempt());

try {
auto state = std::make_unique<internal::repeat_until_value_state<futurized_action_type, value_type>>(compat::nullopt, std::move(futurized_action));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<do_until_state<StopCondition, AsyncAction>>(std::move(stop_cond), std::move(action));
auto f = task->get_future();
Expand Down Expand Up @@ -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));
Expand Down
6 changes: 3 additions & 3 deletions include/seastar/core/future.hh
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ private:
Result
then_impl(Func&& func) noexcept {
using futurator = futurize<std::result_of_t<Func(T&&...)>>;
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 {
Expand Down Expand Up @@ -1023,7 +1023,7 @@ private:
Result
then_wrapped_impl(Func&& func) noexcept {
using futurator = futurize<std::result_of_t<Func(future)>>;
if (available() && !__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED)) {
if (available() && !need_preempt()) {
return futurator::apply(std::forward<Func>(func), future(get_available_state()));
}
typename futurator::promise_type pr;
Expand Down Expand Up @@ -1262,7 +1262,7 @@ inline
void promise<T...>::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));
Expand Down
26 changes: 9 additions & 17 deletions include/seastar/core/preempt.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@
#pragma once
#include <atomic>

#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
Expand All @@ -40,12 +32,10 @@ struct preemption_monitor {
std::atomic<uint32_t> 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;
Expand All @@ -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

0 comments on commit f45b960

Please sign in to comment.