]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright Oliver Kowalke 2013. |
2 | // Distributed under the Boost Software License, Version 1.0. | |
3 | // (See accompanying file LICENSE_1_0.txt or copy at | |
4 | // http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
6 | #ifndef BOOST_THIS_FIBER_OPERATIONS_H | |
7 | #define BOOST_THIS_FIBER_OPERATIONS_H | |
8 | ||
9 | #include <chrono> | |
10 | ||
11 | #include <boost/config.hpp> | |
12 | ||
13 | #include <boost/fiber/context.hpp> | |
14 | #include <boost/fiber/detail/config.hpp> | |
15 | #include <boost/fiber/detail/convert.hpp> | |
16 | #include <boost/fiber/fiber.hpp> | |
17 | #include <boost/fiber/scheduler.hpp> | |
18 | ||
19 | #ifdef BOOST_HAS_ABI_HEADERS | |
20 | # include BOOST_ABI_PREFIX | |
21 | #endif | |
22 | ||
23 | namespace boost { | |
24 | namespace this_fiber { | |
25 | ||
26 | inline | |
27 | fibers::fiber::id get_id() noexcept { | |
28 | return fibers::context::active()->get_id(); | |
29 | } | |
30 | ||
31 | inline | |
32 | void yield() noexcept { | |
33 | fibers::context::active()->yield(); | |
34 | } | |
35 | ||
36 | template< typename Clock, typename Duration > | |
37 | void sleep_until( std::chrono::time_point< Clock, Duration > const& sleep_time_) { | |
38 | std::chrono::steady_clock::time_point sleep_time( | |
39 | boost::fibers::detail::convert( sleep_time_) ); | |
40 | fibers::context::active()->wait_until( sleep_time); | |
41 | } | |
42 | ||
43 | template< typename Rep, typename Period > | |
44 | void sleep_for( std::chrono::duration< Rep, Period > const& timeout_duration) { | |
45 | fibers::context::active()->wait_until( | |
46 | std::chrono::steady_clock::now() + timeout_duration); | |
47 | } | |
48 | ||
49 | template< typename PROPS > | |
50 | PROPS & properties() { | |
51 | fibers::fiber_properties * props = | |
52 | fibers::context::active()->get_properties(); | |
53 | if ( ! props) { | |
54 | // props could be nullptr if the thread's main fiber has not yet | |
55 | // yielded (not yet passed through algorithm_with_properties:: | |
56 | // awakened()). Address that by yielding right now. | |
57 | yield(); | |
58 | // Try again to obtain the fiber_properties subclass instance ptr. | |
59 | // Walk through the whole chain again because who knows WHAT might | |
60 | // have happened while we were yielding! | |
61 | props = fibers::context::active()->get_properties(); | |
62 | // Could still be hosed if the running manager isn't a subclass of | |
63 | // algorithm_with_properties. | |
64 | BOOST_ASSERT_MSG(props, "this_fiber::properties not set"); | |
65 | } | |
66 | return dynamic_cast< PROPS & >( * props ); | |
67 | } | |
68 | ||
69 | } | |
70 | ||
71 | namespace fibers { | |
72 | ||
73 | inline | |
74 | bool has_ready_fibers() noexcept { | |
75 | return boost::fibers::context::active()->get_scheduler()->has_ready_fibers(); | |
76 | } | |
77 | ||
78 | template< typename SchedAlgo, typename ... Args > | |
79 | void use_scheduling_algorithm( Args && ... args) noexcept { | |
80 | boost::fibers::context::active()->get_scheduler() | |
81 | ->set_algo( | |
82 | std::unique_ptr< SchedAlgo >( | |
83 | new SchedAlgo( std::forward< Args >( args) ... ) ) ); | |
84 | } | |
85 | ||
86 | }} | |
87 | ||
88 | #ifdef BOOST_HAS_ABI_HEADERS | |
89 | # include BOOST_ABI_SUFFIX | |
90 | #endif | |
91 | ||
92 | #endif // BOOST_THIS_FIBER_OPERATIONS_H |