YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
all.hpp
Go to the documentation of this file.
1#pragma once
2
4
10
11#include <vector>
12
13namespace yaclib::when {
14
15template <FailPolicy F, typename OutputValue, typename OutputError, typename InputCore>
16struct All {
17 static_assert(F != FailPolicy::LastFail, "LastFail policy is not supported by All");
18};
19
20template <typename OutputValue, typename OutputError, typename InputCore>
21struct All<FailPolicy::None, OutputValue, OutputError, InputCore> {
23
24 static constexpr ConsumePolicy kConsumePolicy = ConsumePolicy::None;
25 static constexpr CorePolicy kCorePolicy = CorePolicy::Owned;
26
27 All(std::size_t count, PromiseType p) : _p{std::move(p)} {
28 _cores.resize(count);
29 }
30
31 void Register(std::size_t i, InputCore& core) {
32 _cores[i] = &core;
33 }
34
35 ~All() {
36 OutputValue output;
37 output.reserve(_cores.size());
38 for (auto* core : _cores) {
39 output.push_back(core->Retire());
40 }
41 std::move(_p).Set(std::move(output));
42 }
43
44 private:
45 std::vector<InputCore*> _cores;
46 PromiseType _p;
47};
48
49template <typename OutputValue, typename OutputError, typename InputCore>
50struct All<FailPolicy::FirstFail, OutputValue, OutputError, InputCore> {
52
53 static constexpr ConsumePolicy kConsumePolicy = ConsumePolicy::Unordered;
54 static constexpr CorePolicy kCorePolicy = CorePolicy::Owned;
55
56 All(std::size_t count, PromiseType p) : _p{std::move(p)} {
57 _cores.resize(count);
58 }
59
60 void Register(std::size_t i, InputCore& core) {
61 _cores[i] = &core;
62 }
63
64 void Consume(InputCore& core) {
65 auto& result = core.Get();
66 if (!result && !_done.load(std::memory_order_relaxed) && !_done.exchange(true, std::memory_order_acq_rel)) {
67 if (result.State() == ResultState::Exception) {
68 std::move(_p).Set(std::as_const(result).Exception());
69 } else {
70 std::move(_p).Set(std::as_const(result).Error());
71 }
72 }
73 }
74
75 ~All() {
76 if (_p.Valid()) {
77 OutputValue result;
78 result.reserve(_cores.size());
79 for (auto* core : _cores) {
80 result.push_back(core->Retire().Value());
81 }
82 std::move(_p).Set(std::move(result));
83 } else {
84 for (auto* core : _cores) {
85 core->DecRef();
86 }
87 }
88 }
89
90 private:
91 std::vector<InputCore*> _cores;
92 yaclib_std::atomic_bool _done = false;
93 PromiseType _p;
94};
95
96} // namespace yaclib::when
atomic< bool > atomic_bool
Definition atomic.hpp:41
FailPolicy
This Policy describe how algorithm interpret if Future will be fulfilled by fail (exception or error)
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25