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 ContextAccessor {
18 bool IsReady()
const noexcept final;
22 void OnFutureCreated();
23 bool IsFutureCreated()
const noexcept;
26 ContextAccessor* TryGetContextAccessor()
noexcept {
return this; }
29 FutureStateBase()
noexcept;
32 void LockResultStore();
33 void ReleaseResultStore();
37 friend class FutureWaitStrategy<FutureStateBase>;
39 EarlyWakeup TryAppendWaiter(TaskContext& waiter)
final;
40 void RemoveWaiter(TaskContext& waiter)
noexcept final;
41 void AfterWait()
noexcept final;
43 FastPimplWaitListLight finish_waiters_;
44 std::atomic<
bool> is_ready_;
45 std::atomic<
bool> is_result_store_locked_;
46 std::atomic<
bool> is_future_created_;
50class FutureState
final :
public FutureStateBase {
52 [[nodiscard]] T Get();
54 void SetValue(
const T& value);
55 void SetValue(T&& value);
56 void SetException(std::exception_ptr&& ex);
59 void RethrowErrorResult()
const override;
61 utils::ResultStore<T> result_store_;
65class FutureState<
void>
final :
public FutureStateBase {
70 void SetException(std::exception_ptr&& ex);
73 void RethrowErrorResult()
const override;
75 utils::ResultStore<
void> result_store_;
79T FutureState<T>::Get() {
81 return result_store_.Retrieve();
85void FutureState<T>::SetValue(
const T& value) {
87 result_store_.SetValue(value);
92void FutureState<T>::SetValue(T&& value) {
94 result_store_.SetValue(std::move(value));
99void FutureState<T>::SetException(std::exception_ptr&& ex) {
101 result_store_.SetException(std::move(ex));
102 ReleaseResultStore();
106void FutureState<T>::RethrowErrorResult()
const {
107 (
void)result_store_.Get();
110inline void FutureState<
void>::Get() {
115inline void FutureState<
void>::SetValue() {
118 ReleaseResultStore();
121inline void FutureState<
void>::SetException(std::exception_ptr&& ex) {
124 ReleaseResultStore();
127inline void FutureState<
void>::RethrowErrorResult()
const {
128 (
void)result_store_.Get();