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_CONTINUATION_H
8 #define BOOST_CONTEXT_CONTINUATION_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_CXX14_STD_EXCHANGE)
28 #include <boost/context/detail/exchange.hpp>
30 #if defined(BOOST_NO_CXX17_STD_INVOKE)
31 #include <boost/context/detail/invoke.hpp>
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>
43 #ifdef BOOST_HAS_ABI_HEADERS
44 # include BOOST_ABI_PREFIX
47 #if defined(BOOST_MSVC)
48 # pragma warning(push)
49 # pragma warning(disable: 4702)
57 transfer_t context_unwind( transfer_t t) {
58 throw forced_unwind( t.fctx);
59 return { nullptr, nullptr };
62 template< typename Rec >
63 transfer_t context_exit( transfer_t t) noexcept {
64 Rec * rec = static_cast< Rec * >( t.data);
65 // destroy context stack
67 return { nullptr, nullptr };
70 template< typename Rec >
71 void context_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);
77 // jump back to `create_context()`
78 t = jump_fcontext( t.fctx, nullptr);
80 t.fctx = rec->run( t.fctx);
81 } catch ( forced_unwind const& e) {
82 t = { e.fctx, nullptr };
84 BOOST_ASSERT( nullptr != t.fctx);
85 // destroy context-stack of `this`context on next context
86 ontop_fcontext( t.fctx, rec, context_exit< Rec >);
87 BOOST_ASSERT_MSG( false, "context already terminated");
90 template< typename Ctx, typename Fn >
91 transfer_t context_ontop( transfer_t t) {
92 auto p = static_cast< std::tuple< Fn > * >( t.data);
93 BOOST_ASSERT( nullptr != p);
94 typename std::decay< Fn >::type fn = std::get< 0 >( * p);
97 // execute function, pass continuation via reference
98 c = fn( std::move( c) );
99 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
100 return { exchange( c.fctx_, nullptr), nullptr };
102 return { std::exchange( c.fctx_, nullptr), nullptr };
106 template< typename Ctx, typename StackAlloc, typename Fn >
110 typename std::decay< StackAlloc >::type salloc_;
111 typename std::decay< Fn >::type fn_;
113 static void destroy( record * p) noexcept {
114 typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
115 stack_context sctx = p->sctx_;
118 // destroy stack with stack allocator
119 salloc.deallocate( sctx);
123 record( stack_context sctx, StackAlloc && salloc,
126 salloc_( std::forward< StackAlloc >( salloc)),
127 fn_( std::forward< Fn >( fn) ) {
130 record( record const&) = delete;
131 record & operator=( record const&) = delete;
133 void deallocate() noexcept {
137 fcontext_t run( fcontext_t fctx) {
139 // invoke context-function
140 #if defined(BOOST_NO_CXX17_STD_INVOKE)
141 c = invoke( fn_, std::move( c) );
143 c = std::invoke( fn_, std::move( c) );
145 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
146 return exchange( c.fctx_, nullptr);
148 return std::exchange( c.fctx_, nullptr);
153 template< typename Record, typename StackAlloc, typename Fn >
154 fcontext_t create_context1( 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, & context_entry< Record >);
172 BOOST_ASSERT( nullptr != fctx);
173 // transfer control structure to context-stack
174 return jump_fcontext( fctx, record).fctx;
177 template< typename Record, typename StackAlloc, typename Fn >
178 fcontext_t create_context2( 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, & context_entry< Record >);
194 BOOST_ASSERT( nullptr != fctx);
195 // transfer control structure to context-stack
196 return jump_fcontext( fctx, record).fctx;
203 template< typename Ctx, typename StackAlloc, typename Fn >
204 friend class detail::record;
206 template< typename Ctx, typename Fn >
207 friend detail::transfer_t
208 detail::context_ontop( detail::transfer_t);
210 template< typename StackAlloc, typename Fn >
212 callcc( std::allocator_arg_t, StackAlloc &&, Fn &&);
214 template< typename StackAlloc, typename Fn >
216 callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&);
218 detail::fcontext_t fctx_{ nullptr };
220 continuation( detail::fcontext_t fctx) noexcept :
225 continuation() noexcept = default;
228 if ( BOOST_UNLIKELY( nullptr != fctx_) ) {
229 detail::ontop_fcontext(
230 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
231 detail::exchange( fctx_, nullptr),
233 std::exchange( fctx_, nullptr),
236 detail::context_unwind);
240 continuation( continuation && other) noexcept {
241 std::swap( fctx_, other.fctx_);
244 continuation & operator=( continuation && other) noexcept {
245 if ( BOOST_LIKELY( this != & other) ) {
246 continuation tmp = std::move( other);
252 continuation( continuation const& other) noexcept = delete;
253 continuation & operator=( continuation const& other) noexcept = delete;
255 continuation resume() {
256 BOOST_ASSERT( nullptr != fctx_);
257 return detail::jump_fcontext(
258 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
259 detail::exchange( fctx_, nullptr),
261 std::exchange( fctx_, nullptr),
266 template< typename Fn >
267 continuation resume_with( Fn && fn) {
268 BOOST_ASSERT( nullptr != fctx_);
269 auto p = std::make_tuple( std::forward< Fn >( fn) );
270 return detail::ontop_fcontext(
271 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
272 detail::exchange( fctx_, nullptr),
274 std::exchange( fctx_, nullptr),
277 detail::context_ontop< continuation, Fn >).fctx;
280 explicit operator bool() const noexcept {
281 return nullptr != fctx_;
284 bool operator!() const noexcept {
285 return nullptr == fctx_;
288 bool operator==( continuation const& other) const noexcept {
289 return fctx_ == other.fctx_;
292 bool operator!=( continuation const& other) const noexcept {
293 return fctx_ != other.fctx_;
296 bool operator<( continuation const& other) const noexcept {
297 return fctx_ < other.fctx_;
300 bool operator>( continuation const& other) const noexcept {
301 return other.fctx_ < fctx_;
304 bool operator<=( continuation const& other) const noexcept {
305 return ! ( * this > other);
308 bool operator>=( continuation const& other) const noexcept {
309 return ! ( * this < other);
312 template< typename charT, class traitsT >
313 friend std::basic_ostream< charT, traitsT > &
314 operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other) {
315 if ( nullptr != other.fctx_) {
316 return os << other.fctx_;
318 return os << "{not-a-context}";
322 void swap( continuation & other) noexcept {
323 std::swap( fctx_, other.fctx_);
329 typename = detail::disable_overload< continuation, Fn >
334 std::allocator_arg, fixedsize_stack(),
335 std::forward< Fn >( fn) );
338 template< typename StackAlloc, typename Fn >
340 callcc( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) {
341 using Record = detail::record< continuation, StackAlloc, Fn >;
343 detail::create_context1< Record >(
344 std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume();
347 template< typename StackAlloc, typename Fn >
349 callcc( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) {
350 using Record = detail::record< continuation, StackAlloc, Fn >;
352 detail::create_context2< Record >(
353 palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume();
356 #if defined(BOOST_USE_SEGMENTED_STACKS)
357 template< typename Fn >
359 callcc( std::allocator_arg_t, segmented_stack, Fn &&);
361 template< typename StackAlloc, typename Fn >
363 callcc( std::allocator_arg_t, preallocated, segmented_stack, Fn &&);
367 void swap( continuation & l, continuation & r) noexcept {
373 #if defined(BOOST_MSVC)
374 # pragma warning(pop)
377 #ifdef BOOST_HAS_ABI_HEADERS
378 # include BOOST_ABI_SUFFIX
381 #endif // BOOST_CONTEXT_CONTINUATION_H