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)
6 #ifndef BOOST_THIS_FIBER_OPERATIONS_H
7 #define BOOST_THIS_FIBER_OPERATIONS_H
11 #include <boost/config.hpp>
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>
19 #ifdef BOOST_HAS_ABI_HEADERS
20 # include BOOST_ABI_PREFIX
24 namespace this_fiber {
27 fibers::fiber::id get_id() noexcept {
28 return fibers::context::active()->get_id();
32 void yield() noexcept {
33 fibers::context::active()->yield();
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);
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);
48 template< typename PROPS >
49 PROPS & properties() {
50 fibers::fiber_properties * props = fibers::context::active()->get_properties();
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.
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");
64 return dynamic_cast< PROPS & >( * props );
72 bool has_ready_fibers() noexcept {
73 return boost::fibers::context::active()->get_scheduler()->has_ready_fibers();
76 template< typename SchedAlgo, typename ... Args >
77 void use_scheduling_algorithm( Args && ... args) noexcept {
78 boost::fibers::context::active()->get_scheduler()
80 std::unique_ptr< SchedAlgo >(
81 new SchedAlgo( std::forward< Args >( args) ... ) ) );
86 #ifdef BOOST_HAS_ABI_HEADERS
87 # include BOOST_ABI_SUFFIX
90 #endif // BOOST_THIS_FIBER_OPERATIONS_H