]>
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/predef.h> | |
11 | #if BOOST_OS_MACOS | |
12 | #define _XOPEN_SOURCE 600 | |
13 | #endif | |
14 | ||
15 | extern "C" { | |
16 | #include <ucontext.h> | |
17 | } | |
18 | ||
19 | #include <boost/context/detail/config.hpp> | |
20 | ||
21 | #include <algorithm> | |
22 | #include <cstddef> | |
23 | #include <cstdint> | |
24 | #include <cstdlib> | |
25 | #include <cstring> | |
26 | #include <functional> | |
27 | #include <memory> | |
28 | #include <ostream> | |
29 | #include <system_error> | |
30 | #include <tuple> | |
31 | #include <utility> | |
32 | ||
33 | #include <boost/assert.hpp> | |
34 | #include <boost/config.hpp> | |
35 | ||
36 | #include <boost/context/detail/disable_overload.hpp> | |
37 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
38 | #include <boost/context/detail/exchange.hpp> | |
39 | #endif | |
11fdf7f2 | 40 | #include <boost/context/detail/externc.hpp> |
b32b8144 FG |
41 | #if defined(BOOST_NO_CXX17_STD_INVOKE) |
42 | #include <boost/context/detail/invoke.hpp> | |
43 | #endif | |
44 | #include <boost/context/fixedsize_stack.hpp> | |
45 | #include <boost/context/flags.hpp> | |
46 | #include <boost/context/preallocated.hpp> | |
47 | #if defined(BOOST_USE_SEGMENTED_STACKS) | |
48 | #include <boost/context/segmented_stack.hpp> | |
49 | #endif | |
50 | #include <boost/context/stack_context.hpp> | |
51 | ||
52 | #ifdef BOOST_HAS_ABI_HEADERS | |
53 | # include BOOST_ABI_PREFIX | |
54 | #endif | |
55 | ||
b32b8144 FG |
56 | namespace boost { |
57 | namespace context { | |
58 | namespace detail { | |
59 | ||
60 | // tampoline function | |
61 | // entered if the execution context | |
62 | // is resumed for the first time | |
63 | template< typename Record > | |
64 | static void entry_func( void * data) noexcept { | |
65 | Record * record = static_cast< Record * >( data); | |
66 | BOOST_ASSERT( nullptr != record); | |
67 | // start execution of toplevel context-function | |
68 | record->run(); | |
69 | } | |
70 | ||
71 | struct BOOST_CONTEXT_DECL activation_record { | |
72 | ucontext_t uctx{}; | |
73 | stack_context sctx{}; | |
74 | bool main_ctx{ true }; | |
75 | activation_record * from{ nullptr }; | |
76 | std::function< activation_record*(activation_record*&) > ontop{}; | |
77 | bool terminated{ false }; | |
78 | bool force_unwind{ false }; | |
79 | #if defined(BOOST_USE_ASAN) | |
80 | void * fake_stack{ nullptr }; | |
81 | void * stack_bottom{ nullptr }; | |
82 | std::size_t stack_size{ 0 }; | |
83 | #endif | |
84 | ||
85 | static activation_record *& current() noexcept; | |
86 | ||
87 | // used for toplevel-context | |
88 | // (e.g. main context, thread-entry context) | |
89 | activation_record() { | |
90 | if ( BOOST_UNLIKELY( 0 != ::getcontext( & uctx) ) ) { | |
91 | throw std::system_error( | |
92 | std::error_code( errno, std::system_category() ), | |
93 | "getcontext() failed"); | |
94 | } | |
95 | } | |
96 | ||
97 | activation_record( stack_context sctx_) noexcept : | |
98 | sctx( sctx_ ), | |
99 | main_ctx( false ) { | |
100 | } | |
101 | ||
102 | virtual ~activation_record() { | |
103 | } | |
104 | ||
105 | activation_record( activation_record const&) = delete; | |
106 | activation_record & operator=( activation_record const&) = delete; | |
107 | ||
108 | bool is_main_context() const noexcept { | |
109 | return main_ctx; | |
110 | } | |
111 | ||
112 | activation_record * resume() { | |
113 | from = current(); | |
114 | // store `this` in static, thread local pointer | |
115 | // `this` will become the active (running) context | |
116 | current() = this; | |
117 | #if defined(BOOST_USE_SEGMENTED_STACKS) | |
118 | // adjust segmented stack properties | |
119 | __splitstack_getcontext( from->sctx.segments_ctx); | |
120 | __splitstack_setcontext( sctx.segments_ctx); | |
121 | #endif | |
122 | #if defined(BOOST_USE_ASAN) | |
123 | if ( terminated) { | |
124 | __sanitizer_start_switch_fiber( nullptr, stack_bottom, stack_size); | |
125 | } else { | |
126 | __sanitizer_start_switch_fiber( & from->fake_stack, stack_bottom, stack_size); | |
127 | } | |
128 | #endif | |
129 | // context switch from parent context to `this`-context | |
130 | ::swapcontext( & from->uctx, & uctx); | |
131 | #if defined(BOOST_USE_ASAN) | |
132 | __sanitizer_finish_switch_fiber( current()->fake_stack, | |
133 | (const void **) & current()->from->stack_bottom, | |
134 | & current()->from->stack_size); | |
135 | #endif | |
136 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
137 | return exchange( current()->from, nullptr); | |
138 | #else | |
139 | return std::exchange( current()->from, nullptr); | |
140 | #endif | |
141 | } | |
142 | ||
143 | template< typename Ctx, typename Fn > | |
144 | activation_record * resume_with( Fn && fn) { | |
145 | from = current(); | |
146 | // store `this` in static, thread local pointer | |
147 | // `this` will become the active (running) context | |
148 | // returned by continuation::current() | |
149 | current() = this; | |
150 | #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) | |
151 | current()->ontop = std::bind( | |
152 | [](typename std::decay< Fn >::type & fn, activation_record *& ptr){ | |
153 | Ctx c{ ptr }; | |
154 | c = fn( std::move( c) ); | |
155 | if ( ! c) { | |
156 | ptr = nullptr; | |
157 | } | |
158 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
159 | return exchange( c.ptr_, nullptr); | |
160 | #else | |
161 | return std::exchange( c.ptr_, nullptr); | |
162 | #endif | |
163 | }, | |
164 | std::forward< Fn >( fn), | |
165 | std::placeholders::_1); | |
166 | #else | |
167 | current()->ontop = [fn=std::forward<Fn>(fn)](activation_record *& ptr){ | |
168 | Ctx c{ ptr }; | |
169 | c = fn( std::move( c) ); | |
170 | if ( ! c) { | |
171 | ptr = nullptr; | |
172 | } | |
173 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
174 | return exchange( c.ptr_, nullptr); | |
175 | #else | |
176 | return std::exchange( c.ptr_, nullptr); | |
177 | #endif | |
178 | }; | |
179 | #endif | |
180 | #if defined(BOOST_USE_SEGMENTED_STACKS) | |
181 | // adjust segmented stack properties | |
182 | __splitstack_getcontext( from->sctx.segments_ctx); | |
183 | __splitstack_setcontext( sctx.segments_ctx); | |
184 | #endif | |
185 | #if defined(BOOST_USE_ASAN) | |
186 | __sanitizer_start_switch_fiber( & from->fake_stack, stack_bottom, stack_size); | |
187 | #endif | |
188 | // context switch from parent context to `this`-context | |
189 | ::swapcontext( & from->uctx, & uctx); | |
190 | #if defined(BOOST_USE_ASAN) | |
191 | __sanitizer_finish_switch_fiber( current()->fake_stack, | |
192 | (const void **) & current()->from->stack_bottom, | |
193 | & current()->from->stack_size); | |
194 | #endif | |
195 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
196 | return exchange( current()->from, nullptr); | |
197 | #else | |
198 | return std::exchange( current()->from, nullptr); | |
199 | #endif | |
200 | } | |
201 | ||
202 | virtual void deallocate() noexcept { | |
203 | } | |
204 | }; | |
205 | ||
206 | struct BOOST_CONTEXT_DECL activation_record_initializer { | |
207 | activation_record_initializer() noexcept; | |
208 | ~activation_record_initializer(); | |
209 | }; | |
210 | ||
211 | struct forced_unwind { | |
92f5a8d4 | 212 | activation_record * from{ nullptr }; |
b32b8144 FG |
213 | |
214 | forced_unwind( activation_record * from_) noexcept : | |
215 | from{ from_ } { | |
216 | } | |
217 | }; | |
218 | ||
219 | template< typename Ctx, typename StackAlloc, typename Fn > | |
220 | class capture_record : public activation_record { | |
221 | private: | |
222 | typename std::decay< StackAlloc >::type salloc_; | |
223 | typename std::decay< Fn >::type fn_; | |
224 | ||
225 | static void destroy( capture_record * p) noexcept { | |
226 | typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_); | |
227 | stack_context sctx = p->sctx; | |
228 | // deallocate activation record | |
229 | p->~capture_record(); | |
230 | // destroy stack with stack allocator | |
231 | salloc.deallocate( sctx); | |
232 | } | |
233 | ||
234 | public: | |
235 | capture_record( stack_context sctx, StackAlloc && salloc, Fn && fn) noexcept : | |
236 | activation_record{ sctx }, | |
237 | salloc_{ std::forward< StackAlloc >( salloc) }, | |
238 | fn_( std::forward< Fn >( fn) ) { | |
239 | } | |
240 | ||
241 | void deallocate() noexcept override final { | |
242 | BOOST_ASSERT( main_ctx || ( ! main_ctx && terminated) ); | |
243 | destroy( this); | |
244 | } | |
245 | ||
246 | void run() { | |
247 | #if defined(BOOST_USE_ASAN) | |
248 | __sanitizer_finish_switch_fiber( fake_stack, | |
249 | (const void **) & from->stack_bottom, | |
250 | & from->stack_size); | |
251 | #endif | |
252 | Ctx c{ from }; | |
253 | try { | |
254 | // invoke context-function | |
255 | #if defined(BOOST_NO_CXX17_STD_INVOKE) | |
11fdf7f2 | 256 | c = boost::context::detail::invoke( fn_, std::move( c) ); |
b32b8144 FG |
257 | #else |
258 | c = std::invoke( fn_, std::move( c) ); | |
259 | #endif | |
260 | } catch ( forced_unwind const& ex) { | |
261 | c = Ctx{ ex.from }; | |
262 | } | |
263 | // this context has finished its task | |
264 | from = nullptr; | |
265 | ontop = nullptr; | |
266 | terminated = true; | |
267 | force_unwind = false; | |
268 | c.resume(); | |
269 | BOOST_ASSERT_MSG( false, "continuation already terminated"); | |
270 | } | |
271 | }; | |
272 | ||
273 | template< typename Ctx, typename StackAlloc, typename Fn > | |
274 | static activation_record * create_context1( StackAlloc && salloc, Fn && fn) { | |
275 | typedef capture_record< Ctx, StackAlloc, Fn > capture_t; | |
276 | ||
277 | auto sctx = salloc.allocate(); | |
278 | // reserve space for control structure | |
279 | void * storage = reinterpret_cast< void * >( | |
280 | ( reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sizeof( capture_t) ) ) | |
281 | & ~ static_cast< uintptr_t >( 0xff) ); | |
282 | // placment new for control structure on context stack | |
283 | capture_t * record = new ( storage) capture_t{ | |
284 | sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) }; | |
285 | // stack bottom | |
286 | void * stack_bottom = reinterpret_cast< void * >( | |
287 | reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sctx.size) ); | |
288 | // create user-context | |
289 | if ( BOOST_UNLIKELY( 0 != ::getcontext( & record->uctx) ) ) { | |
92f5a8d4 TL |
290 | record->~capture_t(); |
291 | salloc.deallocate( sctx); | |
b32b8144 FG |
292 | throw std::system_error( |
293 | std::error_code( errno, std::system_category() ), | |
294 | "getcontext() failed"); | |
295 | } | |
296 | record->uctx.uc_stack.ss_sp = stack_bottom; | |
297 | // 64byte gap between control structure and stack top | |
298 | record->uctx.uc_stack.ss_size = reinterpret_cast< uintptr_t >( storage) - | |
299 | reinterpret_cast< uintptr_t >( stack_bottom) - static_cast< uintptr_t >( 64); | |
300 | record->uctx.uc_link = nullptr; | |
301 | ::makecontext( & record->uctx, ( void (*)() ) & entry_func< capture_t >, 1, record); | |
302 | #if defined(BOOST_USE_ASAN) | |
303 | record->stack_bottom = record->uctx.uc_stack.ss_sp; | |
304 | record->stack_size = record->uctx.uc_stack.ss_size; | |
305 | #endif | |
306 | return record; | |
307 | } | |
308 | ||
309 | template< typename Ctx, typename StackAlloc, typename Fn > | |
310 | static activation_record * create_context2( preallocated palloc, StackAlloc && salloc, Fn && fn) { | |
311 | typedef capture_record< Ctx, StackAlloc, Fn > capture_t; | |
312 | ||
313 | // reserve space for control structure | |
314 | void * storage = reinterpret_cast< void * >( | |
315 | ( reinterpret_cast< uintptr_t >( palloc.sp) - static_cast< uintptr_t >( sizeof( capture_t) ) ) | |
316 | & ~ static_cast< uintptr_t >( 0xff) ); | |
317 | // placment new for control structure on context stack | |
318 | capture_t * record = new ( storage) capture_t{ | |
319 | palloc.sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) }; | |
320 | // stack bottom | |
321 | void * stack_bottom = reinterpret_cast< void * >( | |
322 | reinterpret_cast< uintptr_t >( palloc.sctx.sp) - static_cast< uintptr_t >( palloc.sctx.size) ); | |
323 | // create user-context | |
324 | if ( BOOST_UNLIKELY( 0 != ::getcontext( & record->uctx) ) ) { | |
92f5a8d4 TL |
325 | record->~capture_t(); |
326 | salloc.deallocate( palloc.sctx); | |
b32b8144 FG |
327 | throw std::system_error( |
328 | std::error_code( errno, std::system_category() ), | |
329 | "getcontext() failed"); | |
330 | } | |
331 | record->uctx.uc_stack.ss_sp = stack_bottom; | |
332 | // 64byte gap between control structure and stack top | |
333 | record->uctx.uc_stack.ss_size = reinterpret_cast< uintptr_t >( storage) - | |
334 | reinterpret_cast< uintptr_t >( stack_bottom) - static_cast< uintptr_t >( 64); | |
335 | record->uctx.uc_link = nullptr; | |
336 | ::makecontext( & record->uctx, ( void (*)() ) & entry_func< capture_t >, 1, record); | |
337 | #if defined(BOOST_USE_ASAN) | |
338 | record->stack_bottom = record->uctx.uc_stack.ss_sp; | |
339 | record->stack_size = record->uctx.uc_stack.ss_size; | |
340 | #endif | |
341 | return record; | |
342 | } | |
343 | ||
344 | } | |
345 | ||
346 | class BOOST_CONTEXT_DECL continuation { | |
347 | private: | |
348 | friend struct detail::activation_record; | |
349 | ||
350 | template< typename Ctx, typename StackAlloc, typename Fn > | |
351 | friend class detail::capture_record; | |
352 | ||
353 | template< typename Ctx, typename StackAlloc, typename Fn > | |
354 | friend detail::activation_record * detail::create_context1( StackAlloc &&, Fn &&); | |
355 | ||
356 | template< typename Ctx, typename StackAlloc, typename Fn > | |
357 | friend detail::activation_record * detail::create_context2( preallocated, StackAlloc &&, Fn &&); | |
358 | ||
359 | template< typename StackAlloc, typename Fn > | |
360 | friend continuation | |
361 | callcc( std::allocator_arg_t, StackAlloc &&, Fn &&); | |
362 | ||
363 | template< typename StackAlloc, typename Fn > | |
364 | friend continuation | |
365 | callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&); | |
366 | ||
367 | detail::activation_record * ptr_{ nullptr }; | |
368 | ||
369 | continuation( detail::activation_record * ptr) noexcept : | |
370 | ptr_{ ptr } { | |
371 | } | |
372 | ||
373 | public: | |
374 | continuation() = default; | |
375 | ||
376 | ~continuation() { | |
377 | if ( BOOST_UNLIKELY( nullptr != ptr_) && ! ptr_->main_ctx) { | |
378 | if ( BOOST_LIKELY( ! ptr_->terminated) ) { | |
379 | ptr_->force_unwind = true; | |
380 | ptr_->resume(); | |
381 | BOOST_ASSERT( ptr_->terminated); | |
382 | } | |
383 | ptr_->deallocate(); | |
384 | } | |
385 | } | |
386 | ||
387 | continuation( continuation const&) = delete; | |
388 | continuation & operator=( continuation const&) = delete; | |
389 | ||
390 | continuation( continuation && other) noexcept { | |
391 | swap( other); | |
392 | } | |
393 | ||
394 | continuation & operator=( continuation && other) noexcept { | |
395 | if ( BOOST_LIKELY( this != & other) ) { | |
396 | continuation tmp = std::move( other); | |
397 | swap( tmp); | |
398 | } | |
399 | return * this; | |
400 | } | |
401 | ||
11fdf7f2 TL |
402 | continuation resume() & { |
403 | return std::move( * this).resume(); | |
404 | } | |
405 | ||
406 | continuation resume() && { | |
b32b8144 FG |
407 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) |
408 | detail::activation_record * ptr = detail::exchange( ptr_, nullptr)->resume(); | |
409 | #else | |
410 | detail::activation_record * ptr = std::exchange( ptr_, nullptr)->resume(); | |
411 | #endif | |
412 | if ( BOOST_UNLIKELY( detail::activation_record::current()->force_unwind) ) { | |
413 | throw detail::forced_unwind{ ptr}; | |
414 | } else if ( BOOST_UNLIKELY( nullptr != detail::activation_record::current()->ontop) ) { | |
415 | ptr = detail::activation_record::current()->ontop( ptr); | |
416 | detail::activation_record::current()->ontop = nullptr; | |
417 | } | |
11fdf7f2 TL |
418 | return { ptr }; |
419 | } | |
420 | ||
421 | template< typename Fn > | |
422 | continuation resume_with( Fn && fn) & { | |
423 | return std::move( * this).resume_with( std::forward< Fn >( fn) ); | |
b32b8144 FG |
424 | } |
425 | ||
426 | template< typename Fn > | |
11fdf7f2 | 427 | continuation resume_with( Fn && fn) && { |
b32b8144 FG |
428 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) |
429 | detail::activation_record * ptr = | |
430 | detail::exchange( ptr_, nullptr)->resume_with< continuation >( std::forward< Fn >( fn) ); | |
431 | #else | |
432 | detail::activation_record * ptr = | |
433 | std::exchange( ptr_, nullptr)->resume_with< continuation >( std::forward< Fn >( fn) ); | |
434 | #endif | |
435 | if ( BOOST_UNLIKELY( detail::activation_record::current()->force_unwind) ) { | |
436 | throw detail::forced_unwind{ ptr}; | |
437 | } else if ( BOOST_UNLIKELY( nullptr != detail::activation_record::current()->ontop) ) { | |
438 | ptr = detail::activation_record::current()->ontop( ptr); | |
439 | detail::activation_record::current()->ontop = nullptr; | |
440 | } | |
11fdf7f2 | 441 | return { ptr }; |
b32b8144 FG |
442 | } |
443 | ||
444 | explicit operator bool() const noexcept { | |
445 | return nullptr != ptr_ && ! ptr_->terminated; | |
446 | } | |
447 | ||
448 | bool operator!() const noexcept { | |
449 | return nullptr == ptr_ || ptr_->terminated; | |
450 | } | |
451 | ||
b32b8144 FG |
452 | bool operator<( continuation const& other) const noexcept { |
453 | return ptr_ < other.ptr_; | |
454 | } | |
455 | ||
20effc67 TL |
456 | #if !defined(BOOST_EMBTC) |
457 | ||
b32b8144 FG |
458 | template< typename charT, class traitsT > |
459 | friend std::basic_ostream< charT, traitsT > & | |
460 | operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other) { | |
461 | if ( nullptr != other.ptr_) { | |
462 | return os << other.ptr_; | |
463 | } else { | |
464 | return os << "{not-a-context}"; | |
465 | } | |
466 | } | |
467 | ||
20effc67 TL |
468 | #else |
469 | ||
470 | template< typename charT, class traitsT > | |
471 | friend std::basic_ostream< charT, traitsT > & | |
472 | operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other); | |
473 | ||
474 | #endif | |
475 | ||
b32b8144 FG |
476 | void swap( continuation & other) noexcept { |
477 | std::swap( ptr_, other.ptr_); | |
478 | } | |
479 | }; | |
480 | ||
20effc67 TL |
481 | #if defined(BOOST_EMBTC) |
482 | ||
483 | template< typename charT, class traitsT > | |
484 | inline std::basic_ostream< charT, traitsT > & | |
485 | operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other) { | |
486 | if ( nullptr != other.ptr_) { | |
487 | return os << other.ptr_; | |
488 | } else { | |
489 | return os << "{not-a-context}"; | |
490 | } | |
491 | } | |
492 | ||
493 | #endif | |
494 | ||
b32b8144 FG |
495 | template< |
496 | typename Fn, | |
497 | typename = detail::disable_overload< continuation, Fn > | |
498 | > | |
499 | continuation | |
500 | callcc( Fn && fn) { | |
501 | return callcc( | |
502 | std::allocator_arg, | |
503 | #if defined(BOOST_USE_SEGMENTED_STACKS) | |
504 | segmented_stack(), | |
505 | #else | |
506 | fixedsize_stack(), | |
507 | #endif | |
508 | std::forward< Fn >( fn) ); | |
509 | } | |
510 | ||
511 | template< typename StackAlloc, typename Fn > | |
512 | continuation | |
513 | callcc( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) { | |
514 | return continuation{ | |
515 | detail::create_context1< continuation >( | |
516 | std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume(); | |
517 | } | |
518 | ||
519 | template< typename StackAlloc, typename Fn > | |
520 | continuation | |
521 | callcc( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) { | |
522 | return continuation{ | |
523 | detail::create_context2< continuation >( | |
524 | palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume(); | |
525 | } | |
526 | ||
527 | inline | |
528 | void swap( continuation & l, continuation & r) noexcept { | |
529 | l.swap( r); | |
530 | } | |
531 | ||
532 | }} | |
533 | ||
534 | #ifdef BOOST_HAS_ABI_HEADERS | |
535 | # include BOOST_ABI_SUFFIX | |
536 | #endif | |
537 | ||
538 | #endif // BOOST_CONTEXT_CONTINUATION_H |