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)
9 template< typename Ctx, typename Fn >
10 transfer_t context_ontop_void( transfer_t t) {
11 auto tpl = static_cast< std::tuple< Fn > * >( t.data);
12 BOOST_ASSERT( nullptr != tpl);
13 typename std::decay< Fn >::type fn = std::forward< Fn >( std::get< 0 >( * tpl) );
18 std::forward_as_tuple( std::move( ctx) ) );
19 return { exchange( ctx.fctx_, nullptr), nullptr };
22 template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
27 typename std::decay< Fn >::type fn_;
28 std::tuple< typename std::decay< Params >::type ... > params_;
30 static void destroy( record_void * p) noexcept {
31 StackAlloc salloc = p->salloc_;
32 stack_context sctx = p->sctx_;
35 // destroy stack with stack allocator
36 salloc.deallocate( sctx);
40 record_void( stack_context sctx, StackAlloc const& salloc,
41 Fn && fn, Params && ... params) noexcept :
44 fn_( std::forward< Fn >( fn) ),
45 params_( std::forward< Params >( params) ... ) {
48 record_void( record_void const&) = delete;
49 record_void & operator=( record_void const&) = delete;
51 void deallocate() noexcept {
55 transfer_t run( transfer_t t) {
57 // invoke context-function
62 std::forward_as_tuple( std::move( from) ) ) );
63 return { exchange( cc.fctx_, nullptr), nullptr };
67 template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
68 fcontext_t context_create_void( StackAlloc salloc, Fn && fn, Params && ... params) {
69 typedef record_void< Ctx, StackAlloc, Fn, Params ... > record_t;
71 auto sctx = salloc.allocate();
72 // reserve space for control structure
73 #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
74 const std::size_t size = sctx.size - sizeof( record_t);
75 void * sp = static_cast< char * >( sctx.sp) - sizeof( record_t);
77 constexpr std::size_t func_alignment = 64; // alignof( record_t);
78 constexpr std::size_t func_size = sizeof( record_t);
79 // reserve space on stack
80 void * sp = static_cast< char * >( sctx.sp) - func_size - func_alignment;
82 std::size_t space = func_size + func_alignment;
83 sp = std::align( func_alignment, func_size, sp, space);
84 BOOST_ASSERT( nullptr != sp);
85 // calculate remaining size
86 const std::size_t size = sctx.size - ( static_cast< char * >( sctx.sp) - static_cast< char * >( sp) );
88 // create fast-context
89 const fcontext_t fctx = make_fcontext( sp, size, & context_entry< record_t >);
90 BOOST_ASSERT( nullptr != fctx);
91 // placment new for control structure on context-stack
92 auto rec = ::new ( sp) record_t{
93 sctx, salloc, std::forward< Fn >( fn), std::forward< Params >( params) ... };
94 // transfer control structure to context-stack
95 return jump_fcontext( fctx, rec).fctx;
98 template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
99 fcontext_t context_create_void( preallocated palloc, StackAlloc salloc, Fn && fn, Params && ... params) {
100 typedef record_void< Ctx, StackAlloc, Fn, Params ... > record_t;
102 // reserve space for control structure
103 #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
104 const std::size_t size = palloc.size - sizeof( record_t);
105 void * sp = static_cast< char * >( palloc.sp) - sizeof( record_t);
107 constexpr std::size_t func_alignment = 64; // alignof( record_t);
108 constexpr std::size_t func_size = sizeof( record_t);
109 // reserve space on stack
110 void * sp = static_cast< char * >( palloc.sp) - func_size - func_alignment;
112 std::size_t space = func_size + func_alignment;
113 sp = std::align( func_alignment, func_size, sp, space);
114 BOOST_ASSERT( nullptr != sp);
115 // calculate remaining size
116 const std::size_t size = palloc.size - ( static_cast< char * >( palloc.sp) - static_cast< char * >( sp) );
118 // create fast-context
119 const fcontext_t fctx = make_fcontext( sp, size, & context_entry< record_t >);
120 BOOST_ASSERT( nullptr != fctx);
121 // placment new for control structure on context-stack
122 auto rec = ::new ( sp) record_t{
123 palloc.sctx, salloc, std::forward< Fn >( fn), std::forward< Params >( params) ... };
124 // transfer control structure to context-stack
125 return jump_fcontext( fctx, rec).fctx;
131 class execution_context< void > {
133 template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
134 friend class detail::record_void;
136 template< typename Ctx, typename Fn >
137 friend detail::transfer_t detail::context_ontop_void( detail::transfer_t);
139 detail::fcontext_t fctx_{ nullptr };
141 execution_context( detail::fcontext_t fctx) noexcept :
146 constexpr execution_context() noexcept = default;
148 #if defined(BOOST_USE_SEGMENTED_STACKS)
149 // segmented-stack requires to preserve the segments of the `current` context
150 // which is not possible (no global pointer to current context)
151 template< typename Fn, typename ... Params >
152 execution_context( std::allocator_arg_t, segmented_stack, Fn &&, Params && ...) = delete;
154 template< typename Fn, typename ... Params >
155 execution_context( std::allocator_arg_t, preallocated, segmented_stack, Fn &&, Params && ...) = delete;
157 template< typename Fn,
159 typename = detail::disable_overload< execution_context, Fn >
161 execution_context( Fn && fn, Params && ... params) :
162 // deferred execution of fn and its arguments
163 // arguments are stored in std::tuple<>
164 // non-type template parameter pack via std::index_sequence_for<>
165 // preserves the number of arguments
166 // used to extract the function arguments from std::tuple<>
167 fctx_( detail::context_create_void< execution_context >(
169 std::forward< Fn >( fn),
170 std::forward< Params >( params) ... ) ) {
173 template< typename StackAlloc,
177 execution_context( std::allocator_arg_t, StackAlloc salloc, Fn && fn, Params && ... params) :
178 // deferred execution of fn and its arguments
179 // arguments are stored in std::tuple<>
180 // non-type template parameter pack via std::index_sequence_for<>
181 // preserves the number of arguments
182 // used to extract the function arguments from std::tuple<>
183 fctx_( detail::context_create_void< execution_context >(
185 std::forward< Fn >( fn),
186 std::forward< Params >( params) ... ) ) {
189 template< typename StackAlloc,
193 execution_context( std::allocator_arg_t, preallocated palloc, StackAlloc salloc, Fn && fn, Params && ... params) :
194 // deferred execution of fn and its arguments
195 // arguments are stored in std::tuple<>
196 // non-type template parameter pack via std::index_sequence_for<>
197 // preserves the number of arguments
198 // used to extract the function arguments from std::tuple<>
199 fctx_( detail::context_create_void< execution_context >(
201 std::forward< Fn >( fn),
202 std::forward< Params >( params) ... ) ) {
206 ~execution_context() {
207 if ( nullptr != fctx_) {
208 detail::ontop_fcontext( detail::exchange( fctx_, nullptr), nullptr, detail::context_unwind);
212 execution_context( execution_context && other) noexcept :
213 fctx_( other.fctx_) {
214 other.fctx_ = nullptr;
217 execution_context & operator=( execution_context && other) noexcept {
218 if ( this != & other) {
219 execution_context tmp = std::move( other);
225 execution_context( execution_context const& other) noexcept = delete;
226 execution_context & operator=( execution_context const& other) noexcept = delete;
228 execution_context operator()() {
229 BOOST_ASSERT( nullptr != fctx_);
230 detail::transfer_t t = detail::jump_fcontext( detail::exchange( fctx_, nullptr), nullptr);
231 return execution_context( t.fctx);
234 template< typename Fn >
235 execution_context operator()( exec_ontop_arg_t, Fn && fn) {
236 BOOST_ASSERT( nullptr != fctx_);
237 std::tuple< Fn > p = std::forward_as_tuple( fn);
238 detail::transfer_t t = detail::ontop_fcontext(
239 detail::exchange( fctx_, nullptr),
241 detail::context_ontop_void< execution_context, Fn >);
242 return execution_context( t.fctx);
245 explicit operator bool() const noexcept {
246 return nullptr != fctx_;
249 bool operator!() const noexcept {
250 return nullptr == fctx_;
253 bool operator==( execution_context const& other) const noexcept {
254 return fctx_ == other.fctx_;
257 bool operator!=( execution_context const& other) const noexcept {
258 return fctx_ != other.fctx_;
261 bool operator<( execution_context const& other) const noexcept {
262 return fctx_ < other.fctx_;
265 bool operator>( execution_context const& other) const noexcept {
266 return other.fctx_ < fctx_;
269 bool operator<=( execution_context const& other) const noexcept {
270 return ! ( * this > other);
273 bool operator>=( execution_context const& other) const noexcept {
274 return ! ( * this < other);
277 template< typename charT, class traitsT >
278 friend std::basic_ostream< charT, traitsT > &
279 operator<<( std::basic_ostream< charT, traitsT > & os, execution_context const& other) {
280 if ( nullptr != other.fctx_) {
281 return os << other.fctx_;
283 return os << "{not-a-context}";
287 void swap( execution_context & other) noexcept {
288 std::swap( fctx_, other.fctx_);