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_PUSH_COROUTINE_OBJECT_H
8 #define BOOST_COROUTINES_DETAIL_PUSH_COROUTINE_OBJECT_H
10 #include <boost/assert.hpp>
11 #include <boost/config.hpp>
12 #include <boost/cstdint.hpp>
13 #include <boost/exception_ptr.hpp>
14 #include <boost/move/move.hpp>
16 #include <boost/coroutine/detail/config.hpp>
17 #include <boost/coroutine/detail/coroutine_context.hpp>
18 #include <boost/coroutine/detail/flags.hpp>
19 #include <boost/coroutine/detail/preallocated.hpp>
20 #include <boost/coroutine/detail/push_coroutine_impl.hpp>
21 #include <boost/coroutine/detail/trampoline_push.hpp>
22 #include <boost/coroutine/exceptions.hpp>
23 #include <boost/coroutine/flags.hpp>
24 #include <boost/coroutine/stack_context.hpp>
26 #ifdef BOOST_HAS_ABI_HEADERS
27 # include BOOST_ABI_PREFIX
30 #if defined(BOOST_MSVC)
31 # pragma warning(push)
32 # pragma warning(disable:4355)
36 namespace coroutines {
39 struct push_coroutine_context
41 coroutine_context caller;
42 coroutine_context callee;
44 template< typename Coro >
45 push_coroutine_context( preallocated const& palloc, Coro *) :
47 callee( trampoline_push< Coro >, palloc)
51 struct push_coroutine_context_void
53 coroutine_context caller;
54 coroutine_context callee;
56 template< typename Coro >
57 push_coroutine_context_void( preallocated const& palloc, Coro *) :
59 callee( trampoline_push_void< Coro >, palloc)
63 template< typename PullCoro, typename R, typename Fn, typename StackAllocator >
64 class push_coroutine_object : private push_coroutine_context,
65 public push_coroutine_impl< R >
68 typedef push_coroutine_context ctx_t;
69 typedef push_coroutine_impl< R > base_t;
70 typedef push_coroutine_object< PullCoro, R, Fn, StackAllocator > obj_t;
73 stack_context stack_ctx_;
74 StackAllocator stack_alloc_;
76 static void deallocate_( obj_t * obj)
78 stack_context stack_ctx( obj->stack_ctx_);
79 StackAllocator stack_alloc( obj->stack_alloc_);
82 stack_alloc.deallocate( stack_ctx);
86 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
87 push_coroutine_object( Fn fn, attributes const& attrs,
88 preallocated const& palloc,
89 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
91 base_t( & this->caller,
93 stack_unwind == attrs.do_unwind),
95 stack_ctx_( palloc.sctx),
96 stack_alloc_( stack_alloc)
100 push_coroutine_object( BOOST_RV_REF( Fn) fn, attributes const& attrs,
101 preallocated const& palloc,
102 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
103 ctx_t( palloc, this),
104 base_t( & this->caller,
106 stack_unwind == attrs.do_unwind),
107 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
110 fn_( boost::forward< Fn >( fn) ),
112 stack_ctx_( palloc.sctx),
113 stack_alloc_( stack_alloc)
116 void run( R * result)
118 BOOST_ASSERT( ! base_t::unwind_requested() );
120 base_t::flags_ |= flag_started;
121 base_t::flags_ |= flag_running;
123 // create push_coroutine
124 typename PullCoro::synth_type b( & this->callee, & this->caller, false, result);
125 PullCoro pull_coro( synthesized_t::syntesized, b);
128 catch ( forced_unwind const&)
131 { base_t::except_ = current_exception(); }
133 base_t::flags_ |= flag_complete;
134 base_t::flags_ &= ~flag_running;
135 typename base_t::param_type to;
139 BOOST_ASSERT_MSG( false, "pull_coroutine is complete");
143 { deallocate_( this); }
146 template< typename PullCoro, typename R, typename Fn, typename StackAllocator >
147 class push_coroutine_object< PullCoro, R &, Fn, StackAllocator > : private push_coroutine_context,
148 public push_coroutine_impl< R & >
151 typedef push_coroutine_context ctx_t;
152 typedef push_coroutine_impl< R & > base_t;
153 typedef push_coroutine_object< PullCoro, R &, Fn, StackAllocator > obj_t;
156 stack_context stack_ctx_;
157 StackAllocator stack_alloc_;
159 static void deallocate_( obj_t * obj)
161 stack_context stack_ctx( obj->stack_ctx_);
162 StackAllocator stack_alloc( obj->stack_alloc_);
165 stack_alloc.deallocate( stack_ctx);
169 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
170 push_coroutine_object( Fn fn, attributes const& attrs,
171 preallocated const& palloc,
172 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
173 ctx_t( palloc, this),
174 base_t( & this->caller,
176 stack_unwind == attrs.do_unwind),
178 stack_ctx_( palloc.sctx),
179 stack_alloc_( stack_alloc)
183 push_coroutine_object( BOOST_RV_REF( Fn) fn, attributes const& attrs,
184 preallocated const& palloc,
185 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
186 ctx_t( palloc, this),
187 base_t( & this->caller,
189 stack_unwind == attrs.do_unwind),
190 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
193 fn_( boost::forward< Fn >( fn) ),
195 stack_ctx_( palloc.sctx),
196 stack_alloc_( stack_alloc)
199 void run( R * result)
201 BOOST_ASSERT( ! base_t::unwind_requested() );
203 base_t::flags_ |= flag_started;
204 base_t::flags_ |= flag_running;
206 // create push_coroutine
207 typename PullCoro::synth_type b( & this->callee, & this->caller, false, result);
208 PullCoro push_coro( synthesized_t::syntesized, b);
211 catch ( forced_unwind const&)
214 { base_t::except_ = current_exception(); }
216 base_t::flags_ |= flag_complete;
217 base_t::flags_ &= ~flag_running;
218 typename base_t::param_type to;
222 BOOST_ASSERT_MSG( false, "pull_coroutine is complete");
226 { deallocate_( this); }
229 template< typename PullCoro, typename Fn, typename StackAllocator >
230 class push_coroutine_object< PullCoro, void, Fn, StackAllocator > : private push_coroutine_context_void,
231 public push_coroutine_impl< void >
234 typedef push_coroutine_context_void ctx_t;
235 typedef push_coroutine_impl< void > base_t;
236 typedef push_coroutine_object< PullCoro, void, Fn, StackAllocator > obj_t;
239 stack_context stack_ctx_;
240 StackAllocator stack_alloc_;
242 static void deallocate_( obj_t * obj)
244 stack_context stack_ctx( obj->stack_ctx_);
245 StackAllocator stack_alloc( obj->stack_alloc_);
248 stack_alloc.deallocate( stack_ctx);
252 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
253 push_coroutine_object( Fn fn, attributes const& attrs,
254 preallocated const& palloc,
255 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
256 ctx_t( palloc, this),
257 base_t( & this->caller,
259 stack_unwind == attrs.do_unwind),
261 stack_ctx_( palloc.sctx),
262 stack_alloc_( stack_alloc)
266 push_coroutine_object( BOOST_RV_REF( Fn) fn, attributes const& attrs,
267 preallocated const& palloc,
268 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
269 ctx_t( palloc, this),
270 base_t( & this->caller,
272 stack_unwind == attrs.do_unwind),
273 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
276 fn_( boost::forward< Fn >( fn) ),
278 stack_ctx_( palloc.sctx),
279 stack_alloc_( stack_alloc)
284 BOOST_ASSERT( ! base_t::unwind_requested() );
286 base_t::flags_ |= flag_started;
287 base_t::flags_ |= flag_running;
289 // create push_coroutine
290 typename PullCoro::synth_type b( & this->callee, & this->caller, false);
291 PullCoro push_coro( synthesized_t::syntesized, b);
294 catch ( forced_unwind const&)
297 { base_t::except_ = current_exception(); }
299 base_t::flags_ |= flag_complete;
300 base_t::flags_ &= ~flag_running;
301 typename base_t::param_type to;
305 BOOST_ASSERT_MSG( false, "pull_coroutine is complete");
309 { deallocate_( this); }
314 #if defined(BOOST_MSVC)
315 # pragma warning(pop)
318 #ifdef BOOST_HAS_ABI_HEADERS
319 # include BOOST_ABI_SUFFIX
322 #endif // BOOST_COROUTINES_DETAIL_PUSH_COROUTINE_OBJECT_H