]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | |
2 | // Copyright Oliver Kowalke 2017. | |
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_CONTEXT_CONTINUATION_H | |
8 | #define BOOST_CONTEXT_CONTINUATION_H | |
9 | ||
10 | #include <windows.h> | |
11 | ||
12 | #include <boost/context/detail/config.hpp> | |
13 | ||
14 | #include <algorithm> | |
15 | #include <cstddef> | |
16 | #include <cstdint> | |
17 | #include <cstdlib> | |
18 | #include <cstring> | |
19 | #include <functional> | |
20 | #include <memory> | |
21 | #include <ostream> | |
22 | #include <system_error> | |
23 | #include <tuple> | |
24 | #include <utility> | |
25 | ||
26 | #include <boost/assert.hpp> | |
27 | #include <boost/config.hpp> | |
28 | ||
29 | #include <boost/context/detail/disable_overload.hpp> | |
30 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
31 | #include <boost/context/detail/exchange.hpp> | |
32 | #endif | |
33 | #if defined(BOOST_NO_CXX17_STD_INVOKE) | |
34 | #include <boost/context/detail/invoke.hpp> | |
35 | #endif | |
36 | #include <boost/context/fixedsize_stack.hpp> | |
37 | #include <boost/context/flags.hpp> | |
38 | #include <boost/context/preallocated.hpp> | |
39 | #include <boost/context/stack_context.hpp> | |
40 | ||
41 | #ifdef BOOST_HAS_ABI_HEADERS | |
42 | # include BOOST_ABI_PREFIX | |
43 | #endif | |
44 | ||
45 | #if defined(BOOST_MSVC) | |
46 | # pragma warning(push) | |
47 | # pragma warning(disable: 4702) | |
48 | #endif | |
49 | ||
50 | namespace boost { | |
51 | namespace context { | |
52 | namespace detail { | |
53 | ||
54 | // tampoline function | |
55 | // entered if the execution context | |
56 | // is resumed for the first time | |
57 | template< typename Record > | |
58 | static VOID WINAPI entry_func( LPVOID data) noexcept { | |
59 | Record * record = static_cast< Record * >( data); | |
60 | BOOST_ASSERT( nullptr != record); | |
61 | // start execution of toplevel context-function | |
62 | record->run(); | |
63 | } | |
64 | ||
65 | struct BOOST_CONTEXT_DECL activation_record { | |
66 | LPVOID fiber{ nullptr }; | |
67 | stack_context sctx{}; | |
68 | bool main_ctx{ true }; | |
69 | activation_record * from{ nullptr }; | |
70 | std::function< activation_record*(activation_record*&) > ontop{}; | |
71 | bool terminated{ false }; | |
72 | bool force_unwind{ false }; | |
73 | ||
74 | static activation_record *& current() noexcept; | |
75 | ||
76 | // used for toplevel-context | |
77 | // (e.g. main context, thread-entry context) | |
78 | activation_record() noexcept { | |
79 | #if ( _WIN32_WINNT > 0x0600) | |
80 | if ( ::IsThreadAFiber() ) { | |
81 | fiber = ::GetCurrentFiber(); | |
82 | } else { | |
83 | fiber = ::ConvertThreadToFiber( nullptr); | |
84 | } | |
85 | #else | |
86 | fiber = ::ConvertThreadToFiber( nullptr); | |
87 | if ( BOOST_UNLIKELY( nullptr == fiber) ) { | |
88 | DWORD err = ::GetLastError(); | |
89 | BOOST_ASSERT( ERROR_ALREADY_FIBER == err); | |
90 | fiber = ::GetCurrentFiber(); | |
91 | BOOST_ASSERT( nullptr != fiber); | |
92 | BOOST_ASSERT( reinterpret_cast< LPVOID >( 0x1E00) != fiber); | |
93 | } | |
94 | #endif | |
95 | } | |
96 | ||
97 | activation_record( stack_context sctx_) noexcept : | |
98 | sctx{ sctx_ }, | |
99 | main_ctx{ false } { | |
100 | } | |
101 | ||
102 | virtual ~activation_record() { | |
103 | if ( BOOST_UNLIKELY( main_ctx) ) { | |
104 | ::ConvertFiberToThread(); | |
105 | } else { | |
106 | ::DeleteFiber( fiber); | |
107 | } | |
108 | } | |
109 | ||
110 | activation_record( activation_record const&) = delete; | |
111 | activation_record & operator=( activation_record const&) = delete; | |
112 | ||
113 | bool is_main_context() const noexcept { | |
114 | return main_ctx; | |
115 | } | |
116 | ||
117 | activation_record * resume() { | |
118 | from = current(); | |
119 | // store `this` in static, thread local pointer | |
120 | // `this` will become the active (running) context | |
121 | current() = this; | |
122 | // context switch from parent context to `this`-context | |
123 | // context switch | |
124 | ::SwitchToFiber( fiber); | |
125 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
126 | return detail::exchange( current()->from, nullptr); | |
127 | #else | |
128 | return std::exchange( current()->from, nullptr); | |
129 | #endif | |
130 | } | |
131 | ||
132 | template< typename Ctx, typename Fn > | |
133 | activation_record * resume_with( Fn && fn) { | |
134 | from = current(); | |
135 | // store `this` in static, thread local pointer | |
136 | // `this` will become the active (running) context | |
137 | // returned by continuation::current() | |
138 | current() = this; | |
139 | #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) | |
140 | current()->ontop = std::bind( | |
141 | [](typename std::decay< Fn >::type & fn, activation_record *& ptr){ | |
142 | Ctx c{ ptr }; | |
143 | c = fn( std::move( c) ); | |
144 | if ( ! c) { | |
145 | ptr = nullptr; | |
146 | } | |
147 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
148 | return exchange( c.ptr_, nullptr); | |
149 | #else | |
150 | return std::exchange( c.ptr_, nullptr); | |
151 | #endif | |
152 | }, | |
153 | std::forward< Fn >( fn), | |
154 | std::placeholders::_1); | |
155 | #else | |
156 | current()->ontop = [fn=std::forward<Fn>(fn)](activation_record *& ptr){ | |
157 | Ctx c{ ptr }; | |
158 | c = fn( std::move( c) ); | |
159 | if ( ! c) { | |
160 | ptr = nullptr; | |
161 | } | |
162 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
163 | return exchange( c.ptr_, nullptr); | |
164 | #else | |
165 | return std::exchange( c.ptr_, nullptr); | |
166 | #endif | |
167 | }; | |
168 | #endif | |
169 | // context switch | |
170 | ::SwitchToFiber( fiber); | |
171 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
172 | return detail::exchange( current()->from, nullptr); | |
173 | #else | |
174 | return std::exchange( current()->from, nullptr); | |
175 | #endif | |
176 | } | |
177 | ||
178 | virtual void deallocate() noexcept { | |
179 | } | |
180 | }; | |
181 | ||
182 | struct BOOST_CONTEXT_DECL activation_record_initializer { | |
183 | activation_record_initializer() noexcept; | |
184 | ~activation_record_initializer(); | |
185 | }; | |
186 | ||
187 | struct forced_unwind { | |
188 | activation_record * from{ nullptr }; | |
189 | ||
190 | explicit forced_unwind( activation_record * from_) : | |
191 | from{ from_ } { | |
192 | } | |
193 | }; | |
194 | ||
195 | template< typename Ctx, typename StackAlloc, typename Fn > | |
196 | class capture_record : public activation_record { | |
197 | private: | |
198 | typename std::decay< StackAlloc >::type salloc_; | |
199 | typename std::decay< Fn >::type fn_; | |
200 | ||
201 | static void destroy( capture_record * p) noexcept { | |
202 | typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_); | |
203 | stack_context sctx = p->sctx; | |
204 | // deallocate activation record | |
205 | p->~capture_record(); | |
206 | // destroy stack with stack allocator | |
207 | salloc.deallocate( sctx); | |
208 | } | |
209 | ||
210 | public: | |
211 | capture_record( stack_context sctx, StackAlloc && salloc, Fn && fn) noexcept : | |
212 | activation_record( sctx), | |
213 | salloc_( std::forward< StackAlloc >( salloc)), | |
214 | fn_( std::forward< Fn >( fn) ) { | |
215 | } | |
216 | ||
217 | void deallocate() noexcept override final { | |
218 | BOOST_ASSERT( main_ctx || ( ! main_ctx && terminated) ); | |
219 | destroy( this); | |
220 | } | |
221 | ||
222 | void run() { | |
223 | Ctx c{ from }; | |
224 | try { | |
225 | // invoke context-function | |
226 | #if defined(BOOST_NO_CXX17_STD_INVOKE) | |
11fdf7f2 | 227 | c = boost::context::detail::invoke( fn_, std::move( c) ); |
b32b8144 FG |
228 | #else |
229 | c = std::invoke( fn_, std::move( c) ); | |
230 | #endif | |
231 | } catch ( forced_unwind const& ex) { | |
232 | c = Ctx{ ex.from }; | |
233 | } | |
234 | // this context has finished its task | |
235 | from = nullptr; | |
236 | ontop = nullptr; | |
237 | terminated = true; | |
238 | force_unwind = false; | |
239 | c.resume(); | |
240 | BOOST_ASSERT_MSG( false, "continuation already terminated"); | |
241 | } | |
242 | }; | |
243 | ||
244 | template< typename Ctx, typename StackAlloc, typename Fn > | |
245 | static activation_record * create_context1( StackAlloc && salloc, Fn && fn) { | |
246 | typedef capture_record< Ctx, StackAlloc, Fn > capture_t; | |
247 | ||
248 | auto sctx = salloc.allocate(); | |
249 | BOOST_ASSERT( ( sizeof( capture_t) ) < sctx.size); | |
250 | // reserve space for control structure | |
251 | void * storage = reinterpret_cast< void * >( | |
252 | ( reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sizeof( capture_t) ) ) | |
253 | & ~ static_cast< uintptr_t >( 0xff) ); | |
254 | // placment new for control structure on context stack | |
255 | capture_t * record = new ( storage) capture_t{ | |
256 | sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) }; | |
257 | // create user-context | |
258 | record->fiber = ::CreateFiber( sctx.size, & detail::entry_func< capture_t >, record); | |
259 | return record; | |
260 | } | |
261 | ||
262 | template< typename Ctx, typename StackAlloc, typename Fn > | |
263 | static activation_record * create_context2( preallocated palloc, StackAlloc && salloc, Fn && fn) { | |
264 | typedef capture_record< Ctx, StackAlloc, Fn > capture_t; | |
265 | ||
266 | BOOST_ASSERT( ( sizeof( capture_t) ) < palloc.size); | |
267 | // reserve space for control structure | |
268 | void * storage = reinterpret_cast< void * >( | |
269 | ( reinterpret_cast< uintptr_t >( palloc.sp) - static_cast< uintptr_t >( sizeof( capture_t) ) ) | |
270 | & ~ static_cast< uintptr_t >( 0xff) ); | |
271 | // placment new for control structure on context stack | |
272 | capture_t * record = new ( storage) capture_t{ | |
273 | palloc.sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) }; | |
274 | // create user-context | |
275 | record->fiber = ::CreateFiber( palloc.sctx.size, & detail::entry_func< capture_t >, record); | |
276 | return record; | |
277 | } | |
278 | ||
279 | } | |
280 | ||
281 | class BOOST_CONTEXT_DECL continuation { | |
282 | private: | |
283 | friend struct detail::activation_record; | |
284 | ||
285 | template< typename Ctx, typename StackAlloc, typename Fn > | |
286 | friend class detail::capture_record; | |
287 | ||
288 | template< typename Ctx, typename StackAlloc, typename Fn > | |
289 | friend detail::activation_record * detail::create_context1( StackAlloc &&, Fn &&); | |
290 | ||
291 | template< typename Ctx, typename StackAlloc, typename Fn > | |
292 | friend detail::activation_record * detail::create_context2( preallocated, StackAlloc &&, Fn &&); | |
293 | ||
294 | template< typename StackAlloc, typename Fn > | |
295 | friend continuation | |
296 | callcc( std::allocator_arg_t, StackAlloc &&, Fn &&); | |
297 | ||
298 | template< typename StackAlloc, typename Fn > | |
299 | friend continuation | |
300 | callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&); | |
301 | ||
302 | detail::activation_record * ptr_{ nullptr }; | |
303 | ||
304 | continuation( detail::activation_record * ptr) noexcept : | |
305 | ptr_{ ptr } { | |
306 | } | |
307 | ||
308 | public: | |
309 | continuation() = default; | |
310 | ||
311 | ~continuation() { | |
312 | if ( BOOST_UNLIKELY( nullptr != ptr_) && ! ptr_->main_ctx) { | |
313 | if ( BOOST_LIKELY( ! ptr_->terminated) ) { | |
314 | ptr_->force_unwind = true; | |
315 | ptr_->resume(); | |
316 | BOOST_ASSERT( ptr_->terminated); | |
317 | } | |
318 | ptr_->deallocate(); | |
319 | } | |
320 | } | |
321 | ||
322 | continuation( continuation const&) = delete; | |
323 | continuation & operator=( continuation const&) = delete; | |
324 | ||
325 | continuation( continuation && other) noexcept { | |
326 | swap( other); | |
327 | } | |
328 | ||
329 | continuation & operator=( continuation && other) noexcept { | |
330 | if ( BOOST_LIKELY( this != & other) ) { | |
331 | continuation tmp = std::move( other); | |
332 | swap( tmp); | |
333 | } | |
334 | return * this; | |
335 | } | |
336 | ||
11fdf7f2 TL |
337 | continuation resume() & { |
338 | return std::move( * this).resume(); | |
339 | } | |
340 | ||
341 | continuation resume() && { | |
b32b8144 FG |
342 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) |
343 | detail::activation_record * ptr = detail::exchange( ptr_, nullptr)->resume(); | |
344 | #else | |
345 | detail::activation_record * ptr = std::exchange( ptr_, nullptr)->resume(); | |
346 | #endif | |
347 | if ( BOOST_UNLIKELY( detail::activation_record::current()->force_unwind) ) { | |
348 | throw detail::forced_unwind{ ptr}; | |
349 | } else if ( BOOST_UNLIKELY( nullptr != detail::activation_record::current()->ontop) ) { | |
350 | ptr = detail::activation_record::current()->ontop( ptr); | |
351 | detail::activation_record::current()->ontop = nullptr; | |
352 | } | |
11fdf7f2 TL |
353 | return { ptr }; |
354 | } | |
355 | ||
356 | template< typename Fn > | |
357 | continuation resume_with( Fn && fn) & { | |
358 | return std::move( * this).resume_with( std::forward< Fn >( fn) ); | |
b32b8144 FG |
359 | } |
360 | ||
361 | template< typename Fn > | |
11fdf7f2 | 362 | continuation resume_with( Fn && fn) && { |
b32b8144 FG |
363 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) |
364 | detail::activation_record * ptr = | |
365 | detail::exchange( ptr_, nullptr)->resume_with< continuation >( std::forward< Fn >( fn) ); | |
366 | #else | |
367 | detail::activation_record * ptr = | |
368 | std::exchange( ptr_, nullptr)->resume_with< continuation >( std::forward< Fn >( fn) ); | |
369 | #endif | |
370 | if ( BOOST_UNLIKELY( detail::activation_record::current()->force_unwind) ) { | |
371 | throw detail::forced_unwind{ ptr}; | |
372 | } else if ( BOOST_UNLIKELY( nullptr != detail::activation_record::current()->ontop) ) { | |
373 | ptr = detail::activation_record::current()->ontop( ptr); | |
374 | detail::activation_record::current()->ontop = nullptr; | |
375 | } | |
11fdf7f2 | 376 | return { ptr }; |
b32b8144 FG |
377 | } |
378 | ||
379 | explicit operator bool() const noexcept { | |
380 | return nullptr != ptr_ && ! ptr_->terminated; | |
381 | } | |
382 | ||
383 | bool operator!() const noexcept { | |
384 | return nullptr == ptr_ || ptr_->terminated; | |
385 | } | |
386 | ||
387 | bool operator==( continuation const& other) const noexcept { | |
388 | return ptr_ == other.ptr_; | |
389 | } | |
390 | ||
391 | bool operator!=( continuation const& other) const noexcept { | |
392 | return ptr_ != other.ptr_; | |
393 | } | |
394 | ||
395 | bool operator<( continuation const& other) const noexcept { | |
396 | return ptr_ < other.ptr_; | |
397 | } | |
398 | ||
399 | bool operator>( continuation const& other) const noexcept { | |
400 | return other.ptr_ < ptr_; | |
401 | } | |
402 | ||
403 | bool operator<=( continuation const& other) const noexcept { | |
404 | return ! ( * this > other); | |
405 | } | |
406 | ||
407 | bool operator>=( continuation const& other) const noexcept { | |
408 | return ! ( * this < other); | |
409 | } | |
410 | ||
411 | template< typename charT, class traitsT > | |
412 | friend std::basic_ostream< charT, traitsT > & | |
413 | operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other) { | |
414 | if ( nullptr != other.ptr_) { | |
415 | return os << other.ptr_; | |
416 | } else { | |
417 | return os << "{not-a-context}"; | |
418 | } | |
419 | } | |
420 | ||
421 | void swap( continuation & other) noexcept { | |
422 | std::swap( ptr_, other.ptr_); | |
423 | } | |
424 | }; | |
425 | ||
426 | template< | |
427 | typename Fn, | |
428 | typename = detail::disable_overload< continuation, Fn > | |
429 | > | |
430 | continuation | |
431 | callcc( Fn && fn) { | |
432 | return callcc( | |
433 | std::allocator_arg, | |
434 | fixedsize_stack(), | |
435 | std::forward< Fn >( fn) ); | |
436 | } | |
437 | ||
438 | template< typename StackAlloc, typename Fn > | |
439 | continuation | |
440 | callcc( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) { | |
441 | return continuation{ | |
442 | detail::create_context1< continuation >( | |
443 | std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume(); | |
444 | } | |
445 | ||
446 | template< typename StackAlloc, typename Fn > | |
447 | continuation | |
448 | callcc( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) { | |
449 | return continuation{ | |
450 | detail::create_context2< continuation >( | |
451 | palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume(); | |
452 | } | |
453 | ||
454 | inline | |
455 | void swap( continuation & l, continuation & r) noexcept { | |
456 | l.swap( r); | |
457 | } | |
458 | ||
459 | }} | |
460 | ||
461 | #if defined(BOOST_MSVC) | |
462 | # pragma warning(pop) | |
463 | #endif | |
464 | ||
465 | #ifdef BOOST_HAS_ABI_HEADERS | |
466 | # include BOOST_ABI_SUFFIX | |
467 | #endif | |
468 | ||
469 | #endif // BOOST_CONTEXT_CONTINUATION_H |