]>
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_PUSH_COROUTINE_OBJECT_H | |
8 | #define BOOST_COROUTINES_DETAIL_PUSH_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/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> | |
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 push_coroutine_context | |
40 | { | |
41 | coroutine_context caller; | |
42 | coroutine_context callee; | |
43 | ||
44 | template< typename Coro > | |
45 | push_coroutine_context( preallocated const& palloc, Coro *) : | |
46 | caller(), | |
47 | callee( trampoline_push< Coro >, palloc) | |
48 | {} | |
49 | }; | |
50 | ||
51 | struct push_coroutine_context_void | |
52 | { | |
53 | coroutine_context caller; | |
54 | coroutine_context callee; | |
55 | ||
56 | template< typename Coro > | |
57 | push_coroutine_context_void( preallocated const& palloc, Coro *) : | |
58 | caller(), | |
59 | callee( trampoline_push_void< Coro >, palloc) | |
60 | {} | |
61 | }; | |
62 | ||
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 > | |
66 | { | |
67 | private: | |
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; | |
71 | ||
72 | Fn fn_; | |
73 | stack_context stack_ctx_; | |
74 | StackAllocator stack_alloc_; | |
75 | ||
76 | static void deallocate_( obj_t * obj) | |
77 | { | |
78 | stack_context stack_ctx( obj->stack_ctx_); | |
79 | StackAllocator stack_alloc( obj->stack_alloc_); | |
80 | obj->unwind_stack(); | |
81 | obj->~obj_t(); | |
82 | stack_alloc.deallocate( stack_ctx); | |
83 | } | |
84 | ||
85 | public: | |
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 : | |
90 | ctx_t( palloc, this), | |
91 | base_t( & this->caller, | |
92 | & this->callee, | |
93 | stack_unwind == attrs.do_unwind), | |
94 | fn_( fn), | |
95 | stack_ctx_( palloc.sctx), | |
96 | stack_alloc_( stack_alloc) | |
97 | {} | |
98 | #endif | |
99 | ||
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, | |
105 | & this->callee, | |
106 | stack_unwind == attrs.do_unwind), | |
107 | #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES | |
108 | fn_( fn), | |
109 | #else | |
110 | fn_( boost::forward< Fn >( fn) ), | |
111 | #endif | |
112 | stack_ctx_( palloc.sctx), | |
113 | stack_alloc_( stack_alloc) | |
114 | {} | |
115 | ||
116 | void run( R * result) | |
117 | { | |
118 | BOOST_ASSERT( ! base_t::unwind_requested() ); | |
119 | ||
120 | base_t::flags_ |= flag_started; | |
121 | base_t::flags_ |= flag_running; | |
122 | ||
123 | // create push_coroutine | |
124 | typename PullCoro::synth_type b( & this->callee, & this->caller, false, result); | |
125 | PullCoro pull_coro( synthesized_t::syntesized, b); | |
126 | try | |
127 | { fn_( pull_coro); } | |
128 | catch ( forced_unwind const&) | |
129 | {} | |
130 | catch (...) | |
131 | { base_t::except_ = current_exception(); } | |
132 | ||
133 | base_t::flags_ |= flag_complete; | |
134 | base_t::flags_ &= ~flag_running; | |
135 | typename base_t::param_type to; | |
136 | this->callee.jump( | |
137 | this->caller, | |
138 | & to); | |
139 | BOOST_ASSERT_MSG( false, "pull_coroutine is complete"); | |
140 | } | |
141 | ||
142 | void destroy() | |
143 | { deallocate_( this); } | |
144 | }; | |
145 | ||
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 & > | |
149 | { | |
150 | private: | |
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; | |
154 | ||
155 | Fn fn_; | |
156 | stack_context stack_ctx_; | |
157 | StackAllocator stack_alloc_; | |
158 | ||
159 | static void deallocate_( obj_t * obj) | |
160 | { | |
161 | stack_context stack_ctx( obj->stack_ctx_); | |
162 | StackAllocator stack_alloc( obj->stack_alloc_); | |
163 | obj->unwind_stack(); | |
164 | obj->~obj_t(); | |
165 | stack_alloc.deallocate( stack_ctx); | |
166 | } | |
167 | ||
168 | public: | |
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, | |
175 | & this->callee, | |
176 | stack_unwind == attrs.do_unwind), | |
177 | fn_( fn), | |
178 | stack_ctx_( palloc.sctx), | |
179 | stack_alloc_( stack_alloc) | |
180 | {} | |
181 | #endif | |
182 | ||
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, | |
188 | & this->callee, | |
189 | stack_unwind == attrs.do_unwind), | |
190 | #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES | |
191 | fn_( fn), | |
192 | #else | |
193 | fn_( boost::forward< Fn >( fn) ), | |
194 | #endif | |
195 | stack_ctx_( palloc.sctx), | |
196 | stack_alloc_( stack_alloc) | |
197 | {} | |
198 | ||
199 | void run( R * result) | |
200 | { | |
201 | BOOST_ASSERT( ! base_t::unwind_requested() ); | |
202 | ||
203 | base_t::flags_ |= flag_started; | |
204 | base_t::flags_ |= flag_running; | |
205 | ||
206 | // create push_coroutine | |
207 | typename PullCoro::synth_type b( & this->callee, & this->caller, false, result); | |
208 | PullCoro push_coro( synthesized_t::syntesized, b); | |
209 | try | |
210 | { fn_( push_coro); } | |
211 | catch ( forced_unwind const&) | |
212 | {} | |
213 | catch (...) | |
214 | { base_t::except_ = current_exception(); } | |
215 | ||
216 | base_t::flags_ |= flag_complete; | |
217 | base_t::flags_ &= ~flag_running; | |
218 | typename base_t::param_type to; | |
219 | this->callee.jump( | |
220 | this->caller, | |
221 | & to); | |
222 | BOOST_ASSERT_MSG( false, "pull_coroutine is complete"); | |
223 | } | |
224 | ||
225 | void destroy() | |
226 | { deallocate_( this); } | |
227 | }; | |
228 | ||
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 > | |
232 | { | |
233 | private: | |
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; | |
237 | ||
238 | Fn fn_; | |
239 | stack_context stack_ctx_; | |
240 | StackAllocator stack_alloc_; | |
241 | ||
242 | static void deallocate_( obj_t * obj) | |
243 | { | |
244 | stack_context stack_ctx( obj->stack_ctx_); | |
245 | StackAllocator stack_alloc( obj->stack_alloc_); | |
246 | obj->unwind_stack(); | |
247 | obj->~obj_t(); | |
248 | stack_alloc.deallocate( stack_ctx); | |
249 | } | |
250 | ||
251 | public: | |
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, | |
258 | & this->callee, | |
259 | stack_unwind == attrs.do_unwind), | |
260 | fn_( fn), | |
261 | stack_ctx_( palloc.sctx), | |
262 | stack_alloc_( stack_alloc) | |
263 | {} | |
264 | #endif | |
265 | ||
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, | |
271 | & this->callee, | |
272 | stack_unwind == attrs.do_unwind), | |
273 | #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES | |
274 | fn_( fn), | |
275 | #else | |
276 | fn_( boost::forward< Fn >( fn) ), | |
277 | #endif | |
278 | stack_ctx_( palloc.sctx), | |
279 | stack_alloc_( stack_alloc) | |
280 | {} | |
281 | ||
282 | void run() | |
283 | { | |
284 | BOOST_ASSERT( ! base_t::unwind_requested() ); | |
285 | ||
286 | base_t::flags_ |= flag_started; | |
287 | base_t::flags_ |= flag_running; | |
288 | ||
289 | // create push_coroutine | |
290 | typename PullCoro::synth_type b( & this->callee, & this->caller, false); | |
291 | PullCoro push_coro( synthesized_t::syntesized, b); | |
292 | try | |
293 | { fn_( push_coro); } | |
294 | catch ( forced_unwind const&) | |
295 | {} | |
296 | catch (...) | |
297 | { base_t::except_ = current_exception(); } | |
298 | ||
299 | base_t::flags_ |= flag_complete; | |
300 | base_t::flags_ &= ~flag_running; | |
301 | typename base_t::param_type to; | |
302 | this->callee.jump( | |
303 | this->caller, | |
304 | & to); | |
305 | BOOST_ASSERT_MSG( false, "pull_coroutine is complete"); | |
306 | } | |
307 | ||
308 | void destroy() | |
309 | { deallocate_( this); } | |
310 | }; | |
311 | ||
312 | }}} | |
313 | ||
314 | #if defined(BOOST_MSVC) | |
315 | # pragma warning(pop) | |
316 | #endif | |
317 | ||
318 | #ifdef BOOST_HAS_ABI_HEADERS | |
319 | # include BOOST_ABI_SUFFIX | |
320 | #endif | |
321 | ||
322 | #endif // BOOST_COROUTINES_DETAIL_PUSH_COROUTINE_OBJECT_H |