2 // Copyright Oliver Kowalke 2014.
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_EXECUTION_CONTEXT_H
8 #define BOOST_CONTEXT_EXECUTION_CONTEXT_H
10 #include <boost/context/detail/config.hpp>
23 #include <boost/assert.hpp>
24 #include <boost/config.hpp>
25 #include <boost/intrusive_ptr.hpp>
27 #if defined(BOOST_NO_CXX17_STD_APPLY)
28 #include <boost/context/detail/apply.hpp>
30 #include <boost/context/detail/disable_overload.hpp>
31 #include <boost/context/detail/fcontext.hpp>
32 #include <boost/context/fixedsize_stack.hpp>
33 #include <boost/context/flags.hpp>
34 #include <boost/context/preallocated.hpp>
35 #include <boost/context/segmented_stack.hpp>
36 #include <boost/context/stack_context.hpp>
38 #ifdef BOOST_HAS_ABI_HEADERS
39 # include BOOST_ABI_PREFIX
42 #if defined(BOOST_USE_SEGMENTED_STACKS)
44 void __splitstack_getcontext( void * [BOOST_CONTEXT_SEGMENTS]);
45 void __splitstack_setcontext( void * [BOOST_CONTEXT_SEGMENTS]);
53 template< typename Fn >
54 transfer_t context_ontop( transfer_t);
56 struct activation_record;
59 activation_record * from;
63 struct BOOST_CONTEXT_DECL activation_record {
64 typedef boost::intrusive_ptr< activation_record > ptr_t;
66 thread_local static ptr_t current_rec;
68 std::atomic< std::size_t > use_count{ 0 };
69 fcontext_t fctx{ nullptr };
71 bool main_ctx{ true };
73 // used for toplevel-context
74 // (e.g. main context, thread-entry context)
75 activation_record() = default;
77 activation_record( fcontext_t fctx_, stack_context sctx_) noexcept :
79 sctx( sctx_ ), // sctx{ sctx_ } - clang-3.6: no viable conversion from 'boost::context::stack_context' to 'std::size_t'
83 virtual ~activation_record() = default;
85 bool is_main_context() const noexcept {
89 void * resume( void * vp) {
90 // store current activation record in local variable
91 auto from = current_rec.get();
92 // store `this` in static, thread local pointer
93 // `this` will become the active (running) context
94 // returned by execution_context::current()
96 #if defined(BOOST_USE_SEGMENTED_STACKS)
97 // adjust segmented stack properties
98 __splitstack_getcontext( from->sctx.segments_ctx);
99 __splitstack_setcontext( sctx.segments_ctx);
101 data_t d = { from, vp };
102 // context switch from parent context to `this`-context
103 transfer_t t = jump_fcontext( fctx, & d);
104 data_t * dp = reinterpret_cast< data_t * >( t.data);
105 dp->from->fctx = t.fctx;
106 // parent context resumed
110 template< typename Fn >
111 void * resume_ontop( void * data, Fn && fn) {
112 // store current activation record in local variable
113 activation_record * from = current_rec.get();
114 // store `this` in static, thread local pointer
115 // `this` will become the active (running) context
116 // returned by execution_context::current()
118 #if defined(BOOST_USE_SEGMENTED_STACKS)
119 // adjust segmented stack properties
120 __splitstack_getcontext( from->sctx.segments_ctx);
121 __splitstack_setcontext( sctx.segments_ctx);
123 std::tuple< void *, Fn > p = std::forward_as_tuple( data, fn);
124 data_t d = { from, & p };
125 // context switch from parent context to `this`-context
126 // execute Fn( Tpl) on top of `this`
127 transfer_t t = ontop_fcontext( fctx, & d, context_ontop< Fn >);
128 data_t * dp = reinterpret_cast< data_t * >( t.data);
129 dp->from->fctx = t.fctx;
130 // parent context resumed
134 virtual void deallocate() noexcept {
137 friend void intrusive_ptr_add_ref( activation_record * ar) noexcept {
141 friend void intrusive_ptr_release( activation_record * ar) noexcept {
142 BOOST_ASSERT( nullptr != ar);
143 if ( 0 == --ar->use_count) {
149 struct BOOST_CONTEXT_DECL activation_record_initializer {
150 activation_record_initializer() noexcept;
151 ~activation_record_initializer();
154 template< typename Fn >
155 transfer_t context_ontop( transfer_t t) {
156 data_t * dp = reinterpret_cast< data_t * >( t.data);
157 dp->from->fctx = t.fctx;
158 auto tpl = reinterpret_cast< std::tuple< void *, Fn > * >( dp->data);
159 BOOST_ASSERT( nullptr != tpl);
160 auto data = std::get< 0 >( * tpl);
161 typename std::decay< Fn >::type fn = std::forward< Fn >( std::get< 1 >( * tpl) );
162 #if defined(BOOST_NO_CXX17_STD_APPLY)
163 dp->data = apply( fn, std::tie( data) );
165 dp->data = std::apply( fn, std::tie( data) );
167 return { t.fctx, dp };
170 template< typename StackAlloc, typename Fn, typename ... Args >
171 class capture_record : public activation_record {
173 typename std::decay< StackAlloc >::type salloc_;
174 typename std::decay< Fn >::type fn_;
175 std::tuple< typename std::decay< Args >::type ... > args_;
176 activation_record * caller_;
178 static void destroy( capture_record * p) noexcept {
179 typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
180 stack_context sctx = p->sctx;
181 // deallocate activation record
182 p->~capture_record();
183 // destroy stack with stack allocator
184 salloc.deallocate( sctx);
188 capture_record( stack_context sctx, StackAlloc && salloc,
190 activation_record * caller,
191 Fn && fn, Args && ... args) noexcept :
192 activation_record{ fctx, sctx },
193 salloc_{ std::forward< StackAlloc >( salloc) },
194 fn_( std::forward< Fn >( fn) ),
195 args_( std::forward< Args >( args) ... ),
199 void deallocate() noexcept override final {
204 auto data = caller_->resume( nullptr);
205 #if defined(BOOST_NO_CXX17_STD_APPLY)
206 apply( fn_, std::tuple_cat( args_, std::tie( data) ) );
208 std::apply( fn_, std::tuple_cat( args_, std::tie( data) ) );
210 BOOST_ASSERT_MSG( ! main_ctx, "main-context does not execute activation-record::run()");
216 class BOOST_CONTEXT_DECL execution_context {
218 // tampoline function
219 // entered if the execution context
220 // is resumed for the first time
221 template< typename AR >
222 static void entry_func( detail::transfer_t t) noexcept {
223 detail::data_t * dp = reinterpret_cast< detail::data_t * >( t.data);
224 AR * ar = static_cast< AR * >( dp->data);
225 BOOST_ASSERT( nullptr != ar);
226 dp->from->fctx = t.fctx;
227 // start execution of toplevel context-function
231 typedef boost::intrusive_ptr< detail::activation_record > ptr_t;
235 template< typename StackAlloc, typename Fn, typename ... Args >
236 static detail::activation_record * create_context( StackAlloc && salloc,
237 Fn && fn, Args && ... args) {
238 typedef detail::capture_record<
239 StackAlloc, Fn, Args ...
242 auto sctx = salloc.allocate();
243 // reserve space for control structure
244 #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
245 const std::size_t size = sctx.size - sizeof( capture_t);
246 void * sp = static_cast< char * >( sctx.sp) - sizeof( capture_t);
248 constexpr std::size_t func_alignment = 64; // alignof( capture_t);
249 constexpr std::size_t func_size = sizeof( capture_t);
250 // reserve space on stack
251 void * sp = static_cast< char * >( sctx.sp) - func_size - func_alignment;
253 std::size_t space = func_size + func_alignment;
254 sp = std::align( func_alignment, func_size, sp, space);
255 BOOST_ASSERT( nullptr != sp);
256 // calculate remaining size
257 const std::size_t size = sctx.size - ( static_cast< char * >( sctx.sp) - static_cast< char * >( sp) );
259 // create fast-context
260 const detail::fcontext_t fctx = detail::make_fcontext( sp, size, & execution_context::entry_func< capture_t >);
261 BOOST_ASSERT( nullptr != fctx);
262 // get current activation record
263 auto curr = execution_context::current().ptr_;
264 // placment new for control structure on fast-context stack
265 return ::new ( sp) capture_t{
266 sctx, std::forward< StackAlloc >( salloc), fctx, curr.get(), std::forward< Fn >( fn), std::forward< Args >( args) ... };
269 template< typename StackAlloc, typename Fn, typename ... Args >
270 static detail::activation_record * create_context( preallocated palloc, StackAlloc && salloc,
271 Fn && fn, Args && ... args) {
272 typedef detail::capture_record<
273 StackAlloc, Fn, Args ...
276 // reserve space for control structure
277 #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
278 const std::size_t size = palloc.size - sizeof( capture_t);
279 void * sp = static_cast< char * >( palloc.sp) - sizeof( capture_t);
281 constexpr std::size_t func_alignment = 64; // alignof( capture_t);
282 constexpr std::size_t func_size = sizeof( capture_t);
283 // reserve space on stack
284 void * sp = static_cast< char * >( palloc.sp) - func_size - func_alignment;
286 std::size_t space = func_size + func_alignment;
287 sp = std::align( func_alignment, func_size, sp, space);
288 BOOST_ASSERT( nullptr != sp);
289 // calculate remaining size
290 const std::size_t size = palloc.size - ( static_cast< char * >( palloc.sp) - static_cast< char * >( sp) );
292 // create fast-context
293 const detail::fcontext_t fctx = detail::make_fcontext( sp, size, & execution_context::entry_func< capture_t >);
294 BOOST_ASSERT( nullptr != fctx);
295 // get current activation record
296 auto curr = execution_context::current().ptr_;
297 // placment new for control structure on fast-context stack
298 return ::new ( sp) capture_t{
299 palloc.sctx, std::forward< StackAlloc >( salloc), fctx, curr.get(), std::forward< Fn >( fn), std::forward< Args >( args) ... };
302 execution_context() noexcept :
303 // default constructed with current activation_record
304 ptr_{ detail::activation_record::current_rec } {
308 static execution_context current() noexcept;
310 #if defined(BOOST_USE_SEGMENTED_STACKS)
311 template< typename Fn,
313 typename = detail::disable_overload< execution_context, Fn >
315 execution_context( Fn && fn, Args && ... args) :
316 // deferred execution of fn and its arguments
317 // arguments are stored in std::tuple<>
318 // non-type template parameter pack via std::index_sequence_for<>
319 // preserves the number of arguments
320 // used to extract the function arguments from std::tuple<>
321 ptr_{ create_context( segmented_stack(),
322 std::forward< Fn >( fn),
323 std::forward< Args >( args) ...) } {
324 ptr_->resume( ptr_.get() );
327 template< typename Fn,
330 execution_context( std::allocator_arg_t, segmented_stack salloc, Fn && fn, Args && ... args) :
331 // deferred execution of fn and its arguments
332 // arguments are stored in std::tuple<>
333 // non-type template parameter pack via std::index_sequence_for<>
334 // preserves the number of arguments
335 // used to extract the function arguments from std::tuple<>
336 ptr_{ create_context( std::forward< StackAlloc >( salloc),
337 std::forward< Fn >( fn),
338 std::forward< Args >( args) ...) } {
339 ptr_->resume( ptr_.get() );
342 template< typename Fn,
345 execution_context( std::allocator_arg_t, preallocated palloc, segmented_stack salloc, Fn && fn, Args && ... args) :
346 // deferred execution of fn and its arguments
347 // arguments are stored in std::tuple<>
348 // non-type template parameter pack via std::index_sequence_for<>
349 // preserves the number of arguments
350 // used to extract the function arguments from std::tuple<>
351 ptr_{ create_context( palloc, std::forward< StackAlloc >( salloc),
352 std::forward< Fn >( fn),
353 std::forward< Args >( args) ...) } {
354 ptr_->resume( ptr_.get() );
357 template< typename Fn,
359 typename = detail::disable_overload< execution_context, Fn >
361 execution_context( Fn && fn, Args && ... args) :
362 // deferred execution of fn and its arguments
363 // arguments are stored in std::tuple<>
364 // non-type template parameter pack via std::index_sequence_for<>
365 // preserves the number of arguments
366 // used to extract the function arguments from std::tuple<>
367 ptr_{ create_context( fixedsize_stack(),
368 std::forward< Fn >( fn),
369 std::forward< Args >( args) ...) } {
370 ptr_->resume( ptr_.get() );
373 template< typename StackAlloc,
377 execution_context( std::allocator_arg_t, StackAlloc && salloc, Fn && fn, Args && ... args) :
378 // deferred execution of fn and its arguments
379 // arguments are stored in std::tuple<>
380 // non-type template parameter pack via std::index_sequence_for<>
381 // preserves the number of arguments
382 // used to extract the function arguments from std::tuple<>
383 ptr_{ create_context( std::forward< StackAlloc >( salloc),
384 std::forward< Fn >( fn),
385 std::forward< Args >( args) ...) } {
386 ptr_->resume( ptr_.get() );
389 template< typename StackAlloc,
393 execution_context( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn, Args && ... args) :
394 // deferred execution of fn and its arguments
395 // arguments are stored in std::tuple<>
396 // non-type template parameter pack via std::index_sequence_for<>
397 // preserves the number of arguments
398 // used to extract the function arguments from std::tuple<>
399 ptr_{ create_context( palloc, std::forward< StackAlloc >( salloc),
400 std::forward< Fn >( fn),
401 std::forward< Args >( args) ...) } {
402 ptr_->resume( ptr_.get() );
406 execution_context( execution_context const& other) noexcept :
410 execution_context( execution_context && other) noexcept :
415 execution_context & operator=( execution_context const& other) noexcept {
416 // intrusive_ptr<> does not test for self-assignment
417 if ( this == & other) return * this;
422 execution_context & operator=( execution_context && other) noexcept {
423 if ( this == & other) return * this;
424 execution_context tmp{ std::move( other) };
429 void * operator()( void * vp = nullptr) {
430 return ptr_->resume( vp);
433 template< typename Fn >
434 void * operator()( exec_ontop_arg_t, Fn && fn, void * vp = nullptr) {
435 return ptr_->resume_ontop( vp,
436 std::forward< Fn >( fn) );
439 explicit operator bool() const noexcept {
440 return nullptr != ptr_.get();
443 bool operator!() const noexcept {
444 return nullptr == ptr_.get();
447 bool operator==( execution_context const& other) const noexcept {
448 return ptr_ == other.ptr_;
451 bool operator!=( execution_context const& other) const noexcept {
452 return ptr_ != other.ptr_;
455 bool operator<( execution_context const& other) const noexcept {
456 return ptr_ < other.ptr_;
459 bool operator>( execution_context const& other) const noexcept {
460 return other.ptr_ < ptr_;
463 bool operator<=( execution_context const& other) const noexcept {
464 return ! ( * this > other);
467 bool operator>=( execution_context const& other) const noexcept {
468 return ! ( * this < other);
471 template< typename charT, class traitsT >
472 friend std::basic_ostream< charT, traitsT > &
473 operator<<( std::basic_ostream< charT, traitsT > & os, execution_context const& other) {
474 if ( nullptr != other.ptr_) {
475 return os << other.ptr_;
477 return os << "{not-a-context}";
481 void swap( execution_context & other) noexcept {
482 ptr_.swap( other.ptr_);
487 void swap( execution_context & l, execution_context & r) noexcept {
493 #ifdef BOOST_HAS_ABI_HEADERS
494 # include BOOST_ABI_SUFFIX
497 #endif // BOOST_CONTEXT_EXECUTION_CONTEXT_H