]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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_FIBER_H | |
8 | #define BOOST_CONTEXT_FIBER_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 fiber_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 fiber_activation_record { | |
66 | LPVOID fiber{ nullptr }; | |
67 | stack_context sctx{}; | |
68 | bool main_ctx{ true }; | |
69 | fiber_activation_record * from{ nullptr }; | |
70 | std::function< fiber_activation_record*(fiber_activation_record*&) > ontop{}; | |
71 | bool terminated{ false }; | |
72 | bool force_unwind{ false }; | |
73 | ||
74 | static fiber_activation_record *& current() noexcept; | |
75 | ||
76 | // used for toplevel-context | |
77 | // (e.g. main context, thread-entry context) | |
78 | fiber_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 | fiber_activation_record( stack_context sctx_) noexcept : | |
98 | sctx{ sctx_ }, | |
99 | main_ctx{ false } { | |
100 | } | |
101 | ||
102 | virtual ~fiber_activation_record() { | |
103 | if ( BOOST_UNLIKELY( main_ctx) ) { | |
104 | ::ConvertFiberToThread(); | |
105 | } else { | |
106 | ::DeleteFiber( fiber); | |
107 | } | |
108 | } | |
109 | ||
110 | fiber_activation_record( fiber_activation_record const&) = delete; | |
111 | fiber_activation_record & operator=( fiber_activation_record const&) = delete; | |
112 | ||
113 | bool is_main_context() const noexcept { | |
114 | return main_ctx; | |
115 | } | |
116 | ||
117 | fiber_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 | fiber_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 fiber::current() | |
138 | current() = this; | |
139 | #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) | |
140 | current()->ontop = std::bind( | |
141 | [](typename std::decay< Fn >::type & fn, fiber_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)](fiber_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 fiber_activation_record_initializer { | |
183 | fiber_activation_record_initializer() noexcept; | |
184 | ~fiber_activation_record_initializer(); | |
185 | }; | |
186 | ||
187 | struct forced_unwind { | |
188 | fiber_activation_record * from{ nullptr }; | |
189 | ||
190 | explicit forced_unwind( fiber_activation_record * from_) : | |
191 | from{ from_ } { | |
192 | } | |
193 | }; | |
194 | ||
195 | template< typename Ctx, typename StackAlloc, typename Fn > | |
196 | class fiber_capture_record : public fiber_activation_record { | |
197 | private: | |
198 | typename std::decay< StackAlloc >::type salloc_; | |
199 | typename std::decay< Fn >::type fn_; | |
200 | ||
201 | static void destroy( fiber_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->~fiber_capture_record(); | |
206 | // destroy stack with stack allocator | |
207 | salloc.deallocate( sctx); | |
208 | } | |
209 | ||
210 | public: | |
211 | fiber_capture_record( stack_context sctx, StackAlloc && salloc, Fn && fn) noexcept : | |
212 | fiber_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) | |
227 | c = boost::context::detail::invoke( fn_, std::move( c) ); | |
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 | std::move( c).resume(); | |
240 | BOOST_ASSERT_MSG( false, "fiber already terminated"); | |
241 | } | |
242 | }; | |
243 | ||
244 | template< typename Ctx, typename StackAlloc, typename Fn > | |
245 | static fiber_activation_record * create_fiber1( StackAlloc && salloc, Fn && fn) { | |
246 | typedef fiber_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::fiber_entry_func< capture_t >, record); | |
259 | return record; | |
260 | } | |
261 | ||
262 | template< typename Ctx, typename StackAlloc, typename Fn > | |
263 | static fiber_activation_record * create_fiber2( preallocated palloc, StackAlloc && salloc, Fn && fn) { | |
264 | typedef fiber_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::fiber_entry_func< capture_t >, record); | |
276 | return record; | |
277 | } | |
278 | ||
279 | } | |
280 | ||
281 | class BOOST_CONTEXT_DECL fiber { | |
282 | private: | |
283 | friend struct detail::fiber_activation_record; | |
284 | ||
285 | template< typename Ctx, typename StackAlloc, typename Fn > | |
286 | friend class detail::fiber_capture_record; | |
287 | ||
288 | template< typename Ctx, typename StackAlloc, typename Fn > | |
289 | friend detail::fiber_activation_record * detail::create_fiber1( StackAlloc &&, Fn &&); | |
290 | ||
291 | template< typename Ctx, typename StackAlloc, typename Fn > | |
292 | friend detail::fiber_activation_record * detail::create_fiber2( preallocated, StackAlloc &&, Fn &&); | |
293 | ||
294 | template< typename StackAlloc, typename Fn > | |
295 | friend fiber | |
296 | callcc( std::allocator_arg_t, StackAlloc &&, Fn &&); | |
297 | ||
298 | template< typename StackAlloc, typename Fn > | |
299 | friend fiber | |
300 | callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&); | |
301 | ||
302 | detail::fiber_activation_record * ptr_{ nullptr }; | |
303 | ||
304 | fiber( detail::fiber_activation_record * ptr) noexcept : | |
305 | ptr_{ ptr } { | |
306 | } | |
307 | ||
308 | public: | |
309 | fiber() = default; | |
310 | ||
311 | template< typename Fn, typename = detail::disable_overload< fiber, Fn > > | |
312 | fiber( Fn && fn) : | |
313 | fiber{ std::allocator_arg, | |
314 | fixedsize_stack(), | |
315 | std::forward< Fn >( fn) } { | |
316 | } | |
317 | ||
318 | template< typename StackAlloc, typename Fn > | |
319 | fiber( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) : | |
320 | ptr_{ detail::create_fiber1< fiber >( | |
321 | std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) } {; | |
322 | } | |
323 | ||
324 | template< typename StackAlloc, typename Fn > | |
325 | fiber( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) : | |
326 | ptr_{ detail::create_fiber2< fiber >( | |
327 | palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) } { | |
328 | } | |
329 | ||
330 | ~fiber() { | |
331 | if ( BOOST_UNLIKELY( nullptr != ptr_) && ! ptr_->main_ctx) { | |
332 | if ( BOOST_LIKELY( ! ptr_->terminated) ) { | |
333 | ptr_->force_unwind = true; | |
334 | ptr_->resume(); | |
335 | BOOST_ASSERT( ptr_->terminated); | |
336 | } | |
337 | ptr_->deallocate(); | |
338 | } | |
339 | } | |
340 | ||
341 | fiber( fiber const&) = delete; | |
342 | fiber & operator=( fiber const&) = delete; | |
343 | ||
344 | fiber( fiber && other) noexcept { | |
345 | swap( other); | |
346 | } | |
347 | ||
348 | fiber & operator=( fiber && other) noexcept { | |
349 | if ( BOOST_LIKELY( this != & other) ) { | |
350 | fiber tmp = std::move( other); | |
351 | swap( tmp); | |
352 | } | |
353 | return * this; | |
354 | } | |
355 | ||
356 | fiber resume() && { | |
357 | BOOST_ASSERT( nullptr != ptr_); | |
358 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
359 | detail::fiber_activation_record * ptr = detail::exchange( ptr_, nullptr)->resume(); | |
360 | #else | |
361 | detail::fiber_activation_record * ptr = std::exchange( ptr_, nullptr)->resume(); | |
362 | #endif | |
363 | if ( BOOST_UNLIKELY( detail::fiber_activation_record::current()->force_unwind) ) { | |
364 | throw detail::forced_unwind{ ptr}; | |
365 | } else if ( BOOST_UNLIKELY( nullptr != detail::fiber_activation_record::current()->ontop) ) { | |
366 | ptr = detail::fiber_activation_record::current()->ontop( ptr); | |
367 | detail::fiber_activation_record::current()->ontop = nullptr; | |
368 | } | |
369 | return { ptr }; | |
370 | } | |
371 | ||
372 | template< typename Fn > | |
373 | fiber resume_with( Fn && fn) && { | |
374 | BOOST_ASSERT( nullptr != ptr_); | |
375 | #if defined(BOOST_NO_CXX14_STD_EXCHANGE) | |
376 | detail::fiber_activation_record * ptr = | |
377 | detail::exchange( ptr_, nullptr)->resume_with< fiber >( std::forward< Fn >( fn) ); | |
378 | #else | |
379 | detail::fiber_activation_record * ptr = | |
380 | std::exchange( ptr_, nullptr)->resume_with< fiber >( std::forward< Fn >( fn) ); | |
381 | #endif | |
382 | if ( BOOST_UNLIKELY( detail::fiber_activation_record::current()->force_unwind) ) { | |
383 | throw detail::forced_unwind{ ptr}; | |
384 | } else if ( BOOST_UNLIKELY( nullptr != detail::fiber_activation_record::current()->ontop) ) { | |
385 | ptr = detail::fiber_activation_record::current()->ontop( ptr); | |
386 | detail::fiber_activation_record::current()->ontop = nullptr; | |
387 | } | |
388 | return { ptr }; | |
389 | } | |
390 | ||
391 | explicit operator bool() const noexcept { | |
392 | return nullptr != ptr_ && ! ptr_->terminated; | |
393 | } | |
394 | ||
395 | bool operator!() const noexcept { | |
396 | return nullptr == ptr_ || ptr_->terminated; | |
397 | } | |
398 | ||
399 | bool operator==( fiber const& other) const noexcept { | |
400 | return ptr_ == other.ptr_; | |
401 | } | |
402 | ||
403 | bool operator!=( fiber const& other) const noexcept { | |
404 | return ptr_ != other.ptr_; | |
405 | } | |
406 | ||
407 | bool operator<( fiber const& other) const noexcept { | |
408 | return ptr_ < other.ptr_; | |
409 | } | |
410 | ||
411 | bool operator>( fiber const& other) const noexcept { | |
412 | return other.ptr_ < ptr_; | |
413 | } | |
414 | ||
415 | bool operator<=( fiber const& other) const noexcept { | |
416 | return ! ( * this > other); | |
417 | } | |
418 | ||
419 | bool operator>=( fiber const& other) const noexcept { | |
420 | return ! ( * this < other); | |
421 | } | |
422 | ||
423 | template< typename charT, class traitsT > | |
424 | friend std::basic_ostream< charT, traitsT > & | |
425 | operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) { | |
426 | if ( nullptr != other.ptr_) { | |
427 | return os << other.ptr_; | |
428 | } else { | |
429 | return os << "{not-a-context}"; | |
430 | } | |
431 | } | |
432 | ||
433 | void swap( fiber & other) noexcept { | |
434 | std::swap( ptr_, other.ptr_); | |
435 | } | |
436 | }; | |
437 | ||
438 | inline | |
439 | void swap( fiber & l, fiber & r) noexcept { | |
440 | l.swap( r); | |
441 | } | |
442 | ||
443 | }} | |
444 | ||
445 | #if defined(BOOST_MSVC) | |
446 | # pragma warning(pop) | |
447 | #endif | |
448 | ||
449 | #ifdef BOOST_HAS_ABI_HEADERS | |
450 | # include BOOST_ABI_SUFFIX | |
451 | #endif | |
452 | ||
453 | #endif // BOOST_CONTEXT_FIBER_H |