YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
base_core.cpp
Go to the documentation of this file.
4#include <yaclib/log.hpp>
5
6namespace yaclib::detail {
7
9 // TODO(MBkkt) with atomic_ref here can be non-atomic store
10 _callback.store(reinterpret_cast<std::uintptr_t>(&callback), std::memory_order_relaxed);
11}
12
14 auto callback = _callback.load(std::memory_order_acquire);
15 YACLIB_DEBUG(callback != kEmpty && callback != kResult, "That means we call it on already used future or on promise");
16 return callback == kEmpty;
17}
18
20 YACLIB_ASSERT(reinterpret_cast<std::uintptr_t>(&callback) != kEmpty);
21 YACLIB_ASSERT(reinterpret_cast<std::uintptr_t>(&callback) != kResult);
22 std::uintptr_t expected = kEmpty;
23 return _callback.load(std::memory_order_acquire) == expected &&
24 _callback.compare_exchange_strong(expected, reinterpret_cast<std::uintptr_t>(&callback),
25 std::memory_order_release, std::memory_order_acquire);
26}
27
29 auto expected = _callback.load(std::memory_order_relaxed);
30 return expected != kResult && _callback.compare_exchange_strong(expected, kEmpty, std::memory_order_relaxed);
31}
32
34 if (!SetCallback(callback)) {
35 auto* next = callback.Here(*this);
36 YACLIB_ASSERT(next == nullptr);
37 }
38}
39
40template <bool SymmetricTransfer>
47
48template <bool SymmetricTransfer>
50 const auto expected = _callback.exchange(kResult, std::memory_order_acq_rel);
51 if (expected != kEmpty) {
53 auto* const callback = reinterpret_cast<InlineCore*>(expected);
54 YACLIB_ASSERT(callback != nullptr);
55 return Step<SymmetricTransfer>(*this, *callback);
56 }
58}
59
62
64 if (!callback._executor) {
65 YACLIB_ASSERT(_executor != nullptr);
66 callback._executor = std::move(_executor);
67 }
68}
69
70template BaseCore::Transfer<false> BaseCore::SetInline<false>(InlineCore& caller) noexcept;
71#if YACLIB_SYMMETRIC_TRANSFER != 0
72template BaseCore::Transfer<true> BaseCore::SetInline<true>(InlineCore& caller) noexcept;
73#endif
74
75template BaseCore::Transfer<false> BaseCore::SetResult<false>() noexcept;
76#if YACLIB_SYMMETRIC_TRANSFER != 0
77template BaseCore::Transfer<true> BaseCore::SetResult<true>() noexcept;
78#endif
79
80} // namespace yaclib::detail
void StoreCallback(InlineCore &callback) noexcept
Definition base_core.cpp:8
Transfer< SymmetricTransfer > SetInline(InlineCore &callback) noexcept
Definition base_core.cpp:41
void MoveExecutorTo(BaseCore &callback) noexcept
Definition base_core.cpp:63
bool SetCallback(InlineCore &callback) noexcept
Definition base_core.cpp:19
BaseCore(State callback) noexcept
Definition base_core.cpp:60
yaclib_std::atomic_uintptr_t _callback
Definition base_core.hpp:61
bool Reset() noexcept
Definition base_core.cpp:28
bool Empty() const noexcept
Definition base_core.cpp:13
Transfer< SymmetricTransfer > SetResult() noexcept
Definition base_core.cpp:49
void CallInline(InlineCore &callback) noexcept
Definition base_core.cpp:33
#define YACLIB_DEBUG(cond, message)
Definition log.hpp:84
#define YACLIB_ASSERT(cond)
Definition log.hpp:85
auto SetCallback(ResultCorePtr< Arg, E > &core, IExecutor *executor, Func &&f)
Definition core.hpp:304
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25