operations.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #ifndef BOOST_THIS_FIBER_OPERATIONS_H
  6. #define BOOST_THIS_FIBER_OPERATIONS_H
  7. #include <chrono>
  8. #include <boost/config.hpp>
  9. #include <boost/fiber/algo/algorithm.hpp>
  10. #include <boost/fiber/context.hpp>
  11. #include <boost/fiber/detail/config.hpp>
  12. #include <boost/fiber/detail/convert.hpp>
  13. #include <boost/fiber/fiber.hpp>
  14. #include <boost/fiber/scheduler.hpp>
  15. #ifdef BOOST_HAS_ABI_HEADERS
  16. # include BOOST_ABI_PREFIX
  17. #endif
  18. namespace boost {
  19. namespace this_fiber {
  20. inline
  21. fibers::fiber::id get_id() noexcept {
  22. return fibers::context::active()->get_id();
  23. }
  24. inline
  25. void yield() noexcept {
  26. fibers::context::active()->yield();
  27. }
  28. template< typename Clock, typename Duration >
  29. void sleep_until( std::chrono::time_point< Clock, Duration > const& sleep_time_) {
  30. std::chrono::steady_clock::time_point sleep_time = boost::fibers::detail::convert( sleep_time_);
  31. fibers::context * active_ctx = fibers::context::active();
  32. active_ctx->wait_until( sleep_time);
  33. }
  34. template< typename Rep, typename Period >
  35. void sleep_for( std::chrono::duration< Rep, Period > const& timeout_duration) {
  36. fibers::context * active_ctx = fibers::context::active();
  37. active_ctx->wait_until( std::chrono::steady_clock::now() + timeout_duration);
  38. }
  39. template< typename PROPS >
  40. PROPS & properties() {
  41. fibers::fiber_properties * props = fibers::context::active()->get_properties();
  42. if ( BOOST_LIKELY( nullptr == props) ) {
  43. // props could be nullptr if the thread's main fiber has not yet
  44. // yielded (not yet passed through algorithm_with_properties::
  45. // awakened()). Address that by yielding right now.
  46. yield();
  47. // Try again to obtain the fiber_properties subclass instance ptr.
  48. // Walk through the whole chain again because who knows WHAT might
  49. // have happened while we were yielding!
  50. props = fibers::context::active()->get_properties();
  51. // Could still be hosed if the running manager isn't a subclass of
  52. // algorithm_with_properties.
  53. BOOST_ASSERT_MSG( props, "this_fiber::properties not set");
  54. }
  55. return dynamic_cast< PROPS & >( * props );
  56. }
  57. }
  58. namespace fibers {
  59. inline
  60. bool has_ready_fibers() noexcept {
  61. return boost::fibers::context::active()->get_scheduler()->has_ready_fibers();
  62. }
  63. template< typename SchedAlgo, typename ... Args >
  64. void use_scheduling_algorithm( Args && ... args) noexcept {
  65. boost::fibers::context::active()->get_scheduler()
  66. ->set_algo( new SchedAlgo( std::forward< Args >( args) ... ) );
  67. }
  68. }}
  69. #ifdef BOOST_HAS_ABI_HEADERS
  70. # include BOOST_ABI_SUFFIX
  71. #endif
  72. #endif // BOOST_THIS_FIBER_OPERATIONS_H