operations.hpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include <boost/fiber/stack_allocator_wrapper.hpp>
  16. #ifdef BOOST_HAS_ABI_HEADERS
  17. # include BOOST_ABI_PREFIX
  18. #endif
  19. namespace boost {
  20. namespace this_fiber {
  21. inline
  22. fibers::fiber::id get_id() noexcept {
  23. return fibers::context::active()->get_id();
  24. }
  25. inline
  26. void yield() noexcept {
  27. fibers::context::active()->yield();
  28. }
  29. template< typename Clock, typename Duration >
  30. void sleep_until( std::chrono::time_point< Clock, Duration > const& sleep_time_) {
  31. std::chrono::steady_clock::time_point sleep_time = boost::fibers::detail::convert( sleep_time_);
  32. fibers::context * active_ctx = fibers::context::active();
  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->wait_until( std::chrono::steady_clock::now() + timeout_duration);
  39. }
  40. template< typename PROPS >
  41. PROPS & properties() {
  42. fibers::fiber_properties * props = fibers::context::active()->get_properties();
  43. if ( BOOST_LIKELY( nullptr == props) ) {
  44. // props could be nullptr if the thread's main fiber has not yet
  45. // yielded (not yet passed through algorithm_with_properties::
  46. // awakened()). Address that by yielding right now.
  47. yield();
  48. // Try again to obtain the fiber_properties subclass instance ptr.
  49. // Walk through the whole chain again because who knows WHAT might
  50. // have happened while we were yielding!
  51. props = fibers::context::active()->get_properties();
  52. // Could still be hosed if the running manager isn't a subclass of
  53. // algorithm_with_properties.
  54. BOOST_ASSERT_MSG( props, "this_fiber::properties not set");
  55. }
  56. return dynamic_cast< PROPS & >( * props );
  57. }
  58. }
  59. namespace fibers {
  60. inline
  61. bool has_ready_fibers() noexcept {
  62. return boost::fibers::context::active()->get_scheduler()->has_ready_fibers();
  63. }
  64. // Returns true if the thread could be initialize, false otherwise (it was already initialized previously).
  65. inline bool initialize_thread(algo::algorithm::ptr_t algo, stack_allocator_wrapper&& salloc) noexcept {
  66. return boost::fibers::context::initialize_thread(algo, std::move(salloc));
  67. }
  68. template< typename SchedAlgo, typename ... Args >
  69. void use_scheduling_algorithm( Args && ... args) noexcept {
  70. initialize_thread(new SchedAlgo(std::forward< Args >( args) ... ), make_stack_allocator_wrapper<boost::fibers::default_stack>());
  71. }
  72. }}
  73. #ifdef BOOST_HAS_ABI_HEADERS
  74. # include BOOST_ABI_SUFFIX
  75. #endif
  76. #endif // BOOST_THIS_FIBER_OPERATIONS_H