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)
7 #ifndef BOOST_CONTEXT_CONTINUATION_H
8 #define BOOST_CONTEXT_CONTINUATION_H
12 #include <boost/context/detail/config.hpp>
22 #include <system_error>
26 #include <boost/assert.hpp>
27 #include <boost/config.hpp>
29 #include <boost/context/detail/disable_overload.hpp>
30 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
31 #include <boost/context/detail/exchange.hpp>
33 #if defined(BOOST_NO_CXX17_STD_INVOKE)
34 #include <boost/context/detail/invoke.hpp>
36 #include <boost/context/fixedsize_stack.hpp>
37 #include <boost/context/flags.hpp>
38 #include <boost/context/preallocated.hpp>
39 #include <boost/context/stack_context.hpp>
41 #ifdef BOOST_HAS_ABI_HEADERS
42 # include BOOST_ABI_PREFIX
45 #if defined(BOOST_MSVC)
46 # pragma warning(push)
47 # pragma warning(disable: 4702)
55 // entered if the execution context
56 // is resumed for the first time
57 template< typename Record >
58 static VOID WINAPI entry_func( LPVOID data) noexcept {
59 Record * record = static_cast< Record * >( data);
60 BOOST_ASSERT( nullptr != record);
61 // start execution of toplevel context-function
65 struct BOOST_CONTEXT_DECL activation_record {
66 LPVOID fiber{ nullptr };
68 bool main_ctx{ true };
69 activation_record * from{ nullptr };
70 std::function< activation_record*(activation_record*&) > ontop{};
71 bool terminated{ false };
72 bool force_unwind{ false };
74 static activation_record *& current() noexcept;
76 // used for toplevel-context
77 // (e.g. main context, thread-entry context)
78 activation_record() noexcept {
79 #if ( _WIN32_WINNT > 0x0600)
80 if ( ::IsThreadAFiber() ) {
81 fiber = ::GetCurrentFiber();
83 fiber = ::ConvertThreadToFiber( nullptr);
86 fiber = ::ConvertThreadToFiber( nullptr);
87 if ( BOOST_UNLIKELY( nullptr == fiber) ) {
88 DWORD err = ::GetLastError();
89 BOOST_ASSERT( ERROR_ALREADY_FIBER == err);
90 fiber = ::GetCurrentFiber();
91 BOOST_ASSERT( nullptr != fiber);
92 BOOST_ASSERT( reinterpret_cast< LPVOID >( 0x1E00) != fiber);
97 activation_record( stack_context sctx_) noexcept :
102 virtual ~activation_record() {
103 if ( BOOST_UNLIKELY( main_ctx) ) {
104 ::ConvertFiberToThread();
106 ::DeleteFiber( fiber);
110 activation_record( activation_record const&) = delete;
111 activation_record & operator=( activation_record const&) = delete;
113 bool is_main_context() const noexcept {
117 activation_record * resume() {
119 // store `this` in static, thread local pointer
120 // `this` will become the active (running) context
122 // context switch from parent context to `this`-context
124 ::SwitchToFiber( fiber);
125 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
126 return detail::exchange( current()->from, nullptr);
128 return std::exchange( current()->from, nullptr);
132 template< typename Ctx, typename Fn >
133 activation_record * resume_with( Fn && fn) {
135 // store `this` in static, thread local pointer
136 // `this` will become the active (running) context
137 // returned by continuation::current()
139 #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS)
140 current()->ontop = std::bind(
141 [](typename std::decay< Fn >::type & fn, activation_record *& ptr){
143 c = fn( std::move( c) );
147 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
148 return exchange( c.ptr_, nullptr);
150 return std::exchange( c.ptr_, nullptr);
153 std::forward< Fn >( fn),
154 std::placeholders::_1);
156 current()->ontop = [fn=std::forward<Fn>(fn)](activation_record *& ptr){
158 c = fn( std::move( c) );
162 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
163 return exchange( c.ptr_, nullptr);
165 return std::exchange( c.ptr_, nullptr);
170 ::SwitchToFiber( fiber);
171 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
172 return detail::exchange( current()->from, nullptr);
174 return std::exchange( current()->from, nullptr);
178 virtual void deallocate() noexcept {
182 struct BOOST_CONTEXT_DECL activation_record_initializer {
183 activation_record_initializer() noexcept;
184 ~activation_record_initializer();
187 struct forced_unwind {
188 activation_record * from{ nullptr };
190 explicit forced_unwind( activation_record * from_) :
195 template< typename Ctx, typename StackAlloc, typename Fn >
196 class capture_record : public activation_record {
198 typename std::decay< StackAlloc >::type salloc_;
199 typename std::decay< Fn >::type fn_;
201 static void destroy( capture_record * p) noexcept {
202 typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
203 stack_context sctx = p->sctx;
204 // deallocate activation record
205 p->~capture_record();
206 // destroy stack with stack allocator
207 salloc.deallocate( sctx);
211 capture_record( stack_context sctx, StackAlloc && salloc, Fn && fn) noexcept :
212 activation_record( sctx),
213 salloc_( std::forward< StackAlloc >( salloc)),
214 fn_( std::forward< Fn >( fn) ) {
217 void deallocate() noexcept override final {
218 BOOST_ASSERT( main_ctx || ( ! main_ctx && terminated) );
225 // invoke context-function
226 #if defined(BOOST_NO_CXX17_STD_INVOKE)
227 c = invoke( fn_, std::move( c) );
229 c = std::invoke( fn_, std::move( c) );
231 } catch ( forced_unwind const& ex) {
234 // this context has finished its task
238 force_unwind = false;
240 BOOST_ASSERT_MSG( false, "continuation already terminated");
244 template< typename Ctx, typename StackAlloc, typename Fn >
245 static activation_record * create_context1( StackAlloc && salloc, Fn && fn) {
246 typedef capture_record< Ctx, StackAlloc, Fn > capture_t;
248 auto sctx = salloc.allocate();
249 BOOST_ASSERT( ( sizeof( capture_t) ) < sctx.size);
250 // reserve space for control structure
251 void * storage = reinterpret_cast< void * >(
252 ( reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sizeof( capture_t) ) )
253 & ~ static_cast< uintptr_t >( 0xff) );
254 // placment new for control structure on context stack
255 capture_t * record = new ( storage) capture_t{
256 sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) };
257 // create user-context
258 record->fiber = ::CreateFiber( sctx.size, & detail::entry_func< capture_t >, record);
262 template< typename Ctx, typename StackAlloc, typename Fn >
263 static activation_record * create_context2( preallocated palloc, StackAlloc && salloc, Fn && fn) {
264 typedef capture_record< Ctx, StackAlloc, Fn > capture_t;
266 BOOST_ASSERT( ( sizeof( capture_t) ) < palloc.size);
267 // reserve space for control structure
268 void * storage = reinterpret_cast< void * >(
269 ( reinterpret_cast< uintptr_t >( palloc.sp) - static_cast< uintptr_t >( sizeof( capture_t) ) )
270 & ~ static_cast< uintptr_t >( 0xff) );
271 // placment new for control structure on context stack
272 capture_t * record = new ( storage) capture_t{
273 palloc.sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) };
274 // create user-context
275 record->fiber = ::CreateFiber( palloc.sctx.size, & detail::entry_func< capture_t >, record);
281 class BOOST_CONTEXT_DECL continuation {
283 friend struct detail::activation_record;
285 template< typename Ctx, typename StackAlloc, typename Fn >
286 friend class detail::capture_record;
288 template< typename Ctx, typename StackAlloc, typename Fn >
289 friend detail::activation_record * detail::create_context1( StackAlloc &&, Fn &&);
291 template< typename Ctx, typename StackAlloc, typename Fn >
292 friend detail::activation_record * detail::create_context2( preallocated, StackAlloc &&, Fn &&);
294 template< typename StackAlloc, typename Fn >
296 callcc( std::allocator_arg_t, StackAlloc &&, Fn &&);
298 template< typename StackAlloc, typename Fn >
300 callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&);
302 detail::activation_record * ptr_{ nullptr };
304 continuation( detail::activation_record * ptr) noexcept :
309 continuation() = default;
312 if ( BOOST_UNLIKELY( nullptr != ptr_) && ! ptr_->main_ctx) {
313 if ( BOOST_LIKELY( ! ptr_->terminated) ) {
314 ptr_->force_unwind = true;
316 BOOST_ASSERT( ptr_->terminated);
322 continuation( continuation const&) = delete;
323 continuation & operator=( continuation const&) = delete;
325 continuation( continuation && other) noexcept {
329 continuation & operator=( continuation && other) noexcept {
330 if ( BOOST_LIKELY( this != & other) ) {
331 continuation tmp = std::move( other);
337 continuation resume() {
338 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
339 detail::activation_record * ptr = detail::exchange( ptr_, nullptr)->resume();
341 detail::activation_record * ptr = std::exchange( ptr_, nullptr)->resume();
343 if ( BOOST_UNLIKELY( detail::activation_record::current()->force_unwind) ) {
344 throw detail::forced_unwind{ ptr};
345 } else if ( BOOST_UNLIKELY( nullptr != detail::activation_record::current()->ontop) ) {
346 ptr = detail::activation_record::current()->ontop( ptr);
347 detail::activation_record::current()->ontop = nullptr;
349 return continuation{ ptr };
352 template< typename Fn >
353 continuation resume_with( Fn && fn) {
354 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
355 detail::activation_record * ptr =
356 detail::exchange( ptr_, nullptr)->resume_with< continuation >( std::forward< Fn >( fn) );
358 detail::activation_record * ptr =
359 std::exchange( ptr_, nullptr)->resume_with< continuation >( std::forward< Fn >( fn) );
361 if ( BOOST_UNLIKELY( detail::activation_record::current()->force_unwind) ) {
362 throw detail::forced_unwind{ ptr};
363 } else if ( BOOST_UNLIKELY( nullptr != detail::activation_record::current()->ontop) ) {
364 ptr = detail::activation_record::current()->ontop( ptr);
365 detail::activation_record::current()->ontop = nullptr;
367 return continuation{ ptr };
370 explicit operator bool() const noexcept {
371 return nullptr != ptr_ && ! ptr_->terminated;
374 bool operator!() const noexcept {
375 return nullptr == ptr_ || ptr_->terminated;
378 bool operator==( continuation const& other) const noexcept {
379 return ptr_ == other.ptr_;
382 bool operator!=( continuation const& other) const noexcept {
383 return ptr_ != other.ptr_;
386 bool operator<( continuation const& other) const noexcept {
387 return ptr_ < other.ptr_;
390 bool operator>( continuation const& other) const noexcept {
391 return other.ptr_ < ptr_;
394 bool operator<=( continuation const& other) const noexcept {
395 return ! ( * this > other);
398 bool operator>=( continuation const& other) const noexcept {
399 return ! ( * this < other);
402 template< typename charT, class traitsT >
403 friend std::basic_ostream< charT, traitsT > &
404 operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other) {
405 if ( nullptr != other.ptr_) {
406 return os << other.ptr_;
408 return os << "{not-a-context}";
412 void swap( continuation & other) noexcept {
413 std::swap( ptr_, other.ptr_);
419 typename = detail::disable_overload< continuation, Fn >
426 std::forward< Fn >( fn) );
429 template< typename StackAlloc, typename Fn >
431 callcc( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) {
433 detail::create_context1< continuation >(
434 std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume();
437 template< typename StackAlloc, typename Fn >
439 callcc( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) {
441 detail::create_context2< continuation >(
442 palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume();
446 void swap( continuation & l, continuation & r) noexcept {
452 #if defined(BOOST_MSVC)
453 # pragma warning(pop)
456 #ifdef BOOST_HAS_ABI_HEADERS
457 # include BOOST_ABI_SUFFIX
460 #endif // BOOST_CONTEXT_CONTINUATION_H