Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / fiber / operations.hpp
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 = boost::fibers::detail::convert( sleep_time_);
39     fibers::context::active()->wait_until( sleep_time);
40 }
41
42 template< typename Rep, typename Period >
43 void sleep_for( std::chrono::duration< Rep, Period > const& timeout_duration) {
44     fibers::context::active()->wait_until(
45             std::chrono::steady_clock::now() + timeout_duration);
46 }
47
48 template< typename PROPS >
49 PROPS & properties() {
50     fibers::fiber_properties * props = fibers::context::active()->get_properties();
51     if ( ! props) {
52         // props could be nullptr if the thread's main fiber has not yet
53         // yielded (not yet passed through algorithm_with_properties::
54         // awakened()). Address that by yielding right now.
55         yield();
56         // Try again to obtain the fiber_properties subclass instance ptr.
57         // Walk through the whole chain again because who knows WHAT might
58         // have happened while we were yielding!
59         props = fibers::context::active()->get_properties();
60         // Could still be hosed if the running manager isn't a subclass of
61         // algorithm_with_properties.
62         BOOST_ASSERT_MSG( props, "this_fiber::properties not set");
63     }
64     return dynamic_cast< PROPS & >( * props );
65 }
66
67 }
68
69 namespace fibers {
70
71 inline
72 bool has_ready_fibers() noexcept {
73     return boost::fibers::context::active()->get_scheduler()->has_ready_fibers();
74 }
75
76 template< typename SchedAlgo, typename ... Args >
77 void use_scheduling_algorithm( Args && ... args) noexcept {
78     boost::fibers::context::active()->get_scheduler()
79         ->set_algo(
80             std::unique_ptr< SchedAlgo >(
81                 new SchedAlgo( std::forward< Args >( args) ... ) ) );
82 }
83
84 }}
85
86 #ifdef BOOST_HAS_ABI_HEADERS
87 #  include BOOST_ABI_SUFFIX
88 #endif
89
90 #endif // BOOST_THIS_FIBER_OPERATIONS_H