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_V1_H
8 #define BOOST_CONTEXT_EXECUTION_CONTEXT_V1_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/externc.hpp>
32 #include <boost/context/detail/fcontext.hpp>
33 #include <boost/context/fixedsize_stack.hpp>
34 #include <boost/context/flags.hpp>
35 #include <boost/context/preallocated.hpp>
36 #include <boost/context/segmented_stack.hpp>
37 #include <boost/context/stack_context.hpp>
39 #ifdef BOOST_HAS_ABI_HEADERS
40 # include BOOST_ABI_PREFIX
47 template< typename Fn >
48 transfer_t ecv1_context_ontop( transfer_t);
50 struct ecv1_activation_record;
53 ecv1_activation_record * from;
57 struct BOOST_CONTEXT_DECL ecv1_activation_record {
58 typedef boost::intrusive_ptr< ecv1_activation_record > ptr_t;
60 static ptr_t & current() noexcept;
62 std::atomic< std::size_t > use_count{ 0 };
63 fcontext_t fctx{ nullptr };
65 bool main_ctx{ true };
67 // used for toplevel-context
68 // (e.g. main context, thread-entry context)
69 ecv1_activation_record() = default;
71 ecv1_activation_record( fcontext_t fctx_, stack_context sctx_) noexcept :
73 sctx( sctx_ ), // sctx{ sctx_ } - clang-3.6: no viable conversion from 'boost::context::stack_context' to 'std::size_t'
77 virtual ~ecv1_activation_record() = default;
79 bool is_main_context() const noexcept {
83 void * resume( void * vp) {
84 // store current activation record in local variable
85 auto from = current().get();
86 // store `this` in static, thread local pointer
87 // `this` will become the active (running) context
88 // returned by execution_context::current()
90 #if defined(BOOST_USE_SEGMENTED_STACKS)
91 // adjust segmented stack properties
92 __splitstack_getcontext( from->sctx.segments_ctx);
93 __splitstack_setcontext( sctx.segments_ctx);
95 ecv1_data_t d = { from, vp };
96 // context switch from parent context to `this`-context
97 transfer_t t = jump_fcontext( fctx, & d);
98 ecv1_data_t * dp = reinterpret_cast< ecv1_data_t * >( t.data);
99 dp->from->fctx = t.fctx;
100 // parent context resumed
104 template< typename Fn >
105 void * resume_ontop( void * data, Fn && fn) {
106 // store current activation record in local variable
107 ecv1_activation_record * from = current().get();
108 // store `this` in static, thread local pointer
109 // `this` will become the active (running) context
110 // returned by execution_context::current()
112 #if defined(BOOST_USE_SEGMENTED_STACKS)
113 // adjust segmented stack properties
114 __splitstack_getcontext( from->sctx.segments_ctx);
115 __splitstack_setcontext( sctx.segments_ctx);
117 std::tuple< void *, Fn > p = std::forward_as_tuple( data, fn);
118 ecv1_data_t d = { from, & p };
119 // context switch from parent context to `this`-context
120 // execute Fn( Tpl) on top of `this`
121 transfer_t t = ontop_fcontext( fctx, & d, ecv1_context_ontop< Fn >);
122 ecv1_data_t * dp = reinterpret_cast< ecv1_data_t * >( t.data);
123 dp->from->fctx = t.fctx;
124 // parent context resumed
128 virtual void deallocate() noexcept {
131 friend void intrusive_ptr_add_ref( ecv1_activation_record * ar) noexcept {
135 friend void intrusive_ptr_release( ecv1_activation_record * ar) noexcept {
136 BOOST_ASSERT( nullptr != ar);
137 if ( 0 == --ar->use_count) {
143 struct BOOST_CONTEXT_DECL ecv1_activation_record_initializer {
144 ecv1_activation_record_initializer() noexcept;
145 ~ecv1_activation_record_initializer();
148 template< typename Fn >
149 transfer_t ecv1_context_ontop( transfer_t t) {
150 ecv1_data_t * dp = reinterpret_cast< ecv1_data_t * >( t.data);
151 dp->from->fctx = t.fctx;
152 auto tpl = reinterpret_cast< std::tuple< void *, Fn > * >( dp->data);
153 BOOST_ASSERT( nullptr != tpl);
154 auto data = std::get< 0 >( * tpl);
155 typename std::decay< Fn >::type fn = std::forward< Fn >( std::get< 1 >( * tpl) );
156 #if defined(BOOST_NO_CXX17_STD_APPLY)
157 dp->data = boost::context::detail::apply( fn, std::tie( data) );
159 dp->data = std::apply( fn, std::tie( data) );
161 return { t.fctx, dp };
164 template< typename StackAlloc, typename Fn, typename ... Args >
165 class ecv1_capture_record : public ecv1_activation_record {
167 typename std::decay< StackAlloc >::type salloc_;
168 typename std::decay< Fn >::type fn_;
169 std::tuple< typename std::decay< Args >::type ... > args_;
170 ecv1_activation_record * caller_;
172 static void destroy( ecv1_capture_record * p) noexcept {
173 typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
174 stack_context sctx = p->sctx;
175 // deallocate activation record
176 p->~ecv1_capture_record();
177 // destroy stack with stack allocator
178 salloc.deallocate( sctx);
182 ecv1_capture_record( stack_context sctx, StackAlloc && salloc,
184 ecv1_activation_record * caller,
185 Fn && fn, Args && ... args) noexcept :
186 ecv1_activation_record{ fctx, sctx },
187 salloc_{ std::forward< StackAlloc >( salloc) },
188 fn_( std::forward< Fn >( fn) ),
189 args_( std::forward< Args >( args) ... ),
193 void deallocate() noexcept override final {
198 auto data = caller_->resume( nullptr);
199 #if defined(BOOST_NO_CXX17_STD_APPLY)
200 boost::context::detail::apply( fn_, std::tuple_cat( args_, std::tie( data) ) );
202 std::apply( fn_, std::tuple_cat( args_, std::tie( data) ) );
204 BOOST_ASSERT_MSG( ! main_ctx, "main-context does not execute activation-record::run()");
212 class BOOST_CONTEXT_DECL execution_context {
214 // tampoline function
215 // entered if the execution context
216 // is resumed for the first time
217 template< typename AR >
218 static void entry_func( detail::transfer_t t) noexcept {
219 detail::ecv1_data_t * dp = reinterpret_cast< detail::ecv1_data_t * >( t.data);
220 AR * ar = static_cast< AR * >( dp->data);
221 BOOST_ASSERT( nullptr != ar);
222 dp->from->fctx = t.fctx;
223 // start execution of toplevel context-function
227 typedef boost::intrusive_ptr< detail::ecv1_activation_record > ptr_t;
231 template< typename StackAlloc, typename Fn, typename ... Args >
232 static detail::ecv1_activation_record * create_context( StackAlloc && salloc,
233 Fn && fn, Args && ... args) {
234 typedef detail::ecv1_capture_record<
235 StackAlloc, Fn, Args ...
238 auto sctx = salloc.allocate();
239 // reserve space for control structure
240 #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
241 const std::size_t size = sctx.size - sizeof( capture_t);
242 void * sp = static_cast< char * >( sctx.sp) - sizeof( capture_t);
244 constexpr std::size_t func_alignment = 64; // alignof( capture_t);
245 constexpr std::size_t func_size = sizeof( capture_t);
246 // reserve space on stack
247 void * sp = static_cast< char * >( sctx.sp) - func_size - func_alignment;
249 std::size_t space = func_size + func_alignment;
250 sp = std::align( func_alignment, func_size, sp, space);
251 BOOST_ASSERT( nullptr != sp);
252 // calculate remaining size
253 const std::size_t size = sctx.size - ( static_cast< char * >( sctx.sp) - static_cast< char * >( sp) );
255 // create fast-context
256 const detail::fcontext_t fctx = detail::make_fcontext( sp, size, & execution_context::entry_func< capture_t >);
257 BOOST_ASSERT( nullptr != fctx);
258 // get current activation record
259 auto curr = execution_context::current().ptr_;
260 // placment new for control structure on fast-context stack
261 return ::new ( sp) capture_t{
262 sctx, std::forward< StackAlloc >( salloc), fctx, curr.get(), std::forward< Fn >( fn), std::forward< Args >( args) ... };
265 template< typename StackAlloc, typename Fn, typename ... Args >
266 static detail::ecv1_activation_record * create_context( preallocated palloc, StackAlloc && salloc,
267 Fn && fn, Args && ... args) {
268 typedef detail::ecv1_capture_record<
269 StackAlloc, Fn, Args ...
272 // reserve space for control structure
273 #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
274 const std::size_t size = palloc.size - sizeof( capture_t);
275 void * sp = static_cast< char * >( palloc.sp) - sizeof( capture_t);
277 constexpr std::size_t func_alignment = 64; // alignof( capture_t);
278 constexpr std::size_t func_size = sizeof( capture_t);
279 // reserve space on stack
280 void * sp = static_cast< char * >( palloc.sp) - func_size - func_alignment;
282 std::size_t space = func_size + func_alignment;
283 sp = std::align( func_alignment, func_size, sp, space);
284 BOOST_ASSERT( nullptr != sp);
285 // calculate remaining size
286 const std::size_t size = palloc.size - ( static_cast< char * >( palloc.sp) - static_cast< char * >( sp) );
288 // create fast-context
289 const detail::fcontext_t fctx = detail::make_fcontext( sp, size, & execution_context::entry_func< capture_t >);
290 BOOST_ASSERT( nullptr != fctx);
291 // get current activation record
292 auto curr = execution_context::current().ptr_;
293 // placment new for control structure on fast-context stack
294 return ::new ( sp) capture_t{
295 palloc.sctx, std::forward< StackAlloc >( salloc), fctx, curr.get(), std::forward< Fn >( fn), std::forward< Args >( args) ... };
298 execution_context() noexcept :
299 // default constructed with current ecv1_activation_record
300 ptr_{ detail::ecv1_activation_record::current() } {
304 static execution_context current() noexcept;
306 #if defined(BOOST_USE_SEGMENTED_STACKS)
307 template< typename Fn,
309 typename = detail::disable_overload< execution_context, Fn >
311 execution_context( Fn && fn, Args && ... args) :
312 // deferred execution of fn and its arguments
313 // arguments are stored in std::tuple<>
314 // non-type template parameter pack via std::index_sequence_for<>
315 // preserves the number of arguments
316 // used to extract the function arguments from std::tuple<>
317 ptr_{ create_context( segmented_stack(),
318 std::forward< Fn >( fn),
319 std::forward< Args >( args) ...) } {
320 ptr_->resume( ptr_.get() );
323 template< typename Fn,
326 execution_context( std::allocator_arg_t, segmented_stack salloc, Fn && fn, Args && ... args) :
327 // deferred execution of fn and its arguments
328 // arguments are stored in std::tuple<>
329 // non-type template parameter pack via std::index_sequence_for<>
330 // preserves the number of arguments
331 // used to extract the function arguments from std::tuple<>
332 ptr_{ create_context( salloc,
333 std::forward< Fn >( fn),
334 std::forward< Args >( args) ...) } {
335 ptr_->resume( ptr_.get() );
338 template< typename Fn,
341 execution_context( std::allocator_arg_t, preallocated palloc, segmented_stack salloc, Fn && fn, Args && ... args) :
342 // deferred execution of fn and its arguments
343 // arguments are stored in std::tuple<>
344 // non-type template parameter pack via std::index_sequence_for<>
345 // preserves the number of arguments
346 // used to extract the function arguments from std::tuple<>
347 ptr_{ create_context( palloc, salloc,
348 std::forward< Fn >( fn),
349 std::forward< Args >( args) ...) } {
350 ptr_->resume( ptr_.get() );
353 template< typename Fn,
355 typename = detail::disable_overload< execution_context, Fn >
357 execution_context( Fn && fn, Args && ... args) :
358 // deferred execution of fn and its arguments
359 // arguments are stored in std::tuple<>
360 // non-type template parameter pack via std::index_sequence_for<>
361 // preserves the number of arguments
362 // used to extract the function arguments from std::tuple<>
363 ptr_{ create_context( fixedsize_stack(),
364 std::forward< Fn >( fn),
365 std::forward< Args >( args) ...) } {
366 ptr_->resume( ptr_.get() );
369 template< typename StackAlloc,
373 execution_context( std::allocator_arg_t, StackAlloc && salloc, Fn && fn, Args && ... args) :
374 // deferred execution of fn and its arguments
375 // arguments are stored in std::tuple<>
376 // non-type template parameter pack via std::index_sequence_for<>
377 // preserves the number of arguments
378 // used to extract the function arguments from std::tuple<>
379 ptr_{ create_context( std::forward< StackAlloc >( salloc),
380 std::forward< Fn >( fn),
381 std::forward< Args >( args) ...) } {
382 ptr_->resume( ptr_.get() );
385 template< typename StackAlloc,
389 execution_context( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn, Args && ... args) :
390 // deferred execution of fn and its arguments
391 // arguments are stored in std::tuple<>
392 // non-type template parameter pack via std::index_sequence_for<>
393 // preserves the number of arguments
394 // used to extract the function arguments from std::tuple<>
395 ptr_{ create_context( palloc, std::forward< StackAlloc >( salloc),
396 std::forward< Fn >( fn),
397 std::forward< Args >( args) ...) } {
398 ptr_->resume( ptr_.get() );
402 execution_context( execution_context const& other) noexcept :
406 execution_context( execution_context && other) noexcept :
411 execution_context & operator=( execution_context const& other) noexcept {
412 // intrusive_ptr<> does not test for self-assignment
413 if ( this == & other) return * this;
418 execution_context & operator=( execution_context && other) noexcept {
419 if ( this == & other) return * this;
420 execution_context tmp{ std::move( other) };
425 void * operator()( void * vp = nullptr) {
426 return ptr_->resume( vp);
429 template< typename Fn >
430 void * operator()( exec_ontop_arg_t, Fn && fn, void * vp = nullptr) {
431 return ptr_->resume_ontop( vp,
432 std::forward< Fn >( fn) );
435 explicit operator bool() const noexcept {
436 return nullptr != ptr_.get();
439 bool operator!() const noexcept {
440 return nullptr == ptr_.get();
443 bool operator==( execution_context const& other) const noexcept {
444 return ptr_ == other.ptr_;
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 other.ptr_ < ptr_;
459 bool operator<=( execution_context const& other) const noexcept {
460 return ! ( * this > other);
463 bool operator>=( execution_context const& other) const noexcept {
464 return ! ( * this < other);
467 template< typename charT, class traitsT >
468 friend std::basic_ostream< charT, traitsT > &
469 operator<<( std::basic_ostream< charT, traitsT > & os, execution_context const& other) {
470 if ( nullptr != other.ptr_) {
471 return os << other.ptr_;
473 return os << "{not-a-context}";
477 void swap( execution_context & other) noexcept {
478 ptr_.swap( other.ptr_);
483 void swap( execution_context & l, execution_context & r) noexcept {
489 #ifdef BOOST_HAS_ABI_HEADERS
490 # include BOOST_ABI_SUFFIX
493 #endif // BOOST_CONTEXT_EXECUTION_CONTEXT_V1_H