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_FIBER_H
8 #define BOOST_CONTEXT_FIBER_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 fiber_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 fiber_activation_record {
66 LPVOID fiber{ nullptr };
68 bool main_ctx{ true };
69 fiber_activation_record * from{ nullptr };
70 std::function< fiber_activation_record*(fiber_activation_record*&) > ontop{};
71 bool terminated{ false };
72 bool force_unwind{ false };
74 static fiber_activation_record *& current() noexcept;
76 // used for toplevel-context
77 // (e.g. main context, thread-entry context)
78 fiber_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 BOOST_ASSERT( ERROR_ALREADY_FIBER == ::GetLastError());
89 fiber = ::GetCurrentFiber();
90 BOOST_ASSERT( nullptr != fiber);
91 BOOST_ASSERT( reinterpret_cast< LPVOID >( 0x1E00) != fiber);
96 fiber_activation_record( stack_context sctx_) noexcept :
101 virtual ~fiber_activation_record() {
102 if ( BOOST_UNLIKELY( main_ctx) ) {
103 ::ConvertFiberToThread();
105 ::DeleteFiber( fiber);
109 fiber_activation_record( fiber_activation_record const&) = delete;
110 fiber_activation_record & operator=( fiber_activation_record const&) = delete;
112 bool is_main_context() const noexcept {
116 fiber_activation_record * resume() {
118 // store `this` in static, thread local pointer
119 // `this` will become the active (running) context
121 // context switch from parent context to `this`-context
123 ::SwitchToFiber( fiber);
124 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
125 return detail::exchange( current()->from, nullptr);
127 return std::exchange( current()->from, nullptr);
131 template< typename Ctx, typename Fn >
132 fiber_activation_record * resume_with( Fn && fn) {
134 // store `this` in static, thread local pointer
135 // `this` will become the active (running) context
136 // returned by fiber::current()
138 #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS)
139 current()->ontop = std::bind(
140 [](typename std::decay< Fn >::type & fn, fiber_activation_record *& ptr){
142 c = fn( std::move( c) );
146 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
147 return exchange( c.ptr_, nullptr);
149 return std::exchange( c.ptr_, nullptr);
152 std::forward< Fn >( fn),
153 std::placeholders::_1);
155 current()->ontop = [fn=std::forward<Fn>(fn)](fiber_activation_record *& ptr){
157 c = fn( std::move( c) );
161 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
162 return exchange( c.ptr_, nullptr);
164 return std::exchange( c.ptr_, nullptr);
169 ::SwitchToFiber( fiber);
170 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
171 return detail::exchange( current()->from, nullptr);
173 return std::exchange( current()->from, nullptr);
177 virtual void deallocate() noexcept {
181 struct BOOST_CONTEXT_DECL fiber_activation_record_initializer {
182 fiber_activation_record_initializer() noexcept;
183 ~fiber_activation_record_initializer();
186 struct forced_unwind {
187 fiber_activation_record * from{ nullptr };
188 #ifndef BOOST_ASSERT_IS_VOID
189 bool caught{ false };
192 explicit forced_unwind( fiber_activation_record * from_) :
196 #ifndef BOOST_ASSERT_IS_VOID
198 BOOST_ASSERT( caught);
203 template< typename Ctx, typename StackAlloc, typename Fn >
204 class fiber_capture_record : public fiber_activation_record {
206 typename std::decay< StackAlloc >::type salloc_;
207 typename std::decay< Fn >::type fn_;
209 static void destroy( fiber_capture_record * p) noexcept {
210 typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
211 stack_context sctx = p->sctx;
212 // deallocate activation record
213 p->~fiber_capture_record();
214 // destroy stack with stack allocator
215 salloc.deallocate( sctx);
219 fiber_capture_record( stack_context sctx, StackAlloc && salloc, Fn && fn) noexcept :
220 fiber_activation_record( sctx),
221 salloc_( std::forward< StackAlloc >( salloc)),
222 fn_( std::forward< Fn >( fn) ) {
225 void deallocate() noexcept override final {
226 BOOST_ASSERT( main_ctx || ( ! main_ctx && terminated) );
233 // invoke context-function
234 #if defined(BOOST_NO_CXX17_STD_INVOKE)
235 c = boost::context::detail::invoke( fn_, std::move( c) );
237 c = std::invoke( fn_, std::move( c) );
239 } catch ( forced_unwind const& ex) {
241 #ifndef BOOST_ASSERT_IS_VOID
242 const_cast< forced_unwind & >( ex).caught = true;
245 // this context has finished its task
249 force_unwind = false;
250 std::move( c).resume();
251 BOOST_ASSERT_MSG( false, "fiber already terminated");
255 template< typename Ctx, typename StackAlloc, typename Fn >
256 static fiber_activation_record * create_fiber1( StackAlloc && salloc, Fn && fn) {
257 typedef fiber_capture_record< Ctx, StackAlloc, Fn > capture_t;
259 auto sctx = salloc.allocate();
260 BOOST_ASSERT( ( sizeof( capture_t) ) < sctx.size);
261 // reserve space for control structure
262 void * storage = reinterpret_cast< void * >(
263 ( reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sizeof( capture_t) ) )
264 & ~ static_cast< uintptr_t >( 0xff) );
265 // placment new for control structure on context stack
266 capture_t * record = new ( storage) capture_t{
267 sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) };
268 // create user-context
269 record->fiber = ::CreateFiber( sctx.size, & detail::fiber_entry_func< capture_t >, record);
273 template< typename Ctx, typename StackAlloc, typename Fn >
274 static fiber_activation_record * create_fiber2( preallocated palloc, StackAlloc && salloc, Fn && fn) {
275 typedef fiber_capture_record< Ctx, StackAlloc, Fn > capture_t;
277 BOOST_ASSERT( ( sizeof( capture_t) ) < palloc.size);
278 // reserve space for control structure
279 void * storage = reinterpret_cast< void * >(
280 ( reinterpret_cast< uintptr_t >( palloc.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 palloc.sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) };
285 // create user-context
286 record->fiber = ::CreateFiber( palloc.sctx.size, & detail::fiber_entry_func< capture_t >, record);
292 class BOOST_CONTEXT_DECL fiber {
294 friend struct detail::fiber_activation_record;
296 template< typename Ctx, typename StackAlloc, typename Fn >
297 friend class detail::fiber_capture_record;
299 template< typename Ctx, typename StackAlloc, typename Fn >
300 friend detail::fiber_activation_record * detail::create_fiber1( StackAlloc &&, Fn &&);
302 template< typename Ctx, typename StackAlloc, typename Fn >
303 friend detail::fiber_activation_record * detail::create_fiber2( preallocated, StackAlloc &&, Fn &&);
305 template< typename StackAlloc, typename Fn >
307 callcc( std::allocator_arg_t, StackAlloc &&, Fn &&);
309 template< typename StackAlloc, typename Fn >
311 callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&);
313 detail::fiber_activation_record * ptr_{ nullptr };
315 fiber( detail::fiber_activation_record * ptr) noexcept :
322 template< typename Fn, typename = detail::disable_overload< fiber, Fn > >
324 fiber{ std::allocator_arg,
326 std::forward< Fn >( fn) } {
329 template< typename StackAlloc, typename Fn >
330 fiber( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) :
331 ptr_{ detail::create_fiber1< fiber >(
332 std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) } {;
335 template< typename StackAlloc, typename Fn >
336 fiber( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) :
337 ptr_{ detail::create_fiber2< fiber >(
338 palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) } {
342 if ( BOOST_UNLIKELY( nullptr != ptr_) && ! ptr_->main_ctx) {
343 if ( BOOST_LIKELY( ! ptr_->terminated) ) {
344 ptr_->force_unwind = true;
346 BOOST_ASSERT( ptr_->terminated);
352 fiber( fiber const&) = delete;
353 fiber & operator=( fiber const&) = delete;
355 fiber( fiber && other) noexcept {
359 fiber & operator=( fiber && other) noexcept {
360 if ( BOOST_LIKELY( this != & other) ) {
361 fiber tmp = std::move( other);
368 BOOST_ASSERT( nullptr != ptr_);
369 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
370 detail::fiber_activation_record * ptr = detail::exchange( ptr_, nullptr)->resume();
372 detail::fiber_activation_record * ptr = std::exchange( ptr_, nullptr)->resume();
374 if ( BOOST_UNLIKELY( detail::fiber_activation_record::current()->force_unwind) ) {
375 throw detail::forced_unwind{ ptr};
376 } else if ( BOOST_UNLIKELY( nullptr != detail::fiber_activation_record::current()->ontop) ) {
377 ptr = detail::fiber_activation_record::current()->ontop( ptr);
378 detail::fiber_activation_record::current()->ontop = nullptr;
383 template< typename Fn >
384 fiber resume_with( Fn && fn) && {
385 BOOST_ASSERT( nullptr != ptr_);
386 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
387 detail::fiber_activation_record * ptr =
388 detail::exchange( ptr_, nullptr)->resume_with< fiber >( std::forward< Fn >( fn) );
390 detail::fiber_activation_record * ptr =
391 std::exchange( ptr_, nullptr)->resume_with< fiber >( std::forward< Fn >( fn) );
393 if ( BOOST_UNLIKELY( detail::fiber_activation_record::current()->force_unwind) ) {
394 throw detail::forced_unwind{ ptr};
395 } else if ( BOOST_UNLIKELY( nullptr != detail::fiber_activation_record::current()->ontop) ) {
396 ptr = detail::fiber_activation_record::current()->ontop( ptr);
397 detail::fiber_activation_record::current()->ontop = nullptr;
402 explicit operator bool() const noexcept {
403 return nullptr != ptr_ && ! ptr_->terminated;
406 bool operator!() const noexcept {
407 return nullptr == ptr_ || ptr_->terminated;
410 bool operator<( fiber const& other) const noexcept {
411 return ptr_ < other.ptr_;
414 #if !defined(BOOST_EMBTC)
416 template< typename charT, class traitsT >
417 friend std::basic_ostream< charT, traitsT > &
418 operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
419 if ( nullptr != other.ptr_) {
420 return os << other.ptr_;
422 return os << "{not-a-context}";
428 template< typename charT, class traitsT >
429 friend std::basic_ostream< charT, traitsT > &
430 operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other);
434 void swap( fiber & other) noexcept {
435 std::swap( ptr_, other.ptr_);
439 #if defined(BOOST_EMBTC)
441 template< typename charT, class traitsT >
442 inline std::basic_ostream< charT, traitsT > &
443 operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
444 if ( nullptr != other.ptr_) {
445 return os << other.ptr_;
447 return os << "{not-a-context}";
454 void swap( fiber & l, fiber & r) noexcept {
458 typedef fiber fiber_context;
462 #if defined(BOOST_MSVC)
463 # pragma warning(pop)
466 #ifdef BOOST_HAS_ABI_HEADERS
467 # include BOOST_ABI_SUFFIX
470 #endif // BOOST_CONTEXT_FIBER_H