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 virtual ~push_coroutine_impl() {}
59 bool force_unwind() const BOOST_NOEXCEPT
60 { return 0 != ( flags_ & flag_force_unwind); }
62 bool unwind_requested() const BOOST_NOEXCEPT
63 { return 0 != ( flags_ & flag_unwind_stack); }
65 bool is_started() const BOOST_NOEXCEPT
66 { return 0 != ( flags_ & flag_started); }
68 bool is_running() const BOOST_NOEXCEPT
69 { return 0 != ( flags_ & flag_running); }
71 bool is_complete() const BOOST_NOEXCEPT
72 { return 0 != ( flags_ & flag_complete); }
74 void unwind_stack() BOOST_NOEXCEPT
76 if ( is_started() && ! is_complete() && force_unwind() )
78 flags_ |= flag_unwind_stack;
79 param_type to( unwind_t::force_unwind);
83 flags_ &= ~flag_unwind_stack;
85 BOOST_ASSERT( is_complete() );
89 void push( Arg const& arg)
91 BOOST_ASSERT( ! is_running() );
92 BOOST_ASSERT( ! is_complete() );
94 flags_ |= flag_running;
95 param_type to( const_cast< Arg * >( & arg), this);
97 static_cast< param_type * >(
101 flags_ &= ~flag_running;
102 if ( from->do_unwind) throw forced_unwind();
103 if ( except_) rethrow_exception( except_);
106 void push( BOOST_RV_REF( Arg) arg)
108 BOOST_ASSERT( ! is_running() );
109 BOOST_ASSERT( ! is_complete() );
111 flags_ |= flag_running;
112 param_type to( const_cast< Arg * >( & arg), this);
114 static_cast< param_type * >(
118 flags_ &= ~flag_running;
119 if ( from->do_unwind) throw forced_unwind();
120 if ( except_) rethrow_exception( except_);
123 virtual void destroy() = 0;
126 template< typename Arg >
127 class push_coroutine_impl< Arg & > : private noncopyable
131 exception_ptr except_;
132 coroutine_context * caller_;
133 coroutine_context * callee_;
136 typedef parameters< Arg & > param_type;
138 push_coroutine_impl( coroutine_context * caller,
139 coroutine_context * callee,
146 if ( unwind) flags_ |= flag_force_unwind;
149 virtual ~push_coroutine_impl() {}
151 bool force_unwind() const BOOST_NOEXCEPT
152 { return 0 != ( flags_ & flag_force_unwind); }
154 bool unwind_requested() const BOOST_NOEXCEPT
155 { return 0 != ( flags_ & flag_unwind_stack); }
157 bool is_started() const BOOST_NOEXCEPT
158 { return 0 != ( flags_ & flag_started); }
160 bool is_running() const BOOST_NOEXCEPT
161 { return 0 != ( flags_ & flag_running); }
163 bool is_complete() const BOOST_NOEXCEPT
164 { return 0 != ( flags_ & flag_complete); }
166 void unwind_stack() BOOST_NOEXCEPT
168 if ( is_started() && ! is_complete() && force_unwind() )
170 flags_ |= flag_unwind_stack;
171 param_type to( unwind_t::force_unwind);
175 flags_ &= ~flag_unwind_stack;
177 BOOST_ASSERT( is_complete() );
181 void push( Arg & arg)
183 BOOST_ASSERT( ! is_running() );
184 BOOST_ASSERT( ! is_complete() );
186 flags_ |= flag_running;
187 param_type to( & arg, this);
189 static_cast< param_type * >(
193 flags_ &= ~flag_running;
194 if ( from->do_unwind) throw forced_unwind();
195 if ( except_) rethrow_exception( except_);
198 virtual void destroy() = 0;
202 class push_coroutine_impl< void > : private noncopyable
206 exception_ptr except_;
207 coroutine_context * caller_;
208 coroutine_context * callee_;
211 typedef parameters< void > param_type;
213 push_coroutine_impl( coroutine_context * caller,
214 coroutine_context * callee,
221 if ( unwind) flags_ |= flag_force_unwind;
224 virtual ~push_coroutine_impl() {}
226 inline bool force_unwind() const BOOST_NOEXCEPT
227 { return 0 != ( flags_ & flag_force_unwind); }
229 inline bool unwind_requested() const BOOST_NOEXCEPT
230 { return 0 != ( flags_ & flag_unwind_stack); }
232 inline bool is_started() const BOOST_NOEXCEPT
233 { return 0 != ( flags_ & flag_started); }
235 inline bool is_running() const BOOST_NOEXCEPT
236 { return 0 != ( flags_ & flag_running); }
238 inline bool is_complete() const BOOST_NOEXCEPT
239 { return 0 != ( flags_ & flag_complete); }
241 inline void unwind_stack() BOOST_NOEXCEPT
243 if ( is_started() && ! is_complete() && force_unwind() )
245 flags_ |= flag_unwind_stack;
246 param_type to( unwind_t::force_unwind);
250 flags_ &= ~flag_unwind_stack;
252 BOOST_ASSERT( is_complete() );
258 BOOST_ASSERT( ! is_running() );
259 BOOST_ASSERT( ! is_complete() );
261 flags_ |= flag_running;
262 param_type to( this);
264 static_cast< param_type * >(
268 flags_ &= ~flag_running;
269 if ( from->do_unwind) throw forced_unwind();
270 if ( except_) rethrow_exception( except_);
273 virtual void destroy() = 0;
278 #ifdef BOOST_HAS_ABI_HEADERS
279 # include BOOST_ABI_SUFFIX
282 #endif // BOOST_COROUTINES_DETAIL_PUSH_COROUTINE_IMPL_H