-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhen_all.h
More file actions
128 lines (120 loc) · 4.86 KB
/
when_all.h
File metadata and controls
128 lines (120 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#pragma once
/*
when_all.h - Implementation of a C++20 async coroutines when-all awaiter.
Copyright (c) 2023 Dirk O. Kaar. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "run_task.h"
#include "task_completion_source.h"
#include <vector>
namespace ghostl
{
template<typename T = void>
struct when_all final
{
static auto continuation(T result, size_t pos,
std::shared_ptr<std::vector<T>> results,
std::shared_ptr<std::atomic<size_t>> remaining,
ghostl::task_completion_source<std::vector<T>> tcs) -> void
{
(*results)[pos] = std::move(result);
if (auto isremaining = -- * remaining; !isremaining)
{
tcs.set_value(std::move(*results));
}
}
when_all() = delete;
template<typename C> explicit when_all(C&& _tasks)
{
C tasks = std::move(_tasks);
auto results = this->results;
auto remaining = this->remaining;
auto tcs = this->tcs;
for (ghostl::task<T>& task : tasks)
{
auto pos = remaining->load();
++*remaining;
auto runner = ghostl::run_task<T>(std::move(std::exchange(task, {})));
runner.continue_with([pos, results, remaining, tcs](T result) { continuation(result, pos, results, remaining, tcs); });
continuations->emplace_back(std::move(runner));
}
results->resize(remaining->load());
if (continuations->empty()) tcs.set_value(std::move(*results));
else for (auto& runner : *continuations)
{
runner.resume();
}
}
template<typename C> explicit when_all(const C& tasks) = delete;
when_all(const when_all& other) = delete;
when_all(when_all&& other) = delete;
when_all& operator=(when_all& other) = delete;
when_all& operator=(when_all&& other) = delete;
auto operator ()() {
return tcs.token();
}
private:
std::shared_ptr<std::vector<ghostl::run_task<T>>> continuations{
std::make_shared<std::vector<ghostl::run_task<T>>>() };
std::shared_ptr<std::vector<T>> results = std::make_shared<std::vector<T>>();
std::shared_ptr<std::atomic<size_t>> remaining{
std::make_shared<std::atomic<size_t>>(0) };
ghostl::task_completion_source<std::vector<T>> tcs{};
};
template<>
struct when_all<void> final
{
static auto continuation(
std::shared_ptr<std::atomic<size_t>> remaining,
ghostl::task_completion_source<> tcs) -> void
{
if (auto isremaining = -- * remaining; !isremaining)
{
tcs.set_value();
}
}
when_all() = delete;
template<typename C> explicit when_all(C&& _tasks)
{
C tasks = std::move(_tasks);
auto remaining = this->remaining;
auto tcs = this->tcs;
for (ghostl::task<>& task : tasks)
{
++*remaining;
auto runner = ghostl::run_task<>(std::move(std::exchange(task, {})));
runner.continue_with([remaining, tcs]() { continuation(remaining, tcs); });
continuations->emplace_back(std::move(runner));
}
if (continuations->empty()) tcs.set_value();
else for (auto& runner : *continuations)
{
runner.resume();
}
}
template<typename C> explicit when_all(const C& tasks) = delete;
when_all(const when_all& other) = delete;
when_all(when_all&& other) = delete;
when_all& operator=(when_all& other) = delete;
when_all& operator=(when_all&& other) = delete;
auto operator ()() {
return tcs.token();
}
private:
std::shared_ptr<std::vector<ghostl::run_task<>>> continuations{
std::make_shared<std::vector<ghostl::run_task<>>>() };
std::shared_ptr<std::atomic<size_t>> remaining{
std::make_shared<std::atomic<size_t>>(0) };
ghostl::task_completion_source<> tcs{};
};
} // namespace ghostl