36#include <seastar/core/task.hh>
37#include <seastar/core/thread_impl.hh>
38#include <seastar/core/function_traits.hh>
40#include <seastar/util/critical_alloc_section.hh>
41#include <seastar/util/noncopyable_function.hh>
42#include <seastar/util/backtrace.hh>
43#include <seastar/util/std-compat.hh>
44#include <seastar/util/modules.hh>
49 std::exception_ptr inner;
50 std::exception_ptr outer;
51 nested_exception(std::exception_ptr inner, std::exception_ptr outer)
noexcept;
54 [[noreturn]]
void rethrow_nested()
const;
55 virtual const char* what()
const noexcept override;
148SEASTAR_MODULE_EXPORT_BEGIN
149template <
class T =
void>
155template <
typename... T>
166template <
typename T = void,
typename... A>
174 return make_ready_future<std::remove_cv_t<std::remove_reference_t<T>>>(
184template <
typename T =
void>
187template <
typename T =
void,
typename Exception>
190template <
typename T =
void>
192 return make_exception_future<T>(std::exception_ptr(ex));
195template <
typename T =
void>
197 return make_exception_future<T>(
static_cast<const std::exception_ptr&
>(ex));
200template <
typename T =
void>
203 return make_exception_future<T>(std::exception_ptr(ex));
205SEASTAR_MODULE_EXPORT_END
207void engine_exit(std::exception_ptr eptr = {});
209void report_failed_future(
const std::exception_ptr& ex)
noexcept;
211void report_failed_future(
const future_state_base& state)
noexcept;
232template <
typename T =
void>
240template <
class T =
void>
241class promise_base_with_type;
246template <
typename... T>
247struct future_stored_type;
250struct future_stored_type<> {
251 using type = monostate;
255struct future_stored_type<T> {
256 using type = std::conditional_t<std::is_void_v<T>, internal::monostate, T>;
259template <
typename... T>
260using future_stored_type_t =
typename future_stored_type<T...>::type;
263using future_tuple_type_t = std::conditional_t<std::is_same_v<T, monostate>, std::tuple<>, std::tuple<T>>;
268struct get0_return_type;
271struct get0_return_type<internal::monostate> {
273 static type get0(internal::monostate) { }
277struct get0_return_type {
279 static T get0(T&& v) {
return std::move(v); }
283using maybe_wrap_ref = std::conditional_t<std::is_reference_v<T>, std::reference_wrapper<std::remove_reference_t<T>>, T>;
292template <
typename T,
bool is_trivial_
class>
293struct uninitialized_wrapper_base;
296struct uninitialized_wrapper_base<T, false> {
297 using tuple_type = future_tuple_type_t<T>;
302 maybe_wrap_ref<T> value;
306 uninitialized_wrapper_base() noexcept = default;
307 template<typename... U>
308 std::enable_if_t<!
std::is_same_v<
std::tuple<
std::remove_cv_t<U>...>,
std::tuple<tuple_type>>,
void>
309 uninitialized_set(U&&... vs) {
310 new (&_v.value) maybe_wrap_ref<T>(T(std::forward<U>(vs)...));
312 void uninitialized_set(tuple_type&& v) {
313 uninitialized_set(std::move(std::get<0>(v)));
315 void uninitialized_set(
const tuple_type& v) {
316 uninitialized_set(std::get<0>(v));
318 maybe_wrap_ref<T>& uninitialized_get() {
321 const maybe_wrap_ref<T>& uninitialized_get()
const {
326template <
typename T>
struct uninitialized_wrapper_base<T, true> :
private T {
327 using tuple_type = future_tuple_type_t<T>;
328 uninitialized_wrapper_base() noexcept = default;
329 template<typename... U>
330 std::enable_if_t<!
std::is_same_v<
std::tuple<
std::remove_cv_t<U>...>,
std::tuple<tuple_type>>,
void>
331 uninitialized_set(U&&... vs) {
332 new (
this) T(std::forward<U>(vs)...);
334 void uninitialized_set(tuple_type&& v) {
335 if constexpr (std::tuple_size_v<tuple_type> != 0) {
336 uninitialized_set(std::move(std::get<0>(v)));
339 void uninitialized_set(
const tuple_type& v) {
340 if constexpr (std::tuple_size_v<tuple_type> != 0) {
341 uninitialized_set(std::get<0>(v));
344 T& uninitialized_get() {
347 const T& uninitialized_get()
const {
353constexpr bool can_inherit =
354 (std::is_trivially_destructible_v<T> && std::is_trivially_constructible_v<T> &&
355 std::is_class_v<T> && !std::is_final_v<T>);
360struct uninitialized_wrapper
361 :
public uninitialized_wrapper_base<T, can_inherit<T>> {};
364struct is_trivially_move_constructible_and_destructible {
365 static constexpr bool value = std::is_trivially_move_constructible_v<T> && std::is_trivially_destructible_v<T>;
369struct all_true : std::false_type {};
372struct all_true<> : std::true_type {};
375struct all_true<true, v...> :
public all_true<v...> {};
378struct is_tuple_effectively_trivially_move_constructible_and_destructible_helper;
380template <
typename... T>
381struct is_tuple_effectively_trivially_move_constructible_and_destructible_helper<
std::tuple<T...>> {
382 static constexpr bool value = all_true<is_trivially_move_constructible_and_destructible<T>::value...>::value;
386static constexpr bool is_tuple_effectively_trivially_move_constructible_and_destructible =
387 is_tuple_effectively_trivially_move_constructible_and_destructible_helper<T>::value;
420 static_assert(
sizeof(std::exception_ptr) ==
sizeof(
void*),
"exception_ptr not a pointer");
421 enum class state : uintptr_t {
431 result_unavailable = 2,
436 any()
noexcept { st = state::future; }
437 any(state s)
noexcept { st = s; }
438 void set_exception(std::exception_ptr&& e)
noexcept {
439 new (&ex) std::exception_ptr(std::move(e));
440 assert(st >= state::exception_min);
442 any(std::exception_ptr&& e)
noexcept {
443 set_exception(std::move(e));
446 bool valid()
const noexcept {
return st != state::invalid && st != state::result_unavailable; }
447 bool available()
const noexcept {
return st == state::result || st >= state::exception_min; }
448 bool failed()
const noexcept {
return __builtin_expect(st >= state::exception_min,
false); }
449 void check_failure()
noexcept;
451 std::exception_ptr take_exception()
noexcept {
452 std::exception_ptr ret(std::move(ex));
465 void move_it(
any&& x)
noexcept {
474 memmove(
static_cast<void*
>(
this), &x,
sizeof(
any));
475 x.st = state::invalid;
477 if (x.st < state::exception_min) {
479 x.st = state::invalid;
481 new (&ex) std::exception_ptr(x.take_exception());
486 move_it(std::move(x));
488 any& operator=(
any&& x)
noexcept {
493 move_it(std::move(x));
496 bool has_result()
const noexcept {
497 return st == state::result || st == state::result_unavailable;
500 std::exception_ptr ex;
506 future_state_base(future_state_base&& x) noexcept : _u(std::move(x._u)) { }
518 void rethrow_exception() &&;
519 void rethrow_exception() const&;
523 bool valid() const noexcept {
return _u.valid(); }
524 bool available() const noexcept {
return _u.available(); }
525 bool failed() const noexcept {
return _u.failed(); }
527 void ignore() noexcept;
529 void set_exception(
std::exception_ptr&& ex) noexcept {
530 assert(_u.st == state::future);
531 _u.set_exception(std::move(ex));
533 future_state_base& operator=(future_state_base&& x)
noexcept =
default;
534 void set_exception(future_state_base&& state)
noexcept {
535 assert(_u.st == state::future);
536 *
this = std::move(state);
538 std::exception_ptr get_exception() &&
noexcept {
539 assert(_u.st >= state::exception_min);
541 return _u.take_exception();
543 const std::exception_ptr& get_exception() const& noexcept {
544 assert(_u.st >= state::exception_min);
547 template <
typename U>
548 friend struct future_state;
549 template <
typename U>
551 template <typename U>
553 template <typename T>
561 report_failed_future(std::move(*
this));
571struct future_state :
public future_state_base,
private internal::uninitialized_wrapper<T> {
572 static constexpr bool copy_noexcept = std::is_nothrow_copy_constructible_v<T>;
573 static constexpr bool has_trivial_move_and_destroy = internal::is_trivially_move_constructible_and_destructible<T>::value;
574 static_assert(std::is_nothrow_move_constructible_v<T>,
575 "Types must be no-throw move constructible");
576 static_assert(std::is_nothrow_destructible_v<T>,
577 "Types must be no-throw destructible");
578 future_state() noexcept = default;
579 void move_it(future_state&& x) noexcept {
580 if constexpr (has_trivial_move_and_destroy) {
581#pragma GCC diagnostic push
586#pragma GCC diagnostic ignored "-Wuninitialized"
587 memmove(
reinterpret_cast<char*
>(&this->uninitialized_get()),
588 &x.uninitialized_get(),
589 internal::used_size<internal::maybe_wrap_ref<T>>::value);
590#pragma GCC diagnostic pop
591 }
else if (_u.has_result()) {
592 this->uninitialized_set(std::move(x.uninitialized_get()));
593 std::destroy_at(&x.uninitialized_get());
597 [[gnu::always_inline]]
598 future_state(future_state&& x) noexcept : future_state_base(std::move(x)) {
599 move_it(std::move(x));
602 void clear() noexcept {
603 if (_u.has_result()) {
604 std::destroy_at(&this->uninitialized_get());
609 __attribute__((always_inline))
610 ~future_state() noexcept {
613 future_state& operator=(future_state&& x)
noexcept {
615 future_state_base::operator=(std::move(x));
618 move_it(std::move(x));
621 template <
typename... A>
622 future_state(ready_future_marker, A&&... a) noexcept : future_state_base(state::result) {
624 this->uninitialized_set(std::forward<A>(a)...);
626 new (
this) future_state(current_exception_future_marker());
629 template <
typename... A>
630 void set(A&&... a)
noexcept {
631 assert(_u.st == state::future);
632 new (
this) future_state(ready_future_marker(), std::forward<A>(a)...);
634 future_state(exception_future_marker, std::exception_ptr&& ex) noexcept : future_state_base(std::move(ex)) { }
635 future_state(exception_future_marker, future_state_base&& state) noexcept : future_state_base(std::move(state)) { }
636 future_state(current_exception_future_marker m) noexcept : future_state_base(m) { }
637 future_state(nested_exception_marker m, future_state_base&& old) noexcept : future_state_base(m, std::move(old)) { }
638 future_state(nested_exception_marker m, future_state_base&& n, future_state_base&& old) noexcept : future_state_base(m, std::move(n), std::move(old)) { }
639 T&& get_value() &&
noexcept {
640 assert(_u.st == state::result);
641 return static_cast<T&&
>(this->uninitialized_get());
643 T&& take_value() &&
noexcept {
644 assert(_u.st == state::result);
645 _u.st = state::result_unavailable;
646 return static_cast<T&&
>(this->uninitialized_get());
648 template<
typename U = T>
649 const std::enable_if_t<std::is_copy_constructible_v<U>, U>& get_value() const& noexcept(copy_noexcept) {
650 assert(_u.st == state::result);
651 return this->uninitialized_get();
655 if (_u.st >= state::exception_min) {
656 std::move(*this).rethrow_exception();
658 _u.st = state::result_unavailable;
659 return static_cast<T&&
>(this->uninitialized_get());
663 if (_u.st >= state::exception_min) {
664 std::move(*this).rethrow_exception();
666 return static_cast<T&&
>(this->uninitialized_get());
668 const T& get() const& {
670 if (_u.st >= state::exception_min) {
673 return this->uninitialized_get();
675 using get0_return_type =
typename internal::get0_return_type<T>::type;
676 static get0_return_type get0(T&& x) {
677 return internal::get0_return_type<T>::get0(std::move(x));
680 get0_return_type get0() {
681 return std::move(*this).get();
685template <
typename T =
void>
686class continuation_base :
public task {
688 using future_state = seastar::future_state<internal::future_stored_type_t<T>>;
690 using future_type = future<T>;
691 using promise_type = promise<T>;
693 continuation_base() noexcept = default;
694 void set_state(future_state&& state) noexcept {
695 _state = std::move(state);
701 virtual task* waiting_task() noexcept
override {
return nullptr; }
702 friend class internal::promise_base_with_type<T>;
703 friend class promise<T>;
704 friend class future<T>;
708template <
typename Future>
709struct continuation_base_from_future;
711template <
typename... T>
712struct continuation_base_from_future<future<T...>> {
713 using type = continuation_base<T...>;
716template <
typename Future>
717using continuation_base_from_future_t =
typename continuation_base_from_future<Future>::type;
719template <
typename Promise,
typename T =
void>
720class continuation_base_with_promise :
public continuation_base<T> {
721 friend class internal::promise_base_with_type<T>;
723 continuation_base_with_promise(Promise&& pr) noexcept : _pr(std::move(pr)) {
724 task::make_backtrace();
726 virtual task* waiting_task() noexcept override;
730template <typename Promise, typename Func, typename Wrapper, typename T =
void>
731struct continuation final : continuation_base_with_promise<Promise, T> {
742 continuation(Promise&& pr, Func&& func, Wrapper&& wrapper) noexcept
743 : continuation_base_with_promise<Promise, T>(std::move(pr))
744 , _func(std::move(func))
745 , _wrapper(std::move(wrapper)) {}
746 virtual void run_and_dispose() noexcept
override {
748 _wrapper(std::move(this->_pr), _func, std::move(this->_state));
750 this->_pr.set_to_current_exception();
755 [[no_unique_address]] Wrapper _wrapper;
760template <
typename T =
void>
763template <
typename T =
void>
764void set_callback(future<T>&& fut, continuation_base<T>* callback)
noexcept;
770 enum class urgent { no, yes };
771 future_base* _future =
nullptr;
776 future_state_base* _state;
778 task* _task =
nullptr;
779#ifdef SEASTAR_DEBUG_PROMISE
780 int _task_shard = -1;
782 void set_task(task* task)
noexcept {
786 void assert_task_shard() const noexcept;
788 void set_task(task* task)
noexcept {
791 void assert_task_shard() const noexcept { }
794 promise_base(
const promise_base&) =
delete;
795 promise_base(future_state_base* state) noexcept : _state(state) {}
796 promise_base(future_base* future, future_state_base* state)
noexcept;
797 void move_it(promise_base&& x)
noexcept;
798 promise_base(promise_base&& x)
noexcept;
800 void clear() noexcept;
804 ~promise_base() noexcept {
808 void operator=(
const promise_base&) =
delete;
809 promise_base& operator=(promise_base&& x)
noexcept;
811 template<urgent Urgent>
812 void make_ready() noexcept;
815 void set_exception_impl(T&& val) noexcept {
817 _state->set_exception(std::move(val));
818 make_ready<urgent::no>();
827 report_failed_future(val);
831 void set_exception(future_state_base&& state)
noexcept {
832 set_exception_impl(std::move(state));
835 void set_exception(std::exception_ptr&& ex)
noexcept {
836 set_exception_impl(std::move(ex));
839 void set_exception(
const std::exception_ptr& ex)
noexcept {
840 set_exception(std::exception_ptr(ex));
843 template<
typename Exception>
844 std::enable_if_t<!std::is_same_v<std::remove_reference_t<Exception>, std::exception_ptr>,
void> set_exception(Exception&& e)
noexcept {
845 set_exception(std::make_exception_ptr(std::forward<Exception>(e)));
848 friend class future_base;
856 void set_to_current_exception() noexcept;
859 task* waiting_task() const noexcept {
return _task; }
869class promise_base_with_type :
protected internal::promise_base {
871 using future_state = seastar::future_state<future_stored_type_t<T>>;
872 future_state* get_state() noexcept {
873 return static_cast<future_state*
>(_state);
875 static constexpr bool copy_noexcept = future_state::copy_noexcept;
877 promise_base_with_type(future_state_base* state) noexcept : promise_base(state) { }
878 promise_base_with_type(future<T>* future) noexcept : promise_base(future, &future->_state) { }
879 promise_base_with_type(promise_base_with_type&& x)
noexcept =
default;
880 promise_base_with_type(
const promise_base_with_type&) =
delete;
881 promise_base_with_type& operator=(promise_base_with_type&& x)
noexcept =
default;
882 void operator=(
const promise_base_with_type&) =
delete;
884 void set_urgent_state(future_state&& state)
noexcept {
885 auto* ptr = get_state();
892 assert(ptr->_u.st == future_state_base::state::future);
893 new (ptr) future_state(std::move(state));
894 make_ready<urgent::yes>();
898 template <
typename... A>
899 void set_value(A&&... a)
noexcept {
900 if (
auto *s = get_state()) {
901 s->set(std::forward<A>(a)...);
902 make_ready<urgent::no>();
910 void set_to_current_exception() noexcept {
911 internal::promise_base::set_to_current_exception();
915 using internal::promise_base::waiting_task;
919 template <
typename U>
934class promise :
private internal::promise_base_with_type<T> {
935 using future_state =
typename internal::promise_base_with_type<T>::future_state;
936 future_state _local_state;
942 promise() noexcept : internal::promise_base_with_type<T>(&_local_state) {}
946 promise(
promise&& x) noexcept : internal::promise_base_with_type<T>(std::move(x)) {
947 move_it(std::move(x));
951 internal::promise_base_with_type<T>::operator=(std::move(x));
954 move_it(std::move(x));
957 void operator=(
const promise&) =
delete;
964 internal::promise_base::set_to_current_exception();
968 using internal::promise_base::waiting_task;
989 template <typename... A>
990 void set_value(A&&... a) noexcept {
991 internal::promise_base_with_type<T>::set_value(std::forward<A>(a)...);
999 internal::promise_base::set_exception(std::move(ex));
1002 void set_exception(
const std::exception_ptr& ex)
noexcept {
1003 internal::promise_base::set_exception(ex);
1010 template<
typename Exception>
1011 std::enable_if_t<!std::is_same_v<std::remove_reference_t<Exception>, std::exception_ptr>,
void>
set_exception(Exception&& e)
noexcept {
1012 internal::promise_base::set_exception(std::forward<Exception>(e));
1015 using internal::promise_base_with_type<T>::set_urgent_state;
1017 template <
typename U>
1032template <
typename... T>
struct is_future : std::false_type {};
1036template <
typename... T>
struct is_future<
future<T...>> : std::true_type {};
1044SEASTAR_MODULE_EXPORT
1045template <
typename T>
1048template <
typename T>
1049concept Future = is_future<T>::value;
1051template <
typename Func,
typename... T>
1052concept CanInvoke = std::invocable<Func, T...>;
1055template <
typename Func,
typename... T>
1056concept CanApply = CanInvoke<Func, T...>;
1058template <
typename Func,
typename... T>
1059concept CanApplyTuple
1061 &&
requires (Func func, std::tuple<T...> wrapped_val) {
1062 { std::apply(func, std::get<0>(std::move(wrapped_val))) };
1066template <
typename Func,
typename Return,
typename... T>
1067concept InvokeReturns =
requires (Func f, T... args) {
1068 { f(std::forward<T>(args)...) } -> std::same_as<Return>;
1072template <
typename Func,
typename Return,
typename... T>
1073concept ApplyReturns = InvokeReturns<Func, Return, T...>;
1075template <
typename Func,
typename... T>
1076concept InvokeReturnsAnyFuture = Future<std::invoke_result_t<Func, T...>>;
1079template <
typename Func,
typename... T>
1080concept ApplyReturnsAnyFuture = InvokeReturnsAnyFuture<Func, T...>;
1085template <
typename T>
1086using futurize_t =
typename futurize<T>::type;
1090template<
typename Func,
typename... Args>
1091auto futurize_invoke(Func&& func, Args&&... args)
noexcept;
1093template<
typename Func,
typename... Args>
1094auto futurize_apply(Func&& func, std::tuple<Args...>&& args)
noexcept;
1101 promise_base* _promise;
1102 future_base() noexcept : _promise(
nullptr) {}
1103 future_base(promise_base* promise, future_state_base* state) noexcept : _promise(promise) {
1104 _promise->_future =
this;
1105 _promise->_state = state;
1108 void move_it(future_base&& x, future_state_base* state)
noexcept {
1109 _promise = x._promise;
1110 if (
auto* p = _promise) {
1117 future_base(future_base&& x, future_state_base* state)
noexcept {
1118 move_it(std::move(x), state);
1121 void clear() noexcept {
1127 ~future_base() noexcept {
1131 promise_base* detach_promise() noexcept {
1132 _promise->_state =
nullptr;
1133 _promise->_future =
nullptr;
1134 return std::exchange(_promise,
nullptr);
1137 void schedule(task* tws, future_state_base* state)
noexcept {
1138 promise_base* p = detach_promise();
1143 void do_wait() noexcept;
1145 void set_coroutine(task& coroutine) noexcept;
1147 friend class promise_base;
1150template <typename Func, typename... T>
1151struct future_result {
1152 using type = std::invoke_result_t<Func, T...>;
1153 using future_type = futurize_t<type>;
1154 using func_type = future_type (T&&...);
1157template <
typename Func>
1158struct future_result<Func, void> {
1159 using type = std::invoke_result_t<Func>;
1160 using future_type = futurize_t<type>;
1161 using func_type = future_type ();
1164template <
typename Func,
typename T>
1165using future_result_t =
typename future_result<Func, T>::type;
1167template <
typename Func,
typename T>
1168auto future_invoke(Func&& func, T&& v) {
1169 if constexpr (std::is_same_v<T, monostate>) {
1170 return std::invoke(std::forward<Func>(func));
1172 return std::invoke(std::forward<Func>(func), std::forward<T>(v));
1176template <
typename Func,
typename... T>
1177struct result_of_apply {
1181template <
typename Func,
typename... T>
1182struct result_of_apply<Func,
std::tuple<T...>> : std::invoke_result<Func, T...> {
1186template <
typename Func,
typename... T>
1187using result_of_apply_t =
typename result_of_apply<Func, T...>::type;
1191template <
typename Promise,
typename T>
1192task* continuation_base_with_promise<Promise, T>::waiting_task() noexcept {
1193 return _pr.waiting_task();
1238SEASTAR_MODULE_EXPORT
1239template <
typename T>
1240class [[nodiscard]]
future :
private internal::future_base {
1241 using future_state = seastar::future_state<internal::future_stored_type_t<T>>;
1242 future_state _state;
1243 static constexpr bool copy_noexcept = future_state::copy_noexcept;
1254 future(
promise<T>* pr) noexcept : future_base(pr, &_state), _state(std::move(pr->_local_state)) { }
1255 template <
typename... A>
1262 [[gnu::always_inline]]
1263 explicit future(future_state&& state) noexcept
1264 : _state(std::move(state)) {
1266 internal::promise_base_with_type<T> get_promise()
noexcept {
1268 return internal::promise_base_with_type<T>(
this);
1270 internal::promise_base_with_type<T>* detach_promise()
noexcept {
1271 return static_cast<internal::promise_base_with_type<T>*
>(future_base::detach_promise());
1273 void schedule(continuation_base<T>* tws)
noexcept {
1274 future_base::schedule(tws, &tws->_state);
1276 template <
typename Pr,
typename Func,
typename Wrapper>
1277 void schedule(Pr&& pr, Func&& func, Wrapper&& wrapper)
noexcept {
1283 auto tws =
new continuation<Pr, Func, Wrapper, T>(std::move(pr), std::move(func), std::move(wrapper));
1287 if (_state.available()) {
1288 tws->set_state(get_available_state_ref());
1289 ::seastar::schedule(tws);
1294 _state._u.st = future_state_base::state::invalid;
1297 [[gnu::always_inline]]
1298 future_state&& get_available_state_ref()
noexcept {
1302 return std::move(_state);
1309 future<T> rethrow_with_nested()
noexcept {
1313 template<
typename... U>
1318 using tuple_type = internal::future_tuple_type_t<value_type>;
1322 [[gnu::always_inline]]
1323 future(
future&& x) noexcept : future_base(std::move(x), &_state), _state(std::move(x._state)) { }
1327 move_it(std::move(x), &_state);
1328 _state = std::move(x._state);
1331 void operator=(
const future&) =
delete;
1341 [[gnu::always_inline]]
1344 return get_available_state_ref().take();
1347 [[gnu::always_inline]]
1348 std::exception_ptr get_exception() noexcept {
1349 return get_available_state_ref().get_exception();
1373 [[deprecated(
"Use get() instead")]]
1384 if (_state.available()) {
1393 [[gnu::always_inline]]
1395 return _state.available();
1401 [[gnu::always_inline]]
1403 return _state.failed();
1421 template <typename Func, typename Result = typename internal::future_result<Func, T>::future_type>
1422 requires std::invocable<Func, T>
1423 || (std::same_as<void, T> && std::invocable<Func>)
1426#ifndef SEASTAR_TYPE_ERASE_MORE
1427 return then_impl(std::move(func));
1429 using func_type =
typename internal::future_result<Func, T>::func_type;
1434 return futurize_invoke(func, std::forward<
decltype(args)>(args)...);
1437 return then_impl(std::move(ncf));
1460 template <
typename Func,
typename Result = futurize_t<
internal::result_of_apply_t<Func, T>>>
1461 requires ::seastar::CanApplyTuple<Func, T>
1464 return then([func = std::forward<Func>(func)] (T&& tuple)
mutable {
1466 return std::apply(func, std::move(tuple));
1473 template <
typename Func,
typename Result>
1474 Result then_impl_nrvo(Func&& func)
noexcept {
1477 using pr_type =
decltype(fut.get_promise());
1478 schedule(fut.get_promise(), std::move(func), [](pr_type&& pr, Func& func, future_state&& state) {
1479 if (state.failed()) {
1480 pr.set_exception(static_cast<future_state_base&&>(std::move(state)));
1482 futurator::satisfy_with_result_of(std::move(pr), [&func, &state] {
1486 return internal::future_invoke(func, std::move(state).get_value());
1493 template <
typename Func,
typename Result = futurize_t<
internal::future_result_t<Func, T>>>
1495 then_impl(Func&& func)
noexcept {
1496#ifndef SEASTAR_DEBUG
1497 using futurator = futurize<internal::future_result_t<Func, T>>;
1499 return futurator::make_exception_future(
static_cast<future_state_base&&
>(get_available_state_ref()));
1500 }
else if (available()) {
1501 return futurator::invoke(std::forward<Func>(func), get_available_state_ref().take_value());
1504 return then_impl_nrvo<Func, Result>(std::forward<Func>(func));
1523 template <std::invocable<future> Func,
typename FuncResult = std::invoke_result_t<Func, future>>
1524 futurize_t<FuncResult>
1526 return then_wrapped_maybe_erase<false, FuncResult>(std::forward<Func>(func));
1529 template <std::invocable<future&&> Func,
typename FuncResult = std::invoke_result_t<Func, future&&>>
1530 futurize_t<FuncResult>
1531 then_wrapped(Func&& func) &&
noexcept {
1532 return then_wrapped_maybe_erase<true, FuncResult>(std::forward<Func>(func));
1537 template <
bool AsSelf,
typename FuncResult,
typename Func>
1538 futurize_t<FuncResult>
1539 then_wrapped_maybe_erase(Func&& func)
noexcept {
1540#ifndef SEASTAR_TYPE_ERASE_MORE
1541 return then_wrapped_common<AsSelf, FuncResult>(std::forward<Func>(func));
1543 using futurator = futurize<FuncResult>;
1544 using WrapFuncResult =
typename futurator::type;
1545 noncopyable_function<WrapFuncResult (future&&)> ncf;
1547 memory::scoped_critical_alloc_section _;
1548 ncf = noncopyable_function<WrapFuncResult(future &&)>([func = std::forward<Func>(func)](future&& f)
mutable {
1549 return futurator::invoke(func, std::move(f));
1552 return then_wrapped_common<AsSelf, WrapFuncResult>(std::move(ncf));
1557 template <
typename FuncResult,
typename Func>
1558 futurize_t<FuncResult>
1559 then_wrapped_nrvo(Func&& func)
noexcept {
1560 using futurator = futurize<FuncResult>;
1561 typename futurator::type fut(future_for_get_promise_marker{});
1562 using pr_type =
decltype(fut.get_promise());
1563 schedule(fut.get_promise(), std::move(func), [](pr_type&& pr, Func& func, future_state&& state) {
1564 futurator::satisfy_with_result_of(std::move(pr), [&func, &state] {
1565 return func(future(std::move(state)));
1572 template <
bool AsSelf,
typename FuncResult,
typename Func>
1573 futurize_t<FuncResult>
1574 then_wrapped_common(Func&& func)
noexcept {
1575#ifndef SEASTAR_DEBUG
1576 using futurator = futurize<FuncResult>;
1578 if constexpr (AsSelf) {
1582 return futurator::invoke(std::forward<Func>(func), std::move(*
this));
1584 return futurator::invoke(std::forward<Func>(func), future(get_available_state_ref()));
1588 return then_wrapped_nrvo<FuncResult, Func>(std::forward<Func>(func));
1591 void forward_to(internal::promise_base_with_type<T>&& pr)
noexcept {
1592 if (_state.available()) {
1593 pr.set_urgent_state(std::move(_state));
1595 *detach_promise() = std::move(pr);
1611 if (_state.available()) {
1612 pr.set_urgent_state(std::move(_state));
1613 }
else if (&pr._local_state != pr._state) {
1618 *detach_promise() = std::move(pr);
1639 template <std::invocable Func>
1641 return then_wrapped(
finally_body<Func,
is_future<std::invoke_result_t<Func>>::value>(std::forward<Func>(func)));
1645 template <
typename Func,
bool FuncReturnsFuture>
1648 template <
typename Func>
1652 finally_body(Func&& func) noexcept : _func(std::forward<Func>(func))
1656 return futurize_invoke(_func).then_wrapped([result = std::move(result)](
auto&& f_res)
mutable {
1657 if (!f_res.failed()) {
1658 return std::move(result);
1660 return result.rethrow_with_nested(std::move(f_res._state));
1666 template <
typename Func>
1670 finally_body(Func&& func) noexcept : _func(std::forward<Func>(func))
1676 return std::move(result);
1678 return result.rethrow_with_nested();
1688 return then_wrapped([] (
auto&& f) {
1692 engine_exit(std::current_exception());
1704 return then([] (
auto&&...) {});
1720 template <
typename Func>
1721 requires std::is_invocable_r_v<future<T> ,Func, std::exception_ptr>
1722 || (std::tuple_size_v<tuple_type> == 0 && std::is_invocable_r_v<void, Func, std::exception_ptr>)
1723 || (std::tuple_size_v<tuple_type> == 1 && std::is_invocable_r_v<T, Func, std::exception_ptr>)
1724 || (std::tuple_size_v<tuple_type> > 1 && std::is_invocable_r_v<tuple_type ,Func, std::exception_ptr>)
1726 return then_wrapped([func = std::forward<Func>(func)]
1728 if (!fut.failed()) {
1729 return make_ready_future<T>(fut.get());
1731 return futurize_invoke(func, fut.get_exception());
1746 template <
typename Func>
1749 static_assert(trait::arity == 1,
"func can take only one parameter");
1750 using ex_type =
typename trait::template arg<0>::type;
1751 return then_wrapped([func = std::forward<Func>(func)]
1754 return make_ready_future<T>(fut.get());
1755 }
catch(ex_type& ex) {
1756 return futurize_invoke(func, ex);
1770 using future_base::set_coroutine;
1773 void set_task(
task& t)
noexcept {
1775 _promise->set_task(&t);
1778 void set_callback(continuation_base<T>* callback)
noexcept {
1779 if (_state.available()) {
1780 callback->set_state(get_available_state_ref());
1781 ::seastar::schedule(callback);
1790 template <
typename U>
1791 friend class future;
1792 template <
typename U>
1793 friend class promise;
1794 template <
typename U>
1795 friend struct futurize;
1796 template <
typename U>
1797 friend class internal::promise_base_with_type;
1798 template <
typename U,
typename... A>
1800 template <
typename U>
1802 template <
typename U,
typename Exception>
1804 template <
typename U>
1805 friend future<U> internal::make_exception_future(future_state_base&& state)
noexcept;
1806 template <
typename U>
1808 template <typename U>
1809 friend
void internal::set_callback(future<U>&&, continuation_base<U>*) noexcept;
1815template <
typename T>
1816struct futurize_base {
1818 using type = future<T>;
1820 using promise_type = promise<T>;
1821 using promise_base_with_type = internal::promise_base_with_type<T>;
1824 static inline type convert(T&& value) {
return make_ready_future<T>(std::move(value)); }
1825 static inline type convert(type&& value) {
return std::move(value); }
1828 template <
typename Arg>
1833struct futurize_base<void> {
1834 using type = future<>;
1836 using promise_base_with_type = internal::promise_base_with_type<>;
1838 static inline type convert(type&& value) {
1839 return std::move(value);
1841 template <
typename Arg>
1845template <
typename T>
1846struct futurize_base<future<T>> :
public futurize_base<T> {};
1849struct futurize_base<future<>> :
public futurize_base<void> {};
1852template <
typename T>
1854 using base = internal::futurize_base<T>;
1857 using promise_base_with_type =
typename base::promise_base_with_type;
1860 using tuple_type =
typename type::tuple_type;
1861 using base::convert;
1862 using base::make_exception_future;
1866 template<
typename Func,
typename... FuncArgs>
1867 static inline type apply(Func&& func, std::tuple<FuncArgs...>&& args)
noexcept;
1871 template<
typename Func,
typename... FuncArgs>
1872 static inline type invoke(Func&& func, FuncArgs&&... args)
noexcept;
1874 template<
typename Func>
1875 static inline type invoke(Func&& func, internal::monostate)
noexcept {
1876 return invoke(std::forward<Func>(func));
1880 template<
typename Func,
typename... FuncArgs>
1881 [[deprecated(
"Use invoke for varargs")]]
1882 static inline type apply(Func&& func, FuncArgs&&... args)
noexcept {
1883 return invoke(std::forward<Func>(func), std::forward<FuncArgs>(args)...);
1911 template<std::invocable Func>
1912 static void satisfy_with_result_of(promise_base_with_type&&, Func&& func);
1914 template <
typename U>
1918inline internal::promise_base::promise_base(future_base* future, future_state_base* state) noexcept
1919 : _future(future), _state(state) {
1920 _future->_promise =
this;
1923template <
typename T>
1927 assert(!this->_future && this->_state && !this->_task);
1931template <
typename T>
1934 if (this->_state == &x._local_state) {
1935 this->_state = &_local_state;
1936 new (&_local_state) future_state(std::move(x._local_state));
1940SEASTAR_MODULE_EXPORT_BEGIN
1941template <
typename T,
typename... A>
1947template <
typename T>
1952SEASTAR_MODULE_EXPORT_END
1954template <
typename T>
1956future<T> internal::make_exception_future(future_state_base&& state)
noexcept {
1957 return future<T>(exception_future_marker(), std::move(state));
1960SEASTAR_MODULE_EXPORT_BEGIN
1961template <
typename T>
1966void log_exception_trace() noexcept;
1974template <typename T, typename Exception>
1977 log_exception_trace();
1978 return make_exception_future<T>(std::make_exception_ptr(std::forward<Exception>(ex)));
1981template <
typename T,
typename Exception>
1982future<T> make_exception_future_with_backtrace(Exception&& ex)
noexcept {
1983 return make_exception_future<T>(make_backtraced_exception_ptr<Exception>(std::forward<Exception>(ex)));
1985SEASTAR_MODULE_EXPORT_END
1992template<
typename Func,
typename... FuncArgs>
1993typename futurize<T>::type
futurize<T>::apply(Func&& func, std::tuple<FuncArgs...>&& args)
noexcept {
1995 using ret_t =
decltype(std::apply(std::forward<Func>(func), std::move(args)));
1996 if constexpr (std::is_void_v<ret_t>) {
1997 std::apply(std::forward<Func>(func), std::move(args));
1998 return make_ready_future<>();
1999 }
else if constexpr (is_future<ret_t>::value){
2000 return std::apply(std::forward<Func>(func), std::move(args));
2002 return convert(std::apply(std::forward<Func>(func), std::move(args)));
2010template<std::invocable Func>
2011void futurize<T>::satisfy_with_result_of(promise_base_with_type&& pr, Func&& func) {
2012 using ret_t =
decltype(func());
2013 if constexpr (std::is_void_v<ret_t>) {
2016 }
else if constexpr (is_future<ret_t>::value) {
2017 func().forward_to(std::move(pr));
2024template<
typename Func,
typename... FuncArgs>
2027 using ret_t =
decltype(func(std::forward<FuncArgs>(args)...));
2028 if constexpr (std::is_void_v<ret_t>) {
2029 func(std::forward<FuncArgs>(args)...);
2030 return make_ready_future<>();
2031 }
else if constexpr (is_future<ret_t>::value) {
2032 return func(std::forward<FuncArgs>(args)...);
2034 return convert(func(std::forward<FuncArgs>(args)...));
2041template <
typename T>
2042template <
typename Arg>
2045internal::futurize_base<T>::make_exception_future(Arg&& arg)
noexcept {
2046 using ::seastar::make_exception_future;
2047 using ::seastar::internal::make_exception_future;
2048 return make_exception_future<T>(std::forward<Arg>(arg));
2051template <
typename Arg>
2054internal::futurize_base<void>::make_exception_future(Arg&& arg)
noexcept {
2055 using ::seastar::make_exception_future;
2056 using ::seastar::internal::make_exception_future;
2057 return make_exception_future<>(std::forward<Arg>(arg));
2060template<
typename Func,
typename... Args>
2061auto futurize_invoke(Func&& func, Args&&... args)
noexcept {
2062 using futurator = futurize<std::invoke_result_t<Func, Args&&...>>;
2063 return futurator::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
2066template<
typename Func,
typename... Args>
2067[[deprecated(
"Use futurize_invoke for varargs")]]
2068auto futurize_apply(Func&& func, Args&&... args)
noexcept {
2069 return futurize_invoke(std::forward<Func>(func), std::forward<Args>(args)...);
2072template<
typename Func,
typename... Args>
2073auto futurize_apply(Func&& func, std::tuple<Args...>&& args)
noexcept {
2074 using futurator = futurize<std::invoke_result_t<Func, Args&&...>>;
2075 return futurator::apply(std::forward<Func>(func), std::move(args));
2080template <
typename T>
2082void set_callback(future<T>&& fut, continuation_base<T>* callback)
noexcept {
2083 return std::move(fut).set_callback(callback);
A representation of a possibly not-yet-computed value.
Definition: future.hh:1240
futurize_t< FuncResult > then_wrapped(Func &&func) &noexcept
Schedule a block of code to run when the future is ready, allowing for exception handling.
Definition: future.hh:1525
future< T > handle_exception_type(Func &&func) noexcept
Handle the exception of a certain type carried by this future.
Definition: future.hh:1747
void wait() noexcept
Definition: future.hh:1383
internal::future_stored_type_t< T > value_type
The data type carried by the future.
Definition: future.hh:1317
future< T > handle_exception(Func &&func) noexcept
Handle the exception carried by this future.
Definition: future.hh:1725
Result then(Func &&func) noexcept
Schedule a block of code to run when the future is ready.
Definition: future.hh:1425
value_type && get()
gets the value returned by the computation
Definition: future.hh:1342
bool failed() const noexcept
Checks whether the future has failed.
Definition: future.hh:1402
requires ::seastar::CanApplyTuple< Func, T > Result then_unpack(Func &&func) noexcept
Schedule a block of code to run when the future is ready, unpacking tuples.
Definition: future.hh:1463
bool available() const noexcept
Checks whether the future is available.
Definition: future.hh:1394
typename future_state::get0_return_type get0_return_type
Definition: future.hh:1372
void forward_to(promise< T > &&pr) noexcept
Satisfy some promise object with this future as a result.
Definition: future.hh:1610
future(future &&x) noexcept
Moves the future into a new object.
Definition: future.hh:1323
future discard_result() noexcept
Discards the value carried by this future.
Definition: future.hh:1701
future or_terminate() noexcept
Terminate the program if this future fails.
Definition: future.hh:1687
void ignore_ready_future() noexcept
Ignore any result hold by this future.
Definition: future.hh:1766
Definition: future.hh:1646
promise - allows a future value to be made available at a later time.
Definition: future.hh:934
void set_to_current_exception() noexcept
Definition: future.hh:963
std::enable_if_t<!std::is_same_v< std::remove_reference_t< Exception >, std::exception_ptr >, void > set_exception(Exception &&e) noexcept
Marks the promise as failed.
Definition: future.hh:1011
void set_value(A &&... a) noexcept
Sets the promises value.
Definition: future.hh:990
void set_exception(std::exception_ptr &&ex) noexcept
Marks the promise as failed.
Definition: future.hh:998
promise() noexcept
Constructs an empty promise.
Definition: future.hh:942
Like future except the result can be waited for by many fibers.
Definition: shared_future.hh:108
future< T > make_ready_future(A &&... value) noexcept
Creates a future in an available, value state.
Definition: future.hh:1943
future< T > get_future() noexcept
Gets the promise's associated future.
Definition: future.hh:1926
future< std::remove_cv_t< std::remove_reference_t< T > > > as_ready_future(T &&v) noexcept
Returns a ready future that is already resolved.
Definition: future.hh:173
future< T > make_exception_future(std::exception_ptr &&value) noexcept
Creates a future in an available, failed state.
Definition: future.hh:1949
future< T > current_exception_as_future() noexcept
Returns std::current_exception() wrapped in a future.
Definition: future.hh:1962
void move_it(promise &&x) noexcept
Moves a promise object.
Definition: future.hh:1933
Definition: future.hh:566
Definition: future.hh:567
Definition: future.hh:565
Definition: critical_alloc_section.hh:80
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
shard_id this_shard_id() noexcept
Returns shard_id of the of the current shard.
Definition: shard_id.hh:52
Definition: noncopyable_function.hh:37
Definition: function_traits.hh:62
Exception type for broken promises.
Definition: future.hh:222
Definition: future.hh:419
friend future< U > current_exception_as_future() noexcept
Returns std::current_exception() wrapped in a future.
Definition: future.hh:1962
Definition: future.hh:511
Definition: future.hh:513
Converts a type to a future type, if it isn't already.
Definition: future.hh:1853
static type apply(Func &&func, FuncArgs &&... args) noexcept
Deprecated alias of invoke.
Definition: future.hh:1882
static type from_tuple(tuple_type &&value)
Convert the tuple representation into a future.
Definition: future.hh:1891
static type from_tuple(value_type &&value)
Convert the tuple representation into a future.
Definition: future.hh:1900
static type apply(Func &&func, std::tuple< FuncArgs... > &&args) noexcept
static type from_tuple(const value_type &value)
Convert the tuple representation into a future.
Definition: future.hh:1904
static type from_tuple(const tuple_type &value)
Convert the tuple representation into a future.
Definition: future.hh:1895
typename type::value_type value_type
The value tuple type associated with type.
Definition: future.hh:1859
static type invoke(Func &&func, FuncArgs &&... args) noexcept
Check whether a type is a future.
Definition: future.hh:1032
Definition: future.hh:435