operations.hpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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->twstatus.store( static_cast< std::intptr_t >( 0), std::memory_order_release);
  33. active_ctx->wait_until( sleep_time);
  34. }
  35. template< typename Rep, typename Period >
  36. void sleep_for( std::chrono::duration< Rep, Period > const& timeout_duration) {
  37. fibers::context * active_ctx = fibers::context::active();
  38. active_ctx->twstatus.store( static_cast< std::intptr_t >( 0), std::memory_order_release);
  39. active_ctx->wait_until( std::chrono::steady_clock::now() + timeout_duration);
  40. }
  41. template< typename PROPS >
  42. PROPS & properties() {
  43. fibers::fiber_properties * props = fibers::context::active()->get_properties();
  44. if ( BOOST_LIKELY( nullptr == props) ) {
  45. // props could be nullptr if the thread's main fiber has not yet
  46. // yielded (not yet passed through algorithm_with_properties::
  47. // awakened()). Address that by yielding right now.
  48. yield();
  49. // Try again to obtain the fiber_properties subclass instance ptr.
  50. // Walk through the whole chain again because who knows WHAT might
  51. // have happened while we were yielding!
  52. props = fibers::context::active()->get_properties();
  53. // Could still be hosed if the running manager isn't a subclass of
  54. // algorithm_with_properties.
  55. BOOST_ASSERT_MSG( props, "this_fiber::properties not set");
  56. }
  57. return dynamic_cast< PROPS & >( * props );
  58. }
  59. }
  60. namespace fibers {
  61. inline
  62. bool has_ready_fibers() noexcept {
  63. return boost::fibers::context::active()->get_scheduler()->has_ready_fibers();
  64. }
  65. template< typename SchedAlgo, typename ... Args >
  66. void use_scheduling_algorithm( Args && ... args) noexcept {
  67. boost::fibers::context::active()->get_scheduler()
  68. ->set_algo( new SchedAlgo( std::forward< Args >( args) ... ) );
  69. }
  70. }}
  71. #ifdef BOOST_HAS_ABI_HEADERS
  72. # include BOOST_ABI_SUFFIX
  73. #endif
  74. #endif // BOOST_THIS_FIBER_OPERATIONS_H