-
Notifications
You must be signed in to change notification settings - Fork 8
/
p2_linear.cpp
243 lines (202 loc) · 5.58 KB
/
p2_linear.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <type_traits>
#include <utility>
#include <cassert>
#include <future>
#include <iostream>
#include <condition_variable>
#include <mutex>
#include <atomic>
#define FWD(...) ::std::forward<decltype(__VA_ARGS__)>(__VA_ARGS__)
struct nothing { };
template <typename T>
using void_to_nothing_t = std::conditional_t<std::is_void_v<T>, nothing, T>;
template <typename F, typename... Ts>
decltype(auto) returning_nothing_instead_of_void(F&& f, Ts&&... xs)
{
if constexpr(std::is_void_v<decltype(FWD(f)(FWD(xs)...))>)
{
FWD(f)(FWD(xs)...);
return nothing{};
}
else
{
return FWD(f)(FWD(xs)...);
}
}
template <typename F>
decltype(auto) call_ignoring_nothing(F&& f)
{
return returning_nothing_instead_of_void(FWD(f));
}
template <typename F, typename T, typename... Ts>
decltype(auto) call_ignoring_nothing(F&& f, T&& x, Ts&&... xs)
{
return call_ignoring_nothing([&f, &x](auto&&... ys) -> decltype(auto)
{
return std::forward<F>(f)(std::forward<T>(x), FWD(ys)...);
}, FWD(xs)...);
}
template <typename F, typename... Ts>
decltype(auto) call_ignoring_nothing(F&& f, nothing, Ts&&... xs)
{
return call_ignoring_nothing([&f](auto&&... ys) -> decltype(auto)
{
return std::forward<F>(f)(FWD(ys)...);
}, FWD(xs)...);
}
template <typename F, typename... Ts>
using result_of_ignoring_nothing_t =
decltype(call_ignoring_nothing(std::declval<F>(), std::declval<Ts>()...));
class bool_latch
{
private:
std::atomic<bool> _finished{false};
std::condition_variable _cv;
std::mutex _mtx;
public:
void count_down()
{
std::scoped_lock lk{_mtx};
_finished.store(true);
_cv.notify_all();
}
void wait()
{
std::unique_lock lk{_mtx};
_cv.wait(lk, [this]{ return _finished.load(); });
}
};
class root
{
template <typename, typename>
friend class node;
template <typename, typename...>
friend class when_all;
template <typename>
friend class schedule;
public:
// The `root` produces `nothing`.
using output_type = nothing;
private:
// When we are at the `root`, we cannot go "up" the chain anymore.
// Therefore we being going "down".
template <typename Scheduler, typename Child, typename... Children>
void walk_up(Scheduler&& s, Child& c, Children&... cs) &
{
c.execute(s, nothing{}, cs...);
}
};
template <typename Parent>
class child_of : public Parent
{
public:
using input_type = typename Parent::output_type;
protected:
template <typename ParentFwd>
child_of(ParentFwd&& p) : Parent{FWD(p)}
{
}
auto& as_parent() noexcept
{
return static_cast<Parent&>(*this);
}
};
template <typename Parent, typename F>
class node : private child_of<Parent>, private F
{
public:
using typename child_of<Parent>::input_type;
using output_type = result_of_ignoring_nothing_t<F&, input_type>;
template <typename ParentFwd, typename FFwd>
node(ParentFwd&& p, FFwd&& f) : child_of<Parent>{FWD(p)}, F{FWD(f)}
{
}
template <typename FThen>
auto then(FThen&& f_then) &&
{
return ::node{std::move(*this), FWD(f_then)};
}
private:
auto& as_f() noexcept
{
return static_cast<F&>(*this);
}
public:
template <typename Scheduler, typename Result>
void execute(Scheduler&&, Result&& r) &
{
call_ignoring_nothing(as_f(), FWD(r));
}
template <typename Scheduler, typename Result, typename Child, typename... Children>
void execute(Scheduler&& s, Result&& r, Child& c, Children&... cs) &
{
c.execute(s, call_ignoring_nothing(as_f(), FWD(r)), cs...);
}
template <typename Scheduler, typename... Children>
void walk_up(Scheduler&& s, Children&... cs) &
{
this->as_parent().walk_up(s, *this, cs...);
}
template <typename Scheduler>
decltype(auto) wait_and_get(Scheduler&& s) &&
{
output_type out;
bool_latch l;
auto f = std::move(*this).then([&](auto&&... x)
{
((out = FWD(x)), ...);
l.count_down();
});
f.walk_up(s);
l.wait();
return out;
}
};
template <typename ParentFwd, typename FFwd>
node(ParentFwd&&, FFwd&&) -> node<std::decay_t<ParentFwd>, std::decay_t<FFwd>>;
template <typename Parent>
class schedule : private child_of<Parent>
{
public:
using typename child_of<Parent>::input_type;
using output_type = nothing;
template <typename ParentFwd>
schedule(ParentFwd&& p) : child_of<Parent>{FWD(p)} { }
template <typename FThen>
auto then(FThen&& f_then) &&
{
return ::node{std::move(*this), FWD(f_then)};
}
template <typename Scheduler, typename Result, typename Child, typename... Children>
void execute(Scheduler&& s, Result&&, Child& c, Children&... cs) &
{
s([&]{ c.execute(s, nothing{}, cs...); });
}
template <typename Scheduler, typename... Children>
void walk_up(Scheduler&& s, Children&... cs) &
{
this->as_parent().walk_up(s, *this, cs...);
}
};
template <typename ParentFwd>
schedule(ParentFwd&&) -> schedule<std::decay_t<ParentFwd>>;
template <typename... Fs>
auto initiate(Fs&&... fs)
{
return schedule{root{}}.then(FWD(fs)...);
}
struct world_s_best_thread_pool
{
template <typename F>
void operator()(F&& f)
{
std::thread{FWD(f)}.detach();
}
};
int main()
{
auto f = initiate([]{ return 1; })
.then([](int x){ return x + 1; })
.then([](int x){ return x + 1; });
assert(std::move(f).wait_and_get(world_s_best_thread_pool{}) == 3);
}