Skip to content

Commit afb186c

Browse files
committed
future, core: convert need_preempt() into a macro.
Signed-off-by: Radoslaw Zarzynski <rzarzyns@redhat.com>
1 parent 9939aef commit afb186c

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

include/seastar/core/future-util.hh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public:
224224
_promise.set_value();
225225
return;
226226
}
227-
} while (!__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED));
227+
} while (!need_preempt());
228228
} catch (...) {
229229
_promise.set_exception(std::current_exception());
230230
return;
@@ -297,7 +297,7 @@ future<> repeat(AsyncAction action) {
297297
if (f.get0() == stop_iteration::yes) {
298298
return make_ready_future<>();
299299
}
300-
} while (!__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED));
300+
} while (!need_preempt());
301301

302302
auto repeater = std::make_unique<internal::repeater<futurized_action_type>>(stop_iteration::no, std::move(futurized_action));
303303
auto ret = repeater->get_future();
@@ -370,7 +370,7 @@ public:
370370
_promise.set_value(std::make_tuple(std::move(*ret)));
371371
return;
372372
}
373-
} while (!__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED));
373+
} while (!need_preempt());
374374
} catch (...) {
375375
_promise.set_exception(std::current_exception());
376376
return;
@@ -427,7 +427,7 @@ repeat_until_value(AsyncAction action) {
427427
if (optional) {
428428
return make_ready_future<value_type>(std::move(optional.value()));
429429
}
430-
} while (!__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED));
430+
} while (!need_preempt());
431431

432432
try {
433433
auto state = std::make_unique<internal::repeat_until_value_state<futurized_action_type, value_type>>(compat::nullopt, std::move(futurized_action));
@@ -473,7 +473,7 @@ public:
473473
f.forward_to(std::move(_promise));
474474
return;
475475
}
476-
} while (!__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED));
476+
} while (!need_preempt());
477477
} catch (...) {
478478
_promise.set_exception(std::current_exception());
479479
return;
@@ -517,7 +517,7 @@ future<> do_until(StopCondition stop_cond, AsyncAction action) {
517517
if (f.failed()) {
518518
return f;
519519
}
520-
} while (!__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED));
520+
} while (!need_preempt());
521521

522522
auto task = std::make_unique<do_until_state<StopCondition, AsyncAction>>(std::move(stop_cond), std::move(action));
523523
auto f = task->get_future();
@@ -570,7 +570,7 @@ future<> do_for_each(Iterator begin, Iterator end, AsyncAction action) {
570570
if (begin == end) {
571571
return f;
572572
}
573-
if (!f.available() || __builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED)) {
573+
if (!f.available() || need_preempt()) {
574574
return std::move(f).then([action = std::move(action),
575575
begin = std::move(begin), end = std::move(end)] () mutable {
576576
return do_for_each(std::move(begin), std::move(end), std::move(action));

include/seastar/core/future.hh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ private:
962962
Result
963963
then_impl(Func&& func) noexcept {
964964
using futurator = futurize<std::result_of_t<Func(T&&...)>>;
965-
if (available() && !__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED)) {
965+
if (available() && !need_preempt()) {
966966
if (failed()) {
967967
return futurator::make_exception_future(get_available_state().get_exception());
968968
} else {
@@ -1023,7 +1023,7 @@ private:
10231023
Result
10241024
then_wrapped_impl(Func&& func) noexcept {
10251025
using futurator = futurize<std::result_of_t<Func(future)>>;
1026-
if (available() && !__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED)) {
1026+
if (available() && !need_preempt()) {
10271027
return futurator::apply(std::forward<Func>(func), future(get_available_state()));
10281028
}
10291029
typename futurator::promise_type pr;
@@ -1262,7 +1262,7 @@ inline
12621262
void promise<T...>::make_ready() noexcept {
12631263
if (_task) {
12641264
_state = nullptr;
1265-
if (Urgent == urgent::yes && !__builtin_expect(need_preempt(), NEED_PREEMPT_EXPECTED)) {
1265+
if (Urgent == urgent::yes && !need_preempt()) {
12661266
::seastar::schedule_urgent(std::move(_task));
12671267
} else {
12681268
::seastar::schedule(std::move(_task));

include/seastar/core/preempt.hh

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@
2222
#pragma once
2323
#include <atomic>
2424

25-
#ifndef SEASTAR_DEBUG
26-
#define NEED_PREEMPT_EXPECTED false
27-
#else
28-
#define NEED_PREEMPT_EXPECTED true
29-
#endif
30-
3125
namespace seastar {
3226

3327
namespace internal {
@@ -40,12 +34,10 @@ struct preemption_monitor {
4034
std::atomic<uint32_t> tail;
4135
};
4236

43-
}
44-
45-
extern __thread const internal::preemption_monitor* g_need_preempt;
37+
extern __thread const preemption_monitor* g_need_preempt;
4638

47-
inline bool need_preempt() {
4839
#ifndef SEASTAR_DEBUG
40+
inline bool _need_preempt() {
4941
// prevent compiler from eliminating loads in a loop
5042
std::atomic_signal_fence(std::memory_order_seq_cst);
5143
auto np = g_need_preempt;
@@ -56,9 +48,15 @@ inline bool need_preempt() {
5648
// Possible optimization: read head and tail in a single 64-bit load,
5749
// and find a funky way to compare the two 32-bit halves.
5850
return __builtin_expect(head != tail, false);
59-
#else
60-
return true;
61-
#endif
6251
}
52+
#endif // not SEASTAR_DEBUG
6353

64-
}
54+
} // namespace internal
55+
56+
} // namespace seastar
57+
58+
#ifndef SEASTAR_DEBUG
59+
#define need_preempt() __builtin_expect(::seastar::internal::_need_preempt(), false)
60+
#else
61+
#define need_preempt() true
62+
#endif // not SEASTAR_DEBUG

0 commit comments

Comments
 (0)