2 // Copyright Oliver Kowalke 2009.
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_COROUTINES_DETAIL_SYMMETRIC_COROUTINE_OBJECT_H
8 #define BOOST_COROUTINES_DETAIL_SYMMETRIC_COROUTINE_OBJECT_H
10 #include <boost/assert.hpp>
11 #include <boost/config.hpp>
12 #include <boost/move/move.hpp>
14 #include <boost/coroutine/detail/config.hpp>
15 #include <boost/coroutine/detail/flags.hpp>
16 #include <boost/coroutine/detail/preallocated.hpp>
17 #include <boost/coroutine/detail/symmetric_coroutine_impl.hpp>
18 #include <boost/coroutine/detail/symmetric_coroutine_yield.hpp>
19 #include <boost/coroutine/exceptions.hpp>
20 #include <boost/coroutine/stack_context.hpp>
22 #ifdef BOOST_HAS_ABI_HEADERS
23 # include BOOST_ABI_PREFIX
27 namespace coroutines {
33 template< typename R, typename Fn, typename StackAllocator >
34 class symmetric_coroutine_object : public symmetric_coroutine_impl< R >
37 typedef symmetric_coroutine_impl< R > impl_t;
38 typedef symmetric_coroutine_object< R, Fn, StackAllocator > obj_t;
41 stack_context stack_ctx_;
42 StackAllocator stack_alloc_;
44 static void deallocate_( obj_t * obj)
46 stack_context stack_ctx( obj->stack_ctx_);
47 StackAllocator stack_alloc( obj->stack_alloc_);
50 stack_alloc.deallocate( stack_ctx);
54 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
55 symmetric_coroutine_object( Fn fn, attributes const& attrs,
56 preallocated const& palloc,
57 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
59 stack_unwind == attrs.do_unwind),
61 stack_ctx_( palloc.sctx),
62 stack_alloc_( stack_alloc)
66 symmetric_coroutine_object( BOOST_RV_REF( Fn) fn, attributes const& attrs,
67 preallocated const& palloc,
68 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
70 stack_unwind == attrs.do_unwind),
71 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
74 fn_( boost::forward< Fn >( fn) ),
76 stack_ctx_( palloc.sctx),
77 stack_alloc_( stack_alloc)
80 void run( R * r) BOOST_NOEXCEPT
82 BOOST_ASSERT( ! impl_t::unwind_requested() );
84 impl_t::flags_ |= flag_started;
85 impl_t::flags_ |= flag_running;
88 symmetric_coroutine_yield< R > yc( this, r);
91 catch ( forced_unwind const&)
96 impl_t::flags_ |= flag_complete;
97 impl_t::flags_ &= ~flag_running;
98 typename impl_t::param_type to;
102 BOOST_ASSERT_MSG( false, "coroutine is complete");
106 { deallocate_( this); }
109 template< typename R, typename Fn, typename StackAllocator >
110 class symmetric_coroutine_object< R &, Fn, StackAllocator > : public symmetric_coroutine_impl< R & >
113 typedef symmetric_coroutine_impl< R & > impl_t;
114 typedef symmetric_coroutine_object< R &, Fn, StackAllocator > obj_t;
117 stack_context stack_ctx_;
118 StackAllocator stack_alloc_;
120 static void deallocate_( obj_t * obj)
122 stack_context stack_ctx( obj->stack_ctx_);
123 StackAllocator stack_alloc( obj->stack_alloc_);
126 stack_alloc.deallocate( stack_ctx);
130 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
131 symmetric_coroutine_object( Fn fn, attributes const& attrs,
132 preallocated const& palloc,
133 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
135 stack_unwind == attrs.do_unwind),
137 stack_ctx_( palloc.sctx),
138 stack_alloc_( stack_alloc)
142 symmetric_coroutine_object( BOOST_RV_REF( Fn) fn, attributes const& attrs,
143 preallocated const& palloc,
144 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
146 stack_unwind == attrs.do_unwind),
147 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
150 fn_( boost::forward< Fn >( fn) ),
152 stack_ctx_( palloc.sctx),
153 stack_alloc_( stack_alloc)
156 void run( R * r) BOOST_NOEXCEPT
158 BOOST_ASSERT( ! impl_t::unwind_requested() );
160 impl_t::flags_ |= flag_started;
161 impl_t::flags_ |= flag_running;
164 symmetric_coroutine_yield< R & > yc( this, r);
167 catch ( forced_unwind const&)
170 { std::terminate(); }
172 impl_t::flags_ |= flag_complete;
173 impl_t::flags_ &= ~flag_running;
174 typename impl_t::param_type to;
175 impl_t::callee_.jump(
178 BOOST_ASSERT_MSG( false, "coroutine is complete");
182 { deallocate_( this); }
185 template< typename Fn, typename StackAllocator >
186 class symmetric_coroutine_object< void, Fn, StackAllocator > : public symmetric_coroutine_impl< void >
189 typedef symmetric_coroutine_impl< void > impl_t;
190 typedef symmetric_coroutine_object< void, Fn, StackAllocator > obj_t;
193 stack_context stack_ctx_;
194 StackAllocator stack_alloc_;
196 static void deallocate_( obj_t * obj)
198 stack_context stack_ctx( obj->stack_ctx_);
199 StackAllocator stack_alloc( obj->stack_alloc_);
202 stack_alloc.deallocate( stack_ctx);
206 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
207 symmetric_coroutine_object( Fn fn, attributes const& attrs,
208 preallocated const& palloc,
209 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
211 stack_unwind == attrs.do_unwind),
213 stack_ctx_( palloc.sctx),
214 stack_alloc_( stack_alloc)
218 symmetric_coroutine_object( BOOST_RV_REF( Fn) fn, attributes const& attrs,
219 preallocated const& palloc,
220 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
222 stack_unwind == attrs.do_unwind),
223 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
226 fn_( boost::forward< Fn >( fn) ),
228 stack_ctx_( palloc.sctx),
229 stack_alloc_( stack_alloc)
232 void run() BOOST_NOEXCEPT
234 BOOST_ASSERT( ! impl_t::unwind_requested() );
236 impl_t::flags_ |= flag_started;
237 impl_t::flags_ |= flag_running;
240 symmetric_coroutine_yield< void > yc( this);
243 catch ( forced_unwind const&)
246 { std::terminate(); }
248 impl_t::flags_ |= flag_complete;
249 impl_t::flags_ &= ~flag_running;
250 typename impl_t::param_type to;
251 impl_t::callee_.jump(
254 BOOST_ASSERT_MSG( false, "coroutine is complete");
258 { deallocate_( this); }
263 #ifdef BOOST_HAS_ABI_HEADERS
264 # include BOOST_ABI_SUFFIX
267 #endif // BOOST_COROUTINES_DETAIL_SYMMETRIC_COROUTINE_OBJECT_H