-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhen_any.h
More file actions
122 lines (114 loc) · 4.46 KB
/
when_any.h
File metadata and controls
122 lines (114 loc) · 4.46 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
#pragma once
/*
when_any.h - Implementation of a C++20 async coroutines when-any 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_any final
{
static auto continuation(T result,
std::shared_ptr<std::atomic<bool>> completed,
ghostl::task_completion_source<T> tcs) -> void
{
if (auto isCompleted = completed->exchange(true); !isCompleted)
{
tcs.set_value(std::move(result));
}
}
when_any() = delete;
template<typename C> explicit when_any(C&& _tasks)
{
C tasks = std::move(_tasks);
auto completed = this->completed;
auto tcs = this->tcs;
for (ghostl::task<T>& task : tasks)
{
auto runner = ghostl::run_task<T>(std::move(std::exchange(task, {})));
runner.continue_with([completed, tcs](T result) { continuation(result, completed, 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_any(const C& tasks) = delete;
when_any(const when_any& other) = delete;
when_any(when_any&& other) = delete;
~when_any() { }
when_any& operator=(when_any& other) = delete;
when_any& operator=(when_any&& 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::atomic<bool>> completed{
std::make_shared<std::atomic<bool>>(false) };
ghostl::task_completion_source<T> tcs{};
};
template<>
struct when_any<void> final
{
static auto continuation(
std::shared_ptr<std::atomic<bool>> completed,
ghostl::task_completion_source<> tcs) -> void
{
if (auto isCompleted = completed->exchange(true); !isCompleted)
{
tcs.set_value();
}
}
when_any() = delete;
template<typename C> explicit when_any(C&& _tasks)
{
C tasks = std::move(_tasks);
auto completed = this->completed;
auto tcs = this->tcs;
for (ghostl::task<>& task : tasks)
{
auto runner = ghostl::run_task<>(std::move(std::exchange(task, {})));
runner.continue_with([completed, tcs]() { continuation(completed, 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_any(const C& tasks) = delete;
when_any(const when_any& other) = delete;
when_any(when_any&& other) = delete;
~when_any() { }
when_any& operator=(when_any& other) = delete;
when_any& operator=(when_any&& 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<bool>> completed{
std::make_shared<std::atomic<bool>>(false) };
ghostl::task_completion_source<> tcs{};
};
} // namespace ghostl