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_FIBER_H
8 #define BOOST_CONTEXT_FIBER_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
fiber_unwind(transfer_t t)57 transfer_t fiber_unwind( transfer_t t) {
58     throw forced_unwind( t.fctx);
59     return { nullptr, nullptr };
60 }
61 
62 template< typename Rec >
fiber_exit(transfer_t t)63 transfer_t fiber_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 >
fiber_entry(transfer_t t)71 void fiber_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& ex) {
82         t = { ex.fctx, nullptr };
83 #ifndef BOOST_ASSERT_IS_VOID
84         const_cast< forced_unwind & >( ex).caught = true;
85 #endif
86     }
87     BOOST_ASSERT( nullptr != t.fctx);
88     // destroy context-stack of `this`context on next context
89     ontop_fcontext( t.fctx, rec, fiber_exit< Rec >);
90     BOOST_ASSERT_MSG( false, "context already terminated");
91 }
92 
93 template< typename Ctx, typename Fn >
fiber_ontop(transfer_t t)94 transfer_t fiber_ontop( transfer_t t) {
95     BOOST_ASSERT( nullptr != t.data);
96     auto p = *static_cast< Fn * >( t.data);
97     t.data = nullptr;
98     // execute function, pass fiber via reference
99     Ctx c = p( Ctx{ t.fctx } );
100 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
101     return { exchange( c.fctx_, nullptr), nullptr };
102 #else
103     return { std::exchange( c.fctx_, nullptr), nullptr };
104 #endif
105 }
106 
107 template< typename Ctx, typename StackAlloc, typename Fn >
108 class fiber_record {
109 private:
110     stack_context                                       sctx_;
111     typename std::decay< StackAlloc >::type             salloc_;
112     typename std::decay< Fn >::type                     fn_;
113 
destroy(fiber_record * p)114     static void destroy( fiber_record * p) noexcept {
115         typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
116         stack_context sctx = p->sctx_;
117         // deallocate fiber_record
118         p->~fiber_record();
119         // destroy stack with stack allocator
120         salloc.deallocate( sctx);
121     }
122 
123 public:
fiber_record(stack_context sctx,StackAlloc && salloc,Fn && fn)124     fiber_record( stack_context sctx, StackAlloc && salloc,
125             Fn && fn) noexcept :
126         sctx_( sctx),
127         salloc_( std::forward< StackAlloc >( salloc)),
128         fn_( std::forward< Fn >( fn) ) {
129     }
130 
131     fiber_record( fiber_record const&) = delete;
132     fiber_record & operator=( fiber_record const&) = delete;
133 
deallocate()134     void deallocate() noexcept {
135         destroy( this);
136     }
137 
run(fcontext_t fctx)138     fcontext_t run( fcontext_t fctx) {
139         // invoke context-function
140 #if defined(BOOST_NO_CXX17_STD_INVOKE)
141         Ctx c = boost::context::detail::invoke( fn_, Ctx{ fctx } );
142 #else
143         Ctx c = std::invoke( fn_, Ctx{ fctx } );
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 >
create_fiber1(StackAlloc && salloc,Fn && fn)154 fcontext_t create_fiber1( 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, & fiber_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 >
create_fiber2(preallocated palloc,StackAlloc && salloc,Fn && fn)178 fcontext_t create_fiber2( 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, & fiber_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 fiber {
202 private:
203     template< typename Ctx, typename StackAlloc, typename Fn >
204     friend class detail::fiber_record;
205 
206     template< typename Ctx, typename Fn >
207     friend detail::transfer_t
208     detail::fiber_ontop( detail::transfer_t);
209 
210     template< typename StackAlloc, typename Fn >
211     friend fiber
212     callcc( std::allocator_arg_t, StackAlloc &&, Fn &&);
213 
214     template< typename StackAlloc, typename Fn >
215     friend fiber
216     callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&);
217 
218     detail::fcontext_t  fctx_{ nullptr };
219 
fiber(detail::fcontext_t fctx)220     fiber( detail::fcontext_t fctx) noexcept :
221         fctx_{ fctx } {
222     }
223 
224 public:
225     fiber() noexcept = default;
226 
227     template< typename Fn, typename = detail::disable_overload< fiber, Fn > >
fiber(Fn && fn)228     fiber( Fn && fn) :
229         fiber{ std::allocator_arg, fixedsize_stack(), std::forward< Fn >( fn) } {
230     }
231 
232     template< typename StackAlloc, typename Fn >
fiber(std::allocator_arg_t,StackAlloc && salloc,Fn && fn)233     fiber( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) :
234         fctx_{ detail::create_fiber1< detail::fiber_record< fiber, StackAlloc, Fn > >(
235                 std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) } {
236     }
237 
238     template< typename StackAlloc, typename Fn >
fiber(std::allocator_arg_t,preallocated palloc,StackAlloc && salloc,Fn && fn)239     fiber( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) :
240         fctx_{ detail::create_fiber2< detail::fiber_record< fiber, StackAlloc, Fn > >(
241                 palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) } {
242     }
243 
244 #if defined(BOOST_USE_SEGMENTED_STACKS)
245     template< typename Fn >
246     fiber( std::allocator_arg_t, segmented_stack, Fn &&);
247 
248     template< typename StackAlloc, typename Fn >
249     fiber( std::allocator_arg_t, preallocated, segmented_stack, Fn &&);
250 #endif
251 
~fiber()252     ~fiber() {
253         if ( BOOST_UNLIKELY( nullptr != fctx_) ) {
254             detail::ontop_fcontext(
255 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
256                     detail::exchange( fctx_, nullptr),
257 #else
258                     std::exchange( fctx_, nullptr),
259 #endif
260                    nullptr,
261                    detail::fiber_unwind);
262         }
263     }
264 
fiber(fiber && other)265     fiber( fiber && other) noexcept {
266         swap( other);
267     }
268 
operator =(fiber && other)269     fiber & operator=( fiber && other) noexcept {
270         if ( BOOST_LIKELY( this != & other) ) {
271             fiber tmp = std::move( other);
272             swap( tmp);
273         }
274         return * this;
275     }
276 
277     fiber( fiber const& other) noexcept = delete;
278     fiber & operator=( fiber const& other) noexcept = delete;
279 
resume()280     fiber resume() && {
281         BOOST_ASSERT( nullptr != fctx_);
282         return { detail::jump_fcontext(
283 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
284                     detail::exchange( fctx_, nullptr),
285 #else
286                     std::exchange( fctx_, nullptr),
287 #endif
288                     nullptr).fctx };
289     }
290 
291     template< typename Fn >
resume_with(Fn && fn)292     fiber resume_with( Fn && fn) && {
293         BOOST_ASSERT( nullptr != fctx_);
294         auto p = std::forward< Fn >( fn);
295         return { detail::ontop_fcontext(
296 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
297                     detail::exchange( fctx_, nullptr),
298 #else
299                     std::exchange( fctx_, nullptr),
300 #endif
301                     & p,
302                     detail::fiber_ontop< fiber, decltype(p) >).fctx };
303     }
304 
operator bool() const305     explicit operator bool() const noexcept {
306         return nullptr != fctx_;
307     }
308 
operator !() const309     bool operator!() const noexcept {
310         return nullptr == fctx_;
311     }
312 
operator <(fiber const & other) const313     bool operator<( fiber const& other) const noexcept {
314         return fctx_ < other.fctx_;
315     }
316 
317     #if !defined(BOOST_EMBTC)
318 
319     template< typename charT, class traitsT >
320     friend std::basic_ostream< charT, traitsT > &
operator <<(std::basic_ostream<charT,traitsT> & os,fiber const & other)321     operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
322         if ( nullptr != other.fctx_) {
323             return os << other.fctx_;
324         } else {
325             return os << "{not-a-context}";
326         }
327     }
328 
329     #else
330 
331     template< typename charT, class traitsT >
332     friend std::basic_ostream< charT, traitsT > &
333     operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other);
334 
335     #endif
336 
swap(fiber & other)337     void swap( fiber & other) noexcept {
338         std::swap( fctx_, other.fctx_);
339     }
340 };
341 
342 #if defined(BOOST_EMBTC)
343 
344     template< typename charT, class traitsT >
345     inline std::basic_ostream< charT, traitsT > &
operator <<(std::basic_ostream<charT,traitsT> & os,fiber const & other)346     operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
347         if ( nullptr != other.fctx_) {
348             return os << other.fctx_;
349         } else {
350             return os << "{not-a-context}";
351         }
352     }
353 
354 #endif
355 
356 inline
swap(fiber & l,fiber & r)357 void swap( fiber & l, fiber & r) noexcept {
358     l.swap( r);
359 }
360 
361 typedef fiber fiber_context;
362 
363 }}
364 
365 #if defined(BOOST_MSVC)
366 # pragma warning(pop)
367 #endif
368 
369 #ifdef BOOST_HAS_ABI_HEADERS
370 # include BOOST_ABI_SUFFIX
371 #endif
372 
373 #endif // BOOST_CONTEXT_FIBER_H
374