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_IMPL_H
8 #define BOOST_COROUTINES_DETAIL_PUSH_COROUTINE_IMPL_H
10 #include <boost/assert.hpp>
11 #include <boost/config.hpp>
12 #include <boost/exception_ptr.hpp>
13 #include <boost/throw_exception.hpp>
14 #include <boost/utility.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/parameters.hpp>
20 #include <boost/coroutine/detail/trampoline_push.hpp>
21 #include <boost/coroutine/exceptions.hpp>
23 #ifdef BOOST_HAS_ABI_HEADERS
24 # include BOOST_ABI_PREFIX
28 namespace coroutines {
34 template< typename Arg >
35 class push_coroutine_impl : private noncopyable
39 exception_ptr except_;
40 coroutine_context * caller_;
41 coroutine_context * callee_;
44 typedef parameters< Arg > param_type;
46 push_coroutine_impl( coroutine_context * caller,
47 coroutine_context * callee,
54 if ( unwind) flags_ |= flag_force_unwind;
57 bool force_unwind() const BOOST_NOEXCEPT
58 { return 0 != ( flags_ & flag_force_unwind); }
60 bool unwind_requested() const BOOST_NOEXCEPT
61 { return 0 != ( flags_ & flag_unwind_stack); }
63 bool is_started() const BOOST_NOEXCEPT
64 { return 0 != ( flags_ & flag_started); }
66 bool is_running() const BOOST_NOEXCEPT
67 { return 0 != ( flags_ & flag_running); }
69 bool is_complete() const BOOST_NOEXCEPT
70 { return 0 != ( flags_ & flag_complete); }
72 void unwind_stack() BOOST_NOEXCEPT
74 if ( is_started() && ! is_complete() && force_unwind() )
76 flags_ |= flag_unwind_stack;
77 param_type to( unwind_t::force_unwind);
81 flags_ &= ~flag_unwind_stack;
83 BOOST_ASSERT( is_complete() );
87 void push( Arg const& arg)
89 BOOST_ASSERT( ! is_running() );
90 BOOST_ASSERT( ! is_complete() );
92 flags_ |= flag_running;
93 param_type to( const_cast< Arg * >( & arg), this);
95 static_cast< param_type * >(
99 flags_ &= ~flag_running;
100 if ( from->do_unwind) throw forced_unwind();
101 if ( except_) rethrow_exception( except_);
104 void push( BOOST_RV_REF( Arg) arg)
106 BOOST_ASSERT( ! is_running() );
107 BOOST_ASSERT( ! is_complete() );
109 flags_ |= flag_running;
110 param_type to( const_cast< Arg * >( & arg), this);
112 static_cast< param_type * >(
116 flags_ &= ~flag_running;
117 if ( from->do_unwind) throw forced_unwind();
118 if ( except_) rethrow_exception( except_);
121 virtual void destroy() = 0;
124 template< typename Arg >
125 class push_coroutine_impl< Arg & > : private noncopyable
129 exception_ptr except_;
130 coroutine_context * caller_;
131 coroutine_context * callee_;
134 typedef parameters< Arg & > param_type;
136 push_coroutine_impl( coroutine_context * caller,
137 coroutine_context * callee,
144 if ( unwind) flags_ |= flag_force_unwind;
147 bool force_unwind() const BOOST_NOEXCEPT
148 { return 0 != ( flags_ & flag_force_unwind); }
150 bool unwind_requested() const BOOST_NOEXCEPT
151 { return 0 != ( flags_ & flag_unwind_stack); }
153 bool is_started() const BOOST_NOEXCEPT
154 { return 0 != ( flags_ & flag_started); }
156 bool is_running() const BOOST_NOEXCEPT
157 { return 0 != ( flags_ & flag_running); }
159 bool is_complete() const BOOST_NOEXCEPT
160 { return 0 != ( flags_ & flag_complete); }
162 void unwind_stack() BOOST_NOEXCEPT
164 if ( is_started() && ! is_complete() && force_unwind() )
166 flags_ |= flag_unwind_stack;
167 param_type to( unwind_t::force_unwind);
171 flags_ &= ~flag_unwind_stack;
173 BOOST_ASSERT( is_complete() );
177 void push( Arg & arg)
179 BOOST_ASSERT( ! is_running() );
180 BOOST_ASSERT( ! is_complete() );
182 flags_ |= flag_running;
183 param_type to( & arg, this);
185 static_cast< param_type * >(
189 flags_ &= ~flag_running;
190 if ( from->do_unwind) throw forced_unwind();
191 if ( except_) rethrow_exception( except_);
194 virtual void destroy() = 0;
198 class push_coroutine_impl< void > : private noncopyable
202 exception_ptr except_;
203 coroutine_context * caller_;
204 coroutine_context * callee_;
207 typedef parameters< void > param_type;
209 push_coroutine_impl( coroutine_context * caller,
210 coroutine_context * callee,
217 if ( unwind) flags_ |= flag_force_unwind;
220 inline bool force_unwind() const BOOST_NOEXCEPT
221 { return 0 != ( flags_ & flag_force_unwind); }
223 inline bool unwind_requested() const BOOST_NOEXCEPT
224 { return 0 != ( flags_ & flag_unwind_stack); }
226 inline bool is_started() const BOOST_NOEXCEPT
227 { return 0 != ( flags_ & flag_started); }
229 inline bool is_running() const BOOST_NOEXCEPT
230 { return 0 != ( flags_ & flag_running); }
232 inline bool is_complete() const BOOST_NOEXCEPT
233 { return 0 != ( flags_ & flag_complete); }
235 inline void unwind_stack() BOOST_NOEXCEPT
237 if ( is_started() && ! is_complete() && force_unwind() )
239 flags_ |= flag_unwind_stack;
240 param_type to( unwind_t::force_unwind);
244 flags_ &= ~flag_unwind_stack;
246 BOOST_ASSERT( is_complete() );
252 BOOST_ASSERT( ! is_running() );
253 BOOST_ASSERT( ! is_complete() );
255 flags_ |= flag_running;
256 param_type to( this);
258 static_cast< param_type * >(
262 flags_ &= ~flag_running;
263 if ( from->do_unwind) throw forced_unwind();
264 if ( except_) rethrow_exception( except_);
267 virtual void destroy() = 0;
272 #ifdef BOOST_HAS_ABI_HEADERS
273 # include BOOST_ABI_SUFFIX
276 #endif // BOOST_COROUTINES_DETAIL_PUSH_COROUTINE_IMPL_H