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_PULL_COROUTINE_OBJECT_H
8 #define BOOST_COROUTINES_DETAIL_PULL_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/pull_coroutine_impl.hpp>
21 #include <boost/coroutine/detail/trampoline_pull.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 pull_coroutine_context
41 coroutine_context caller;
42 coroutine_context callee;
44 template< typename Coro >
45 pull_coroutine_context( preallocated const& palloc, Coro *) :
47 callee( trampoline_pull< Coro >, palloc)
51 template< typename PushCoro, typename R, typename Fn, typename StackAllocator >
52 class pull_coroutine_object : private pull_coroutine_context,
53 public pull_coroutine_impl< R >
56 typedef pull_coroutine_context ctx_t;
57 typedef pull_coroutine_impl< R > base_t;
58 typedef pull_coroutine_object< PushCoro, R, Fn, StackAllocator > obj_t;
61 stack_context stack_ctx_;
62 StackAllocator stack_alloc_;
64 static void deallocate_( obj_t * obj)
66 stack_context stack_ctx( obj->stack_ctx_);
67 StackAllocator stack_alloc( obj->stack_alloc_);
70 stack_alloc.deallocate( stack_ctx);
74 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
75 pull_coroutine_object( Fn fn, attributes const& attrs,
76 preallocated const& palloc,
77 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
79 base_t( & this->caller,
81 stack_unwind == attrs.do_unwind),
83 stack_ctx_( palloc.sctx),
84 stack_alloc_( stack_alloc)
88 pull_coroutine_object( BOOST_RV_REF( Fn) fn, attributes const& attrs,
89 preallocated const& palloc,
90 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
92 base_t( & this->caller,
94 stack_unwind == attrs.do_unwind),
95 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
98 fn_( boost::forward< Fn >( fn) ),
100 stack_ctx_( palloc.sctx),
101 stack_alloc_( stack_alloc)
106 BOOST_ASSERT( ! base_t::unwind_requested() );
108 base_t::flags_ |= flag_started;
109 base_t::flags_ |= flag_running;
111 // create push_coroutine
112 typename PushCoro::synth_type b( & this->callee, & this->caller, false);
113 PushCoro push_coro( synthesized_t::syntesized, b);
116 catch ( forced_unwind const&)
119 { base_t::except_ = current_exception(); }
121 base_t::flags_ |= flag_complete;
122 base_t::flags_ &= ~flag_running;
123 typename base_t::param_type to;
127 BOOST_ASSERT_MSG( false, "pull_coroutine is complete");
131 { deallocate_( this); }
134 template< typename PushCoro, typename R, typename Fn, typename StackAllocator >
135 class pull_coroutine_object< PushCoro, R &, Fn, StackAllocator > : private pull_coroutine_context,
136 public pull_coroutine_impl< R & >
139 typedef pull_coroutine_context ctx_t;
140 typedef pull_coroutine_impl< R & > base_t;
141 typedef pull_coroutine_object< PushCoro, R &, Fn, StackAllocator > obj_t;
144 stack_context stack_ctx_;
145 StackAllocator stack_alloc_;
147 static void deallocate_( obj_t * obj)
149 stack_context stack_ctx( obj->stack_ctx_);
150 StackAllocator stack_alloc( obj->stack_alloc_);
153 stack_alloc.deallocate( stack_ctx);
157 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
158 pull_coroutine_object( Fn fn, attributes const& attrs,
159 preallocated const& palloc,
160 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
161 ctx_t( palloc, this),
162 base_t( & this->caller,
164 stack_unwind == attrs.do_unwind),
166 stack_ctx_( palloc.sctx),
167 stack_alloc_( stack_alloc)
171 pull_coroutine_object( BOOST_RV_REF( Fn) fn, attributes const& attrs,
172 preallocated const& palloc,
173 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
174 ctx_t( palloc, this),
175 base_t( & this->caller,
177 stack_unwind == attrs.do_unwind),
178 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
181 fn_( boost::forward< Fn >( fn) ),
183 stack_ctx_( palloc.sctx),
184 stack_alloc_( stack_alloc)
189 BOOST_ASSERT( ! base_t::unwind_requested() );
191 base_t::flags_ |= flag_started;
192 base_t::flags_ |= flag_running;
194 // create push_coroutine
195 typename PushCoro::synth_type b( & this->callee, & this->caller, false);
196 PushCoro push_coro( synthesized_t::syntesized, b);
199 catch ( forced_unwind const&)
202 { base_t::except_ = current_exception(); }
204 base_t::flags_ |= flag_complete;
205 base_t::flags_ &= ~flag_running;
206 typename base_t::param_type to;
210 BOOST_ASSERT_MSG( false, "pull_coroutine is complete");
214 { deallocate_( this); }
217 template< typename PushCoro, typename Fn, typename StackAllocator >
218 class pull_coroutine_object< PushCoro, void, Fn, StackAllocator > : private pull_coroutine_context,
219 public pull_coroutine_impl< void >
222 typedef pull_coroutine_context ctx_t;
223 typedef pull_coroutine_impl< void > base_t;
224 typedef pull_coroutine_object< PushCoro, void, Fn, StackAllocator > obj_t;
227 stack_context stack_ctx_;
228 StackAllocator stack_alloc_;
230 static void deallocate_( obj_t * obj)
232 stack_context stack_ctx( obj->stack_ctx_);
233 StackAllocator stack_alloc( obj->stack_alloc_);
236 stack_alloc.deallocate( stack_ctx);
240 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
241 pull_coroutine_object( Fn fn, attributes const& attrs,
242 preallocated const& palloc,
243 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
244 ctx_t( palloc, this),
245 base_t( & this->caller,
247 stack_unwind == attrs.do_unwind),
249 stack_ctx_( palloc.sctx),
250 stack_alloc_( stack_alloc)
254 pull_coroutine_object( BOOST_RV_REF( Fn) fn, attributes const& attrs,
255 preallocated const& palloc,
256 StackAllocator const& stack_alloc) BOOST_NOEXCEPT :
257 ctx_t( palloc, this),
258 base_t( & this->caller,
260 stack_unwind == attrs.do_unwind),
261 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
264 fn_( boost::forward< Fn >( fn) ),
266 stack_ctx_( palloc.sctx),
267 stack_alloc_( stack_alloc)
272 BOOST_ASSERT( ! base_t::unwind_requested() );
274 base_t::flags_ |= flag_started;
275 base_t::flags_ |= flag_running;
277 // create push_coroutine
278 typename PushCoro::synth_type b( & this->callee, & this->caller, false);
279 PushCoro push_coro( synthesized_t::syntesized, b);
282 catch ( forced_unwind const&)
285 { base_t::except_ = current_exception(); }
287 base_t::flags_ |= flag_complete;
288 base_t::flags_ &= ~flag_running;
289 typename base_t::param_type to;
293 BOOST_ASSERT_MSG( false, "pull_coroutine is complete");
297 { deallocate_( this); }
302 #if defined(BOOST_MSVC)
303 # pragma warning(pop)
306 #ifdef BOOST_HAS_ABI_HEADERS
307 # include BOOST_ABI_SUFFIX
310 #endif // BOOST_COROUTINES_DETAIL_PULL_COROUTINE_OBJECT_H