6#include <userver/engine/deadline.hpp>
7#include <userver/engine/future_status.hpp>
8#include <userver/engine/impl/context_accessor.hpp>
9#include <userver/engine/impl/wait_list_fwd.hpp>
10#include <userver/utils/result_store.hpp>
12USERVER_NAMESPACE_BEGIN
14namespace engine::impl {
16class FutureStateBase :
private engine::impl::ContextAccessor {
18 bool IsReady()
const noexcept final;
22 void OnFutureCreated();
23 bool IsFutureCreated()
const noexcept;
26 ContextAccessor* TryGetContextAccessor()
noexcept {
return this; }
31 FutureStateBase()
noexcept;
34 void LockResultStore();
35 void ReleaseResultStore();
39 void AppendWaiter(impl::TaskContext& context)
noexcept final;
40 void RemoveWaiter(impl::TaskContext& context)
noexcept final;
42 FastPimplWaitListLight finish_waiters_;
43 std::atomic<
bool> is_ready_;
44 std::atomic<
bool> is_result_store_locked_;
45 std::atomic<
bool> is_future_created_;
49class FutureState
final :
public FutureStateBase {
51 [[nodiscard]] T Get();
53 void SetValue(
const T& value);
54 void SetValue(T&& value);
55 void SetException(std::exception_ptr&& ex);
58 void RethrowErrorResult()
const override;
60 utils::ResultStore<T> result_store_;
64class FutureState<
void>
final :
public FutureStateBase {
69 void SetException(std::exception_ptr&& ex);
72 void RethrowErrorResult()
const override;
74 utils::ResultStore<
void> result_store_;
78T FutureState<T>::Get() {
80 return result_store_.Retrieve();
84void FutureState<T>::SetValue(
const T& value) {
86 result_store_.SetValue(value);
91void FutureState<T>::SetValue(T&& value) {
93 result_store_.SetValue(std::move(value));
98void FutureState<T>::SetException(std::exception_ptr&& ex) {
100 result_store_.SetException(std::move(ex));
101 ReleaseResultStore();
105void FutureState<T>::RethrowErrorResult()
const {
106 (
void)result_store_.Get();
109inline void FutureState<
void>::Get() {
114inline void FutureState<
void>::SetValue() {
117 ReleaseResultStore();
120inline void FutureState<
void>::SetException(std::exception_ptr&& ex) {
123 ReleaseResultStore();
126inline void FutureState<
void>::RethrowErrorResult()
const {
127 (
void)result_store_.Get();