YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
promise_core.hpp
Go to the documentation of this file.
1#pragma once
2
8#include <yaclib/log.hpp>
9
10#include <type_traits>
11#include <utility>
12
13namespace yaclib::detail {
14
15template <typename V, typename E, typename Func, bool Shared>
16class PromiseCore : public std::conditional_t<Shared, SharedCore<V, E>, UniqueCore<V, E>>, public FuncCore<Func> {
17 using F = FuncCore<Func>;
18 using Invoke = typename F::Invoke;
19 using Storage = typename F::Storage;
20
21 using CorePtrT = std::conditional_t<Shared, SharedCorePtr<V, E>, UniqueCorePtr<V, E>>;
22 using PromiseT = std::conditional_t<Shared, SharedPromise<V, E>, Promise<V, E>>;
23
24 public:
25 using Base = std::conditional_t<Shared, SharedCore<V, E>, UniqueCore<V, E>>;
26
27 explicit PromiseCore(Func&& f) : F{std::forward<Func>(f)} {
28 }
29
30 private:
31 void Call() noexcept final {
32 PromiseT promise{CorePtrT{NoRefTag{}, this}};
33 try {
34 // We need to move func with capture on stack, because promise can be Set before func return
35 static_assert(std::is_nothrow_move_constructible_v<Storage>);
36 auto func = std::move(this->_func.storage);
37 this->_func.storage.~Storage();
38 std::forward<Invoke>(func)(std::move(promise));
39 } catch (...) {
40 if (promise.Valid()) {
41 std::move(promise).Set(std::current_exception());
42 } else {
43 // ignore it, because promise already used
44 YACLIB_WARN(true, "Your exception will be ignored, you probably move promise too early");
45 }
46 }
47 }
48
49 void Drop() noexcept final {
50 this->_func.storage.~Storage();
51 this->Store(StopTag{});
52 Loop(this, this->template SetResult<false>());
53 }
54};
55
56} // namespace yaclib::detail
A intrusive pointer to objects with an embedded reference count.
std::decay_t< Func > Storage
Definition func_core.hpp:14
YACLIB_NO_UNIQUE_ADDRESS State _func
Definition func_core.hpp:32
std::conditional_t< std::is_function_v< std::remove_reference_t< Func > >, Storage, Func > Invoke
Definition func_core.hpp:15
std::conditional_t< Shared, SharedCore< V, E >, UniqueCore< V, E > > Base
#define YACLIB_WARN(cond, message)
Definition log.hpp:74
YACLIB_INLINE void Loop(InlineCore *prev, InlineCore *curr) noexcept
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25
YACLIB_NO_UNIQUE_ADDRESS Storage storage
Definition func_core.hpp:24