]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/context/continuation_winfib.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / context / continuation_winfib.hpp
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)
227 c = 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 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
337 continuation resume() {
338 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
339 detail::activation_record * ptr = detail::exchange( ptr_, nullptr)->resume();
340 #else
341 detail::activation_record * ptr = std::exchange( ptr_, nullptr)->resume();
342 #endif
343 if ( BOOST_UNLIKELY( detail::activation_record::current()->force_unwind) ) {
344 throw detail::forced_unwind{ ptr};
345 } else if ( BOOST_UNLIKELY( nullptr != detail::activation_record::current()->ontop) ) {
346 ptr = detail::activation_record::current()->ontop( ptr);
347 detail::activation_record::current()->ontop = nullptr;
348 }
349 return continuation{ ptr };
350 }
351
352 template< typename Fn >
353 continuation resume_with( Fn && fn) {
354 #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
355 detail::activation_record * ptr =
356 detail::exchange( ptr_, nullptr)->resume_with< continuation >( std::forward< Fn >( fn) );
357 #else
358 detail::activation_record * ptr =
359 std::exchange( ptr_, nullptr)->resume_with< continuation >( std::forward< Fn >( fn) );
360 #endif
361 if ( BOOST_UNLIKELY( detail::activation_record::current()->force_unwind) ) {
362 throw detail::forced_unwind{ ptr};
363 } else if ( BOOST_UNLIKELY( nullptr != detail::activation_record::current()->ontop) ) {
364 ptr = detail::activation_record::current()->ontop( ptr);
365 detail::activation_record::current()->ontop = nullptr;
366 }
367 return continuation{ ptr };
368 }
369
370 explicit operator bool() const noexcept {
371 return nullptr != ptr_ && ! ptr_->terminated;
372 }
373
374 bool operator!() const noexcept {
375 return nullptr == ptr_ || ptr_->terminated;
376 }
377
378 bool operator==( continuation const& other) const noexcept {
379 return ptr_ == other.ptr_;
380 }
381
382 bool operator!=( continuation const& other) const noexcept {
383 return ptr_ != other.ptr_;
384 }
385
386 bool operator<( continuation const& other) const noexcept {
387 return ptr_ < other.ptr_;
388 }
389
390 bool operator>( continuation const& other) const noexcept {
391 return other.ptr_ < ptr_;
392 }
393
394 bool operator<=( continuation const& other) const noexcept {
395 return ! ( * this > other);
396 }
397
398 bool operator>=( continuation const& other) const noexcept {
399 return ! ( * this < other);
400 }
401
402 template< typename charT, class traitsT >
403 friend std::basic_ostream< charT, traitsT > &
404 operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other) {
405 if ( nullptr != other.ptr_) {
406 return os << other.ptr_;
407 } else {
408 return os << "{not-a-context}";
409 }
410 }
411
412 void swap( continuation & other) noexcept {
413 std::swap( ptr_, other.ptr_);
414 }
415 };
416
417 template<
418 typename Fn,
419 typename = detail::disable_overload< continuation, Fn >
420 >
421 continuation
422 callcc( Fn && fn) {
423 return callcc(
424 std::allocator_arg,
425 fixedsize_stack(),
426 std::forward< Fn >( fn) );
427 }
428
429 template< typename StackAlloc, typename Fn >
430 continuation
431 callcc( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) {
432 return continuation{
433 detail::create_context1< continuation >(
434 std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume();
435 }
436
437 template< typename StackAlloc, typename Fn >
438 continuation
439 callcc( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) {
440 return continuation{
441 detail::create_context2< continuation >(
442 palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume();
443 }
444
445 inline
446 void swap( continuation & l, continuation & r) noexcept {
447 l.swap( r);
448 }
449
450 }}
451
452 #if defined(BOOST_MSVC)
453 # pragma warning(pop)
454 #endif
455
456 #ifdef BOOST_HAS_ABI_HEADERS
457 # include BOOST_ABI_SUFFIX
458 #endif
459
460 #endif // BOOST_CONTEXT_CONTINUATION_H