/
context.hpp
395 lines (324 loc) · 12 KB
/
context.hpp
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright (c) 2024 Klemens D. Morgenstern
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_COBALT_EXPERIMENTAL_CONTEXT_HPP
#define BOOST_COBALT_EXPERIMENTAL_CONTEXT_HPP
#include <boost/callable_traits/args.hpp>
#include <boost/context/fiber.hpp>
#include <boost/context/fixedsize_stack.hpp>
#include <boost/cobalt/concepts.hpp>
#include <boost/cobalt/config.hpp>
#include <coroutine>
#include <new>
// this is all UB according to the standard. BUT it shouldn't be!
namespace boost::cobalt::experimental
{
template<typename, typename ...>
struct context;
namespace detail
{
template<typename Promise>
struct fiber_frame
{
void (*resume_) (fiber_frame *) = +[](fiber_frame * ff) { ff->resume();};
void (*destroy_)(fiber_frame *) = +[](fiber_frame * ff) { ff->destroy();};
Promise promise;
boost::context::fiber caller, callee;
void (*after_resume)(fiber_frame *, void *) = nullptr;
void * after_resume_p;
template<typename ... Args>
requires std::constructible_from<Promise, Args...>
fiber_frame(Args && ... args) : promise(args...) {}
template<typename ... Args>
requires (!std::constructible_from<Promise, Args...> && std::is_default_constructible_v<Promise>)
fiber_frame(Args && ...) {}
void resume()
{
callee = std::move(callee).resume();
if (auto af = std::exchange(after_resume, nullptr))
af(this, after_resume_p);
}
void destroy()
{
auto c = std::exchange(callee, {});
this->~fiber_frame();
}
template<typename Awaitable>
auto do_resume(void * )
{
return +[](fiber_frame * this_, void * p)
{
auto aw_ = static_cast<Awaitable*>(p);
auto h = std::coroutine_handle<Promise>::from_address(this_) ;
aw_->await_suspend(h);
};
}
template<typename Awaitable>
auto do_resume(bool * )
{
return +[](fiber_frame * this_, void * p)
{
auto aw_ = static_cast<Awaitable*>(p);
auto h = std::coroutine_handle<Promise>::from_address(this_) ;
if (!aw_->await_suspend(h))
h.resume();
};
}
template<typename Awaitable, typename Promise_>
auto do_resume(std::coroutine_handle<Promise_> * )
{
return +[](fiber_frame * this_, void * p)
{
auto aw_ = static_cast<Awaitable*>(p);
auto h = std::coroutine_handle<Promise>::from_address(this_) ;
aw_->await_suspend(h).resume();
};
}
template<typename Awaitable>
auto do_await(Awaitable aw)
{
if (!aw.await_ready())
{
after_resume_p = & aw;
after_resume = do_resume<Awaitable>(
static_cast<decltype(aw.await_suspend(std::declval<std::coroutine_handle<Promise>>()))*>(nullptr)
);
caller = std::move(caller).resume();
}
return aw.await_resume();
}
template<typename Handle, typename ... Args>
context<Handle, Args...> get_context()
{
return context<Handle, Args...>{this};
}
};
template<typename Traits, typename Promise, typename ... Args>
struct stack_allocator : boost::context::fixedsize_stack {};
template<typename Traits, typename Promise, typename ... Args>
requires requires {Promise::operator new(std::size_t{});}
struct stack_allocator<Traits, Promise, Args...>
{
boost::context::stack_context allocate()
{
const auto size = Traits::default_size();
const auto p = Promise::operator new(size);
boost::context::stack_context sctx;
sctx.size = size;
sctx.sp = static_cast< char * >( p) + sctx.size;
return sctx;
}
void deallocate( boost::context::stack_context & sctx) noexcept
{
void * vp = static_cast< char * >( sctx.sp) - sctx.size;
Promise::operator delete(vp, sctx.size);
}
};
template<typename Traits, typename Promise, typename ... Args>
requires requires {Promise::operator new(std::size_t{}, std::decay_t<Args&>()...);}
struct stack_allocator<Traits, Promise, Args...>
{
std::tuple<Args&...> args;
boost::context::stack_context allocate()
{
const auto size = Traits::default_size();
const auto p = std::apply(
[size](auto & ... args_)
{
return Promise::operator new(size, args_...);
}, args);
boost::context::stack_context sctx;
sctx.size = size;
sctx.sp = static_cast< char * >( p) + sctx.size;
return sctx;
}
void deallocate( boost::context::stack_context & sctx) noexcept
{
void * vp = static_cast< char * >( sctx.sp) - sctx.size;
Promise::operator delete(vp, sctx.size);
}
};
struct await_transform_base
{
struct dummy {};
void await_transform(dummy);
};
template<typename T>
struct await_transform_impl : await_transform_base, T
{
};
template<typename T>
concept has_await_transform = ! requires (await_transform_impl<T> & p) {p.await_transform(await_transform_base::dummy{});};
template<typename Promise, typename Context, typename Func, typename ... Args>
void do_return(std::true_type /* is_void */, Promise& promise, Context ctx, Func && func, Args && ... args)
{
std::forward<Func>(func)(ctx, std::forward<Args>(args)...);
promise.return_void();
}
template<typename Promise, typename Context, typename Func, typename ... Args>
void do_return(std::false_type /* is_void */, Promise& promise, Context ctx, Func && func, Args && ... args)
{
promise.return_value(std::forward<Func>(func)(ctx, std::forward<Args>(args)...));
}
}
template<typename Return, typename ... Args>
struct context
{
using return_type = Return;
using promise_type = typename std::coroutine_traits<Return, Args...>::promise_type;
promise_type & promise() {return frame_->promise;}
const promise_type & promise() const {return frame_->promise;}
template<typename Return_, typename ... Args_>
requires std::same_as<promise_type, typename context<Return_, Args_...>::promise_type>
constexpr operator context<Return_, Args_...>() const
{
return {frame_};
}
template<typename Awaitable>
requires (detail::has_await_transform<promise_type> &&
requires (promise_type & pro, Awaitable && aw)
{
{pro.await_transform(std::forward<Awaitable>(aw))} -> awaitable_type<promise_type>;
})
auto await(Awaitable && aw)
{
return frame_->do_await(frame_->promise.await_transform(std::forward<Awaitable>(aw)));
}
template<typename Awaitable>
requires (detail::has_await_transform<promise_type> &&
requires (promise_type & pro, Awaitable && aw)
{
{pro.await_transform(std::forward<Awaitable>(aw).operator co_await())} -> awaitable_type<promise_type>;
})
auto await(Awaitable && aw)
{
return frame_->do_await(frame_->promise.await_transform(std::forward<Awaitable>(aw)).operator co_await());
}
template<typename Awaitable>
requires (detail::has_await_transform<promise_type> &&
requires (promise_type & pro, Awaitable && aw)
{
{operator co_await(pro.await_transform(std::forward<Awaitable>(aw)))} -> awaitable_type<promise_type>;
})
auto await(Awaitable && aw)
{
return frame_->do_await(operator co_await(frame_->promise.await_transform(std::forward<Awaitable>(aw))));
}
template<awaitable_type<promise_type> Awaitable>
requires (!detail::has_await_transform<promise_type> )
auto await(Awaitable && aw)
{
return frame_->do_await(std::forward<Awaitable>(aw));
}
template<typename Awaitable>
requires (!detail::has_await_transform<promise_type>
&& requires (Awaitable && aw) {{operator co_await(std::forward<Awaitable>(aw))} -> awaitable_type<promise_type>;})
auto await(Awaitable && aw)
{
return frame_->do_await(operator co_await(std::forward<Awaitable>(aw)));
}
template<typename Awaitable>
requires (!detail::has_await_transform<promise_type>
&& requires (Awaitable && aw) {{std::forward<Awaitable>(aw).operator co_await()} -> awaitable_type<promise_type>;})
auto await(Awaitable && aw)
{
return frame_->do_await(std::forward<Awaitable>(aw).operator co_await());
}
template<typename Yield>
requires requires (promise_type & pro, Yield && value) {{pro.yield_value(std::forward<Yield>(value))} -> awaitable_type<promise_type>;}
auto yield(Yield && value)
{
frame_->do_await(frame_->promise.yield_value(std::forward<Yield>(value)));
}
private:
context(detail::fiber_frame<promise_type> * frame) : frame_(frame) {}
template<typename, typename ...>
friend struct context;
//template<typename >
friend struct detail::fiber_frame<promise_type>;
detail::fiber_frame<promise_type> * frame_;
};
template<typename Return, typename ... Args, std::invocable<context<Return, Args...>, Args...> Func, typename StackAlloc>
auto make_context(Func && func, std::allocator_arg_t, StackAlloc && salloc, Args && ... args)
{
auto sctx_ = salloc.allocate();
using promise_type = typename std::coroutine_traits<Return, Args...>::promise_type;
void * p = static_cast<char*>(sctx_.sp) - sizeof(detail::fiber_frame<promise_type>);
auto sz = sctx_.size - sizeof(detail::fiber_frame<promise_type>);
if (auto diff = reinterpret_cast<std::uintptr_t>(p) % alignof(detail::fiber_frame<promise_type>); diff != 0u)
{
p = static_cast<char*>(p) - diff;
sz -= diff;
}
boost::context::preallocated psc{p, sz, sctx_};
auto f = new (p) detail::fiber_frame<promise_type>(args...);
auto res = f->promise.get_return_object();
constexpr auto is_always_lazy =
requires (promise_type & pro) {{pro.initial_suspend()} -> std::same_as<std::suspend_always>;}
&& noexcept(f->promise.initial_suspend());
struct invoker
{
detail::fiber_frame<promise_type> * frame;
mutable Func func;
mutable std::tuple<Args...> args;
invoker(detail::fiber_frame<promise_type> * frame, Func && func, Args && ... args)
: frame(frame), func(std::forward<Func>(func)), args(std::forward<Args>(args)...)
{
}
boost::context::fiber operator()(boost::context::fiber && f) const
{
auto & promise = frame->promise;
frame->caller = std::move(f);
try
{
if (!is_always_lazy)
frame->do_await(promise.initial_suspend());
std::apply(
[&](auto && ... args_)
{
auto ctx = frame->template get_context<Return, Args...>();
using return_type = decltype(std::forward<Func>(func)(ctx, std::forward<Args>(args_)...));
detail::do_return(std::is_void<return_type>{}, frame->promise, ctx,
std::forward<Func>(func), std::forward<Args>(args_)...);
},
std::move(args));
}
catch (boost::context::detail::forced_unwind &) { throw; }
catch (...) {promise.unhandled_exception();}
static_assert(noexcept(promise.final_suspend()));
frame->do_await(promise.final_suspend());
return std::move(frame->caller);
}
};
f->callee = boost::context::fiber{
std::allocator_arg, psc, std::forward<StackAlloc>(salloc),
invoker(f, std::forward<Func>(func), std::forward<Args>(args)...)};
if constexpr (is_always_lazy)
f->promise.initial_suspend();
else
f->resume();
return res;
}
template<typename Return, typename ... Args, std::invocable<context<Return, Args...>, Args...> Func>
auto make_context(Func && func, Args && ... args)
{
return make_context<Return>(std::forward<Func>(func), std::allocator_arg,
boost::context::fixedsize_stack(), std::forward<Args>(args)...);
}
template<typename ... Args, typename Func, typename StackAlloc>
auto make_context(Func && func, std::allocator_arg_t, StackAlloc && salloc, Args && ... args)
{
return make_context<typename std::tuple_element_t<0u, callable_traits::args_t<Func>>::return_type>(
std::forward<Func>(func), std::allocator_arg, std::forward<StackAlloc>(salloc), std::forward<Args>(args)...
);
}
template<typename ... Args, typename Func>
auto make_context(Func && func, Args && ... args)
{
return make_context<typename std::tuple_element_t<0u, callable_traits::args_t<Func>>::return_type>(
std::forward<Func>(func), std::forward<Args>(args)...
);
}
}
#endif //BOOST_COBALT_EXPERIMENTAL_CONTEXT_HPP