]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | |
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) | |
6 | ||
7 | #ifndef BOOST_COROUTINES_DETAIL_PULL_COROUTINE_OBJECT_H | |
8 | #define BOOST_COROUTINES_DETAIL_PULL_COROUTINE_OBJECT_H | |
9 | ||
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> | |
15 | ||
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> | |
25 | ||
26 | #ifdef BOOST_HAS_ABI_HEADERS | |
27 | # include BOOST_ABI_PREFIX | |
28 | #endif | |
29 | ||
30 | #if defined(BOOST_MSVC) | |
31 | # pragma warning(push) | |
32 | # pragma warning(disable:4355) | |
33 | #endif | |
34 | ||
35 | namespace boost { | |
36 | namespace coroutines { | |
37 | namespace detail { | |
38 | ||
39 | struct pull_coroutine_context | |
40 | { | |
41 | coroutine_context caller; | |
42 | coroutine_context callee; | |
43 | ||
44 | template< typename Coro > | |
45 | pull_coroutine_context( preallocated const& palloc, Coro *) : | |
46 | caller(), | |
47 | callee( trampoline_pull< Coro >, palloc) | |
48 | {} | |
49 | }; | |
50 | ||
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 > | |
54 | { | |
55 | private: | |
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; | |
59 | ||
60 | Fn fn_; | |
61 | stack_context stack_ctx_; | |
62 | StackAllocator stack_alloc_; | |
63 | ||
64 | static void deallocate_( obj_t * obj) | |
65 | { | |
66 | stack_context stack_ctx( obj->stack_ctx_); | |
67 | StackAllocator stack_alloc( obj->stack_alloc_); | |
68 | obj->unwind_stack(); | |
69 | obj->~obj_t(); | |
70 | stack_alloc.deallocate( stack_ctx); | |
71 | } | |
72 | ||
73 | public: | |
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 : | |
78 | ctx_t( palloc, this), | |
79 | base_t( & this->caller, | |
80 | & this->callee, | |
81 | stack_unwind == attrs.do_unwind), | |
82 | fn_( fn), | |
83 | stack_ctx_( palloc.sctx), | |
84 | stack_alloc_( stack_alloc) | |
85 | {} | |
86 | #endif | |
87 | ||
88 | pull_coroutine_object( BOOST_RV_REF( Fn) fn, attributes const& attrs, | |
89 | preallocated const& palloc, | |
90 | StackAllocator const& stack_alloc) BOOST_NOEXCEPT : | |
91 | ctx_t( palloc, this), | |
92 | base_t( & this->caller, | |
93 | & this->callee, | |
94 | stack_unwind == attrs.do_unwind), | |
95 | #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES | |
96 | fn_( fn), | |
97 | #else | |
98 | fn_( boost::forward< Fn >( fn) ), | |
99 | #endif | |
100 | stack_ctx_( palloc.sctx), | |
101 | stack_alloc_( stack_alloc) | |
102 | {} | |
103 | ||
104 | void run() | |
105 | { | |
106 | BOOST_ASSERT( ! base_t::unwind_requested() ); | |
107 | ||
108 | base_t::flags_ |= flag_started; | |
109 | base_t::flags_ |= flag_running; | |
110 | ||
111 | // create push_coroutine | |
112 | typename PushCoro::synth_type b( & this->callee, & this->caller, false); | |
113 | PushCoro push_coro( synthesized_t::syntesized, b); | |
114 | try | |
115 | { fn_( push_coro); } | |
116 | catch ( forced_unwind const&) | |
117 | {} | |
118 | catch (...) | |
119 | { base_t::except_ = current_exception(); } | |
120 | ||
121 | base_t::flags_ |= flag_complete; | |
122 | base_t::flags_ &= ~flag_running; | |
123 | typename base_t::param_type to; | |
124 | this->callee.jump( | |
125 | this->caller, | |
126 | & to); | |
127 | BOOST_ASSERT_MSG( false, "pull_coroutine is complete"); | |
128 | } | |
129 | ||
130 | void destroy() | |
131 | { deallocate_( this); } | |
132 | }; | |
133 | ||
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 & > | |
137 | { | |
138 | private: | |
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; | |
142 | ||
143 | Fn fn_; | |
144 | stack_context stack_ctx_; | |
145 | StackAllocator stack_alloc_; | |
146 | ||
147 | static void deallocate_( obj_t * obj) | |
148 | { | |
149 | stack_context stack_ctx( obj->stack_ctx_); | |
150 | StackAllocator stack_alloc( obj->stack_alloc_); | |
151 | obj->unwind_stack(); | |
152 | obj->~obj_t(); | |
153 | stack_alloc.deallocate( stack_ctx); | |
154 | } | |
155 | ||
156 | public: | |
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, | |
163 | & this->callee, | |
164 | stack_unwind == attrs.do_unwind), | |
165 | fn_( fn), | |
166 | stack_ctx_( palloc.sctx), | |
167 | stack_alloc_( stack_alloc) | |
168 | {} | |
169 | #endif | |
170 | ||
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, | |
176 | & this->callee, | |
177 | stack_unwind == attrs.do_unwind), | |
178 | #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES | |
179 | fn_( fn), | |
180 | #else | |
181 | fn_( boost::forward< Fn >( fn) ), | |
182 | #endif | |
183 | stack_ctx_( palloc.sctx), | |
184 | stack_alloc_( stack_alloc) | |
185 | {} | |
186 | ||
187 | void run() | |
188 | { | |
189 | BOOST_ASSERT( ! base_t::unwind_requested() ); | |
190 | ||
191 | base_t::flags_ |= flag_started; | |
192 | base_t::flags_ |= flag_running; | |
193 | ||
194 | // create push_coroutine | |
195 | typename PushCoro::synth_type b( & this->callee, & this->caller, false); | |
196 | PushCoro push_coro( synthesized_t::syntesized, b); | |
197 | try | |
198 | { fn_( push_coro); } | |
199 | catch ( forced_unwind const&) | |
200 | {} | |
201 | catch (...) | |
202 | { base_t::except_ = current_exception(); } | |
203 | ||
204 | base_t::flags_ |= flag_complete; | |
205 | base_t::flags_ &= ~flag_running; | |
206 | typename base_t::param_type to; | |
207 | this->callee.jump( | |
208 | this->caller, | |
209 | & to); | |
210 | BOOST_ASSERT_MSG( false, "pull_coroutine is complete"); | |
211 | } | |
212 | ||
213 | void destroy() | |
214 | { deallocate_( this); } | |
215 | }; | |
216 | ||
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 > | |
220 | { | |
221 | private: | |
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; | |
225 | ||
226 | Fn fn_; | |
227 | stack_context stack_ctx_; | |
228 | StackAllocator stack_alloc_; | |
229 | ||
230 | static void deallocate_( obj_t * obj) | |
231 | { | |
232 | stack_context stack_ctx( obj->stack_ctx_); | |
233 | StackAllocator stack_alloc( obj->stack_alloc_); | |
234 | obj->unwind_stack(); | |
235 | obj->~obj_t(); | |
236 | stack_alloc.deallocate( stack_ctx); | |
237 | } | |
238 | ||
239 | public: | |
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, | |
246 | & this->callee, | |
247 | stack_unwind == attrs.do_unwind), | |
248 | fn_( fn), | |
249 | stack_ctx_( palloc.sctx), | |
250 | stack_alloc_( stack_alloc) | |
251 | {} | |
252 | #endif | |
253 | ||
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, | |
259 | & this->callee, | |
260 | stack_unwind == attrs.do_unwind), | |
261 | #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES | |
262 | fn_( fn), | |
263 | #else | |
264 | fn_( boost::forward< Fn >( fn) ), | |
265 | #endif | |
266 | stack_ctx_( palloc.sctx), | |
267 | stack_alloc_( stack_alloc) | |
268 | {} | |
269 | ||
270 | void run() | |
271 | { | |
272 | BOOST_ASSERT( ! base_t::unwind_requested() ); | |
273 | ||
274 | base_t::flags_ |= flag_started; | |
275 | base_t::flags_ |= flag_running; | |
276 | ||
277 | // create push_coroutine | |
278 | typename PushCoro::synth_type b( & this->callee, & this->caller, false); | |
279 | PushCoro push_coro( synthesized_t::syntesized, b); | |
280 | try | |
281 | { fn_( push_coro); } | |
282 | catch ( forced_unwind const&) | |
283 | {} | |
284 | catch (...) | |
285 | { base_t::except_ = current_exception(); } | |
286 | ||
287 | base_t::flags_ |= flag_complete; | |
288 | base_t::flags_ &= ~flag_running; | |
289 | typename base_t::param_type to; | |
290 | this->callee.jump( | |
291 | this->caller, | |
292 | & to); | |
293 | BOOST_ASSERT_MSG( false, "pull_coroutine is complete"); | |
294 | } | |
295 | ||
296 | void destroy() | |
297 | { deallocate_( this); } | |
298 | }; | |
299 | ||
300 | }}} | |
301 | ||
302 | #if defined(BOOST_MSVC) | |
303 | # pragma warning(pop) | |
304 | #endif | |
305 | ||
306 | #ifdef BOOST_HAS_ABI_HEADERS | |
307 | # include BOOST_ABI_SUFFIX | |
308 | #endif | |
309 | ||
310 | #endif // BOOST_COROUTINES_DETAIL_PULL_COROUTINE_OBJECT_H |