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(
39 boost::fibers::detail::convert( sleep_time_) );
40 fibers::context::active()->wait_until( sleep_time);
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);
49 template< typename PROPS >
50 PROPS & properties() {
51 fibers::fiber_properties * props =
52 fibers::context::active()->get_properties();
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.
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");
66 return dynamic_cast< PROPS & >( * props );
74 bool has_ready_fibers() noexcept {
75 return boost::fibers::context::active()->get_scheduler()->has_ready_fibers();
78 template< typename SchedAlgo, typename ... Args >
79 void use_scheduling_algorithm( Args && ... args) noexcept {
80 boost::fibers::context::active()->get_scheduler()
82 std::unique_ptr< SchedAlgo >(
83 new SchedAlgo( std::forward< Args >( args) ... ) ) );
88 #ifdef BOOST_HAS_ABI_HEADERS
89 # include BOOST_ABI_SUFFIX
92 #endif // BOOST_THIS_FIBER_OPERATIONS_H