YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
all.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <
yaclib_std/detail/atomic.hpp
>
4
5
#include <
yaclib/async/promise.hpp
>
6
#include <
yaclib/util/combinator_strategy.hpp
>
7
#include <
yaclib/util/fail_policy.hpp
>
8
#include <
yaclib/util/result.hpp
>
9
#include <
yaclib/util/type_traits.hpp
>
10
11
#include <vector>
12
13
namespace
yaclib::when
{
14
15
template
<FailPolicy F,
typename
OutputValue,
typename
OutputError,
typename
InputCore>
16
struct
All
{
17
static_assert
(F !=
FailPolicy::LastFail
,
"LastFail policy is not supported by All"
);
18
};
19
20
template
<
typename
OutputValue,
typename
OutputError,
typename
InputCore>
21
struct
All
<
FailPolicy
::
None
, OutputValue,
OutputError
,
InputCore
> {
22
using
PromiseType
=
Promise<OutputValue, OutputError>
;
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
49
template
<
typename
OutputValue,
typename
OutputError,
typename
InputCore>
50
struct
All
<
FailPolicy
::
FirstFail
, OutputValue,
OutputError
,
InputCore
> {
51
using
PromiseType
=
Promise<OutputValue, OutputError>
;
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
yaclib::Promise< OutputValue, OutputError >
combinator_strategy.hpp
fail_policy.hpp
yaclib::when
Definition
all.hpp:13
yaclib_std::atomic_bool
atomic< bool > atomic_bool
Definition
atomic.hpp:41
yaclib::FailPolicy
FailPolicy
This Policy describe how algorithm interpret if Future will be fulfilled by fail (exception or error)
Definition
fail_policy.hpp:12
yaclib::FailPolicy::FirstFail
@ FirstFail
yaclib::FailPolicy::LastFail
@ LastFail
yaclib::ConsumePolicy
ConsumePolicy
Definition
combinator_strategy.hpp:24
yaclib::ConsumePolicy::Unordered
@ Unordered
yaclib::ConsumePolicy::None
@ None
yaclib::MakeContract
Contract< V, E > MakeContract()
Creates related future and promise.
Definition
contract.hpp:25
yaclib::CorePolicy
CorePolicy
Definition
combinator_strategy.hpp:35
yaclib::CorePolicy::Owned
@ Owned
yaclib::ResultState::Error
@ Error
yaclib::ResultState::Exception
@ Exception
promise.hpp
result.hpp
atomic.hpp
yaclib::when::All< FailPolicy::FirstFail, OutputValue, OutputError, InputCore >::~All
~All()
Definition
all.hpp:75
yaclib::when::All< FailPolicy::FirstFail, OutputValue, OutputError, InputCore >::All
All(std::size_t count, PromiseType p)
Definition
all.hpp:56
yaclib::when::All< FailPolicy::FirstFail, OutputValue, OutputError, InputCore >::Register
void Register(std::size_t i, InputCore &core)
Definition
all.hpp:60
yaclib::when::All< FailPolicy::FirstFail, OutputValue, OutputError, InputCore >::Consume
void Consume(InputCore &core)
Definition
all.hpp:64
yaclib::when::All< FailPolicy::None, OutputValue, OutputError, InputCore >::~All
~All()
Definition
all.hpp:35
yaclib::when::All< FailPolicy::None, OutputValue, OutputError, InputCore >::All
All(std::size_t count, PromiseType p)
Definition
all.hpp:27
yaclib::when::All< FailPolicy::None, OutputValue, OutputError, InputCore >::Register
void Register(std::size_t i, InputCore &core)
Definition
all.hpp:31
yaclib::when::All
Definition
all.hpp:16
type_traits.hpp
include
yaclib
async
when
all.hpp
Generated by
1.9.8