YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
promise_type.hpp
Go to the documentation of this file.
1#pragma once
2
10
11#include <exception>
12
13namespace yaclib::detail {
14
15template <typename V, typename E, bool Lazy, bool Shared>
16class PromiseType;
17
18struct Destroy final {
19 constexpr bool await_ready() const noexcept {
20 return false;
21 }
22
23 template <typename Promise>
24 YACLIB_INLINE auto await_suspend(yaclib_std::coroutine_handle<Promise> handle) const noexcept {
25 auto& promise = handle.promise();
26#if YACLIB_FINAL_SUSPEND_TRANSFER != 0
27 return promise.template SetResult<true>();
28#elif YACLIB_SYMMETRIC_TRANSFER != 0
29 return promise.template SetResult<true>().resume();
30#else
31 return Loop(&promise, promise.template SetResult<false>());
32#endif
33 }
34
35 constexpr void await_resume() const noexcept {
36 }
37};
38
39template <bool Lazy, bool Shared>
41 template <typename V, typename E>
42 static void Delete(ResultCore<V, E>& core) noexcept;
43};
44
45template <typename V, typename E, bool Lazy, bool Shared>
46using PromiseTypeBase = std::conditional_t<Shared, AtomicCounter<SharedCore<V, E>, PromiseTypeDeleter<Lazy, Shared>>,
48
49template <typename V, typename E, bool Lazy, bool Shared>
50class PromiseType final : public PromiseTypeBase<V, E, Lazy, Shared> {
52 static_assert(!Lazy || !Shared, "Not supported");
53
54 public:
56 } // get_return_object is gonna be invoked right after ctor
57
59 if constexpr (Shared) {
61 } else if constexpr (Lazy) {
63 } else {
65 }
66 }
67
69 if constexpr (Lazy) {
70 return yaclib_std::suspend_always{};
71 } else {
72 return yaclib_std::suspend_never{};
73 }
74 }
75
77 return {};
78 }
79
81 this->Store(std::current_exception());
82 }
83
84 template <typename Value>
85 void return_value(Value&& value) noexcept(std::is_nothrow_constructible_v<Result<V, E>, Value&&>) {
86 this->Store(std::forward<Value>(value));
87 }
88
89 void return_value(Unit) noexcept {
90 this->Store(std::in_place);
91 }
92
94 auto handle = yaclib_std::coroutine_handle<PromiseType>::from_promise(*this);
96 return handle;
97 }
98
99 private:
100 void IncRef() noexcept final {
101 return this->Add(1);
102 }
103 size_t GetRef() noexcept final {
104 return this->Get();
105 }
106 void DecRef() noexcept final {
107 this->Sub(1);
108 }
109
110 void Call() noexcept final {
111 auto next = Curr();
112 next.resume();
113 }
114
115 void Drop() noexcept final {
116 this->Store(StopTag{});
117#if YACLIB_SYMMETRIC_TRANSFER != 0
118 this->template SetResult<true>().resume();
119#else
120 Loop(this, this->template SetResult<false>());
121#endif
122 }
123
124 YACLIB_INLINE void Impl(InlineCore& caller) noexcept {
125 this->_executor = std::move(DownCast<BaseCore>(caller)._executor);
126 YACLIB_ASSERT(this->_executor != nullptr);
127 }
128 [[nodiscard]] InlineCore* Here(InlineCore& caller) noexcept final {
129 Impl(caller);
130 Call();
131 return nullptr;
132 }
133#if YACLIB_SYMMETRIC_TRANSFER != 0
134 [[nodiscard]] yaclib_std::coroutine_handle<> Next(InlineCore& caller) noexcept final {
135 Impl(caller);
136 return Curr();
137 }
138#endif
139
140 [[nodiscard]] yaclib_std::coroutine_handle<> Curr() noexcept final {
141 auto handle = Handle();
142 YACLIB_ASSERT(!handle.done());
143 return handle;
144 }
145};
146
147template <bool Lazy, bool Shared>
148template <typename V, typename E>
151 auto handle = promise.Handle();
152 handle.destroy();
153}
154
155} // namespace yaclib::detail
Provides a mechanism to access the result of async operations.
Definition future.hpp:210
A intrusive pointer to objects with an embedded reference count.
Provides a mechanism to schedule the some async operations TODO(MBkkt) add description.
Definition task.hpp:25
auto get_return_object() noexcept
void return_value(Value &&value) noexcept(std::is_nothrow_constructible_v< Result< V, E >, Value && >)
void unhandled_exception() noexcept
void return_value(Unit) noexcept
auto initial_suspend() noexcept
Destroy final_suspend() noexcept
#define YACLIB_ASSERT(cond)
Definition log.hpp:85
constexpr size_t kSharedRefWithFuture
std::conditional_t< Shared, AtomicCounter< SharedCore< V, E >, PromiseTypeDeleter< Lazy, Shared > >, OneCounter< UniqueCore< V, E >, PromiseTypeDeleter< Lazy, Shared > > > PromiseTypeBase
YACLIB_INLINE void Loop(InlineCore *prev, InlineCore *curr) noexcept
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25
YACLIB_INLINE auto await_suspend(yaclib_std::coroutine_handle< Promise > handle) const noexcept
constexpr void await_resume() const noexcept
constexpr bool await_ready() const noexcept
static void Delete(ResultCore< V, E > &core) noexcept