]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | |
2 | // Copyright Oliver Kowalke 2017. | |
3 | // Distributed under the Boost Software License, Version 1.0. | |
4 | // (See accompanying file LICENSE_1_0.txt or copy at | |
5 | // http://www.boost.org/LICENSE_1_0.txt) | |
6 | ||
7 | #ifndef BOOST_CONTEXT_CONTINUATION_H | |
8 | #define BOOST_CONTEXT_CONTINUATION_H | |
9 | ||
10 | #include <boost/context/detail/config.hpp> | |
11 | ||
12 | #include <algorithm> | |
13 | #include <cstddef> | |
14 | #include <cstdint> | |
15 | #include <cstdlib> | |
16 | #include <exception> | |
17 | #include <functional> | |
18 | #include <memory> | |
19 | #include <ostream> | |
20 | #include <tuple> | |
21 | #include <utility> | |
22 | ||
23 | #include <boost/assert.hpp> | |
24 | #include <boost/config.hpp> | |
25 | #include <boost/intrusive_ptr.hpp> | |
26 | ||
27 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
28 | #include <boost/context/detail/exchange.hpp> | |
29 | #endif | |
30 | #if defined(BOOST_NO_CXX17_STD_INVOKE) | |
31 | #include <boost/context/detail/invoke.hpp> | |
32 | #endif | |
33 | #include <boost/context/detail/disable_overload.hpp> | |
34 | #include <boost/context/detail/exception.hpp> | |
35 | #include <boost/context/detail/fcontext.hpp> | |
36 | #include <boost/context/detail/tuple.hpp> | |
37 | #include <boost/context/fixedsize_stack.hpp> | |
38 | #include <boost/context/flags.hpp> | |
39 | #include <boost/context/preallocated.hpp> | |
40 | #include <boost/context/segmented_stack.hpp> | |
41 | #include <boost/context/stack_context.hpp> | |
42 | ||
43 | #ifdef BOOST_HAS_ABI_HEADERS | |
44 | # include BOOST_ABI_PREFIX | |
45 | #endif | |
46 | ||
47 | #if defined(BOOST_MSVC) | |
48 | # pragma warning(push) | |
49 | # pragma warning(disable: 4702) | |
50 | #endif | |
51 | ||
52 | namespace boost { | |
53 | namespace context { | |
54 | namespace detail { | |
55 | ||
56 | inline | |
57 | transfer_t context_unwind( transfer_t t) { | |
58 | throw forced_unwind( t.fctx); | |
59 | return { nullptr, nullptr }; | |
60 | } | |
61 | ||
62 | template< typename Rec > | |
63 | transfer_t context_exit( transfer_t t) noexcept { | |
64 | Rec * rec = static_cast< Rec * >( t.data); | |
65 | // destroy context stack | |
66 | rec->deallocate(); | |
67 | return { nullptr, nullptr }; | |
68 | } | |
69 | ||
70 | template< typename Rec > | |
71 | void context_entry( transfer_t t) noexcept { | |
72 | // transfer control structure to the context-stack | |
73 | Rec * rec = static_cast< Rec * >( t.data); | |
74 | BOOST_ASSERT( nullptr != t.fctx); | |
75 | BOOST_ASSERT( nullptr != rec); | |
76 | try { | |
77 | // jump back to `create_context()` | |
78 | t = jump_fcontext( t.fctx, nullptr); | |
79 | // start executing | |
80 | t.fctx = rec->run( t.fctx); | |
81 | } catch ( forced_unwind const& e) { | |
82 | t = { e.fctx, nullptr }; | |
83 | } | |
84 | BOOST_ASSERT( nullptr != t.fctx); | |
85 | // destroy context-stack of `this`context on next context | |
86 | ontop_fcontext( t.fctx, rec, context_exit< Rec >); | |
87 | BOOST_ASSERT_MSG( false, "context already terminated"); | |
88 | } | |
89 | ||
90 | template< typename Ctx, typename Fn > | |
91 | transfer_t context_ontop( transfer_t t) { | |
92 | auto p = static_cast< std::tuple< Fn > * >( t.data); | |
93 | BOOST_ASSERT( nullptr != p); | |
94 | typename std::decay< Fn >::type fn = std::get< 0 >( * p); | |
95 | t.data = nullptr; | |
96 | Ctx c{ t.fctx }; | |
97 | // execute function, pass continuation via reference | |
98 | c = fn( std::move( c) ); | |
99 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
100 | return { exchange( c.fctx_, nullptr), nullptr }; | |
101 | #else | |
102 | return { std::exchange( c.fctx_, nullptr), nullptr }; | |
103 | #endif | |
104 | } | |
105 | ||
106 | template< typename Ctx, typename StackAlloc, typename Fn > | |
107 | class record { | |
108 | private: | |
109 | stack_context sctx_; | |
110 | typename std::decay< StackAlloc >::type salloc_; | |
111 | typename std::decay< Fn >::type fn_; | |
112 | ||
113 | static void destroy( record * p) noexcept { | |
114 | typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_); | |
115 | stack_context sctx = p->sctx_; | |
116 | // deallocate record | |
117 | p->~record(); | |
118 | // destroy stack with stack allocator | |
119 | salloc.deallocate( sctx); | |
120 | } | |
121 | ||
122 | public: | |
123 | record( stack_context sctx, StackAlloc && salloc, | |
124 | Fn && fn) noexcept : | |
125 | sctx_( sctx), | |
126 | salloc_( std::forward< StackAlloc >( salloc)), | |
127 | fn_( std::forward< Fn >( fn) ) { | |
128 | } | |
129 | ||
130 | record( record const&) = delete; | |
131 | record & operator=( record const&) = delete; | |
132 | ||
133 | void deallocate() noexcept { | |
134 | destroy( this); | |
135 | } | |
136 | ||
137 | fcontext_t run( fcontext_t fctx) { | |
138 | Ctx c{ fctx }; | |
139 | // invoke context-function | |
140 | #if defined(BOOST_NO_CXX17_STD_INVOKE) | |
11fdf7f2 | 141 | c = boost::context::detail::invoke( fn_, std::move( c) ); |
b32b8144 FG |
142 | #else |
143 | c = std::invoke( fn_, std::move( c) ); | |
144 | #endif | |
145 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
146 | return exchange( c.fctx_, nullptr); | |
147 | #else | |
148 | return std::exchange( c.fctx_, nullptr); | |
149 | #endif | |
150 | } | |
151 | }; | |
152 | ||
153 | template< typename Record, typename StackAlloc, typename Fn > | |
154 | fcontext_t create_context1( StackAlloc && salloc, Fn && fn) { | |
155 | auto sctx = salloc.allocate(); | |
156 | // reserve space for control structure | |
157 | void * storage = reinterpret_cast< void * >( | |
158 | ( reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sizeof( Record) ) ) | |
159 | & ~static_cast< uintptr_t >( 0xff) ); | |
160 | // placment new for control structure on context stack | |
161 | Record * record = new ( storage) Record{ | |
162 | sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) }; | |
163 | // 64byte gab between control structure and stack top | |
164 | // should be 16byte aligned | |
165 | void * stack_top = reinterpret_cast< void * >( | |
166 | reinterpret_cast< uintptr_t >( storage) - static_cast< uintptr_t >( 64) ); | |
167 | void * stack_bottom = reinterpret_cast< void * >( | |
168 | reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sctx.size) ); | |
169 | // create fast-context | |
170 | const std::size_t size = reinterpret_cast< uintptr_t >( stack_top) - reinterpret_cast< uintptr_t >( stack_bottom); | |
171 | const fcontext_t fctx = make_fcontext( stack_top, size, & context_entry< Record >); | |
172 | BOOST_ASSERT( nullptr != fctx); | |
173 | // transfer control structure to context-stack | |
174 | return jump_fcontext( fctx, record).fctx; | |
175 | } | |
176 | ||
177 | template< typename Record, typename StackAlloc, typename Fn > | |
178 | fcontext_t create_context2( preallocated palloc, StackAlloc && salloc, Fn && fn) { | |
179 | // reserve space for control structure | |
180 | void * storage = reinterpret_cast< void * >( | |
181 | ( reinterpret_cast< uintptr_t >( palloc.sp) - static_cast< uintptr_t >( sizeof( Record) ) ) | |
182 | & ~ static_cast< uintptr_t >( 0xff) ); | |
183 | // placment new for control structure on context-stack | |
184 | Record * record = new ( storage) Record{ | |
185 | palloc.sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) }; | |
186 | // 64byte gab between control structure and stack top | |
187 | void * stack_top = reinterpret_cast< void * >( | |
188 | reinterpret_cast< uintptr_t >( storage) - static_cast< uintptr_t >( 64) ); | |
189 | void * stack_bottom = reinterpret_cast< void * >( | |
190 | reinterpret_cast< uintptr_t >( palloc.sctx.sp) - static_cast< uintptr_t >( palloc.sctx.size) ); | |
191 | // create fast-context | |
192 | const std::size_t size = reinterpret_cast< uintptr_t >( stack_top) - reinterpret_cast< uintptr_t >( stack_bottom); | |
193 | const fcontext_t fctx = make_fcontext( stack_top, size, & context_entry< Record >); | |
194 | BOOST_ASSERT( nullptr != fctx); | |
195 | // transfer control structure to context-stack | |
196 | return jump_fcontext( fctx, record).fctx; | |
197 | } | |
198 | ||
199 | } | |
200 | ||
201 | class continuation { | |
202 | private: | |
203 | template< typename Ctx, typename StackAlloc, typename Fn > | |
204 | friend class detail::record; | |
205 | ||
206 | template< typename Ctx, typename Fn > | |
207 | friend detail::transfer_t | |
208 | detail::context_ontop( detail::transfer_t); | |
209 | ||
210 | template< typename StackAlloc, typename Fn > | |
211 | friend continuation | |
212 | callcc( std::allocator_arg_t, StackAlloc &&, Fn &&); | |
213 | ||
214 | template< typename StackAlloc, typename Fn > | |
215 | friend continuation | |
216 | callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&); | |
217 | ||
218 | detail::fcontext_t fctx_{ nullptr }; | |
219 | ||
220 | continuation( detail::fcontext_t fctx) noexcept : | |
221 | fctx_{ fctx } { | |
222 | } | |
223 | ||
224 | public: | |
225 | continuation() noexcept = default; | |
226 | ||
227 | ~continuation() { | |
228 | if ( BOOST_UNLIKELY( nullptr != fctx_) ) { | |
229 | detail::ontop_fcontext( | |
230 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
231 | detail::exchange( fctx_, nullptr), | |
232 | #else | |
233 | std::exchange( fctx_, nullptr), | |
234 | #endif | |
235 | nullptr, | |
236 | detail::context_unwind); | |
237 | } | |
238 | } | |
239 | ||
240 | continuation( continuation && other) noexcept { | |
11fdf7f2 | 241 | swap( other); |
b32b8144 FG |
242 | } |
243 | ||
244 | continuation & operator=( continuation && other) noexcept { | |
245 | if ( BOOST_LIKELY( this != & other) ) { | |
246 | continuation tmp = std::move( other); | |
247 | swap( tmp); | |
248 | } | |
249 | return * this; | |
250 | } | |
251 | ||
252 | continuation( continuation const& other) noexcept = delete; | |
253 | continuation & operator=( continuation const& other) noexcept = delete; | |
254 | ||
11fdf7f2 TL |
255 | continuation resume() & { |
256 | return std::move( * this).resume(); | |
257 | } | |
258 | ||
259 | continuation resume() && { | |
b32b8144 | 260 | BOOST_ASSERT( nullptr != fctx_); |
11fdf7f2 | 261 | return { detail::jump_fcontext( |
b32b8144 FG |
262 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) |
263 | detail::exchange( fctx_, nullptr), | |
264 | #else | |
265 | std::exchange( fctx_, nullptr), | |
266 | #endif | |
11fdf7f2 TL |
267 | nullptr).fctx }; |
268 | } | |
269 | ||
270 | template< typename Fn > | |
271 | continuation resume_with( Fn && fn) & { | |
272 | return std::move( * this).resume_with( std::forward< Fn >( fn) ); | |
b32b8144 FG |
273 | } |
274 | ||
275 | template< typename Fn > | |
11fdf7f2 | 276 | continuation resume_with( Fn && fn) && { |
b32b8144 FG |
277 | BOOST_ASSERT( nullptr != fctx_); |
278 | auto p = std::make_tuple( std::forward< Fn >( fn) ); | |
11fdf7f2 | 279 | return { detail::ontop_fcontext( |
b32b8144 FG |
280 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) |
281 | detail::exchange( fctx_, nullptr), | |
282 | #else | |
283 | std::exchange( fctx_, nullptr), | |
284 | #endif | |
285 | & p, | |
11fdf7f2 | 286 | detail::context_ontop< continuation, Fn >).fctx }; |
b32b8144 FG |
287 | } |
288 | ||
289 | explicit operator bool() const noexcept { | |
290 | return nullptr != fctx_; | |
291 | } | |
292 | ||
293 | bool operator!() const noexcept { | |
294 | return nullptr == fctx_; | |
295 | } | |
296 | ||
297 | bool operator==( continuation const& other) const noexcept { | |
298 | return fctx_ == other.fctx_; | |
299 | } | |
300 | ||
301 | bool operator!=( continuation const& other) const noexcept { | |
302 | return fctx_ != other.fctx_; | |
303 | } | |
304 | ||
305 | bool operator<( continuation const& other) const noexcept { | |
306 | return fctx_ < other.fctx_; | |
307 | } | |
308 | ||
309 | bool operator>( continuation const& other) const noexcept { | |
310 | return other.fctx_ < fctx_; | |
311 | } | |
312 | ||
313 | bool operator<=( continuation const& other) const noexcept { | |
314 | return ! ( * this > other); | |
315 | } | |
316 | ||
317 | bool operator>=( continuation const& other) const noexcept { | |
318 | return ! ( * this < other); | |
319 | } | |
320 | ||
321 | template< typename charT, class traitsT > | |
322 | friend std::basic_ostream< charT, traitsT > & | |
323 | operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other) { | |
324 | if ( nullptr != other.fctx_) { | |
325 | return os << other.fctx_; | |
326 | } else { | |
327 | return os << "{not-a-context}"; | |
328 | } | |
329 | } | |
330 | ||
331 | void swap( continuation & other) noexcept { | |
332 | std::swap( fctx_, other.fctx_); | |
333 | } | |
334 | }; | |
335 | ||
336 | template< | |
337 | typename Fn, | |
338 | typename = detail::disable_overload< continuation, Fn > | |
339 | > | |
340 | continuation | |
341 | callcc( Fn && fn) { | |
342 | return callcc( | |
343 | std::allocator_arg, fixedsize_stack(), | |
344 | std::forward< Fn >( fn) ); | |
345 | } | |
346 | ||
347 | template< typename StackAlloc, typename Fn > | |
348 | continuation | |
349 | callcc( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) { | |
350 | using Record = detail::record< continuation, StackAlloc, Fn >; | |
351 | return continuation{ | |
352 | detail::create_context1< Record >( | |
353 | std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume(); | |
354 | } | |
355 | ||
356 | template< typename StackAlloc, typename Fn > | |
357 | continuation | |
358 | callcc( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) { | |
359 | using Record = detail::record< continuation, StackAlloc, Fn >; | |
360 | return continuation{ | |
361 | detail::create_context2< Record >( | |
362 | palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume(); | |
363 | } | |
364 | ||
365 | #if defined(BOOST_USE_SEGMENTED_STACKS) | |
366 | template< typename Fn > | |
367 | continuation | |
368 | callcc( std::allocator_arg_t, segmented_stack, Fn &&); | |
369 | ||
370 | template< typename StackAlloc, typename Fn > | |
371 | continuation | |
372 | callcc( std::allocator_arg_t, preallocated, segmented_stack, Fn &&); | |
373 | #endif | |
374 | ||
375 | inline | |
376 | void swap( continuation & l, continuation & r) noexcept { | |
377 | l.swap( r); | |
378 | } | |
379 | ||
380 | }} | |
381 | ||
382 | #if defined(BOOST_MSVC) | |
383 | # pragma warning(pop) | |
384 | #endif | |
385 | ||
386 | #ifdef BOOST_HAS_ABI_HEADERS | |
387 | # include BOOST_ABI_SUFFIX | |
388 | #endif | |
389 | ||
390 | #endif // BOOST_CONTEXT_CONTINUATION_H |