algorithm.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_FIBERS_ALGO_ALGORITHM_H
  6. #define BOOST_FIBERS_ALGO_ALGORITHM_H
  7. #include <atomic>
  8. #include <chrono>
  9. #include <cstddef>
  10. #include <boost/assert.hpp>
  11. #include <boost/config.hpp>
  12. #include <boost/intrusive_ptr.hpp>
  13. #include <boost/fiber/properties.hpp>
  14. #include <boost/fiber/detail/config.hpp>
  15. #ifdef BOOST_HAS_ABI_HEADERS
  16. # include BOOST_ABI_PREFIX
  17. #endif
  18. namespace boost {
  19. namespace fibers {
  20. class context;
  21. namespace algo {
  22. class BOOST_FIBERS_DECL algorithm {
  23. private:
  24. std::atomic< std::size_t > use_count_{ 0 };
  25. public:
  26. typedef intrusive_ptr< algorithm > ptr_t;
  27. virtual ~algorithm() = default;
  28. virtual void awakened( context *) noexcept = 0;
  29. virtual context * pick_next() noexcept = 0;
  30. virtual bool has_ready_fibers() const noexcept = 0;
  31. virtual void suspend_until( std::chrono::steady_clock::time_point const&) noexcept = 0;
  32. virtual void notify() noexcept = 0;
  33. #if !defined(BOOST_EMBTC)
  34. friend void intrusive_ptr_add_ref( algorithm * algo) noexcept {
  35. BOOST_ASSERT( nullptr != algo);
  36. algo->use_count_.fetch_add( 1, std::memory_order_relaxed);
  37. }
  38. friend void intrusive_ptr_release( algorithm * algo) noexcept {
  39. BOOST_ASSERT( nullptr != algo);
  40. if ( 1 == algo->use_count_.fetch_sub( 1, std::memory_order_release) ) {
  41. std::atomic_thread_fence( std::memory_order_acquire);
  42. delete algo;
  43. }
  44. }
  45. #else
  46. friend void intrusive_ptr_add_ref( algorithm * algo) noexcept;
  47. friend void intrusive_ptr_release( algorithm * algo) noexcept;
  48. #endif
  49. };
  50. #if defined(BOOST_EMBTC)
  51. inline void intrusive_ptr_add_ref( algorithm * algo) noexcept {
  52. BOOST_ASSERT( nullptr != algo);
  53. algo->use_count_.fetch_add( 1, std::memory_order_relaxed);
  54. }
  55. inline void intrusive_ptr_release( algorithm * algo) noexcept {
  56. BOOST_ASSERT( nullptr != algo);
  57. if ( 1 == algo->use_count_.fetch_sub( 1, std::memory_order_release) ) {
  58. std::atomic_thread_fence( std::memory_order_acquire);
  59. delete algo;
  60. }
  61. }
  62. #endif
  63. class BOOST_FIBERS_DECL algorithm_with_properties_base : public algorithm {
  64. public:
  65. // called by fiber_properties::notify() -- don't directly call
  66. virtual void property_change_( context * ctx, fiber_properties * props) noexcept = 0;
  67. protected:
  68. static fiber_properties* get_properties( context * ctx) noexcept;
  69. static void set_properties( context * ctx, fiber_properties * p) noexcept;
  70. };
  71. template< typename PROPS >
  72. struct algorithm_with_properties : public algorithm_with_properties_base {
  73. typedef algorithm_with_properties_base super;
  74. // Mark this override 'final': algorithm_with_properties subclasses
  75. // must override awakened() with properties parameter instead. Otherwise
  76. // you'd have to remember to start every subclass awakened() override
  77. // with: algorithm_with_properties<PROPS>::awakened(fb);
  78. void awakened( context * ctx) noexcept final {
  79. fiber_properties * props = super::get_properties( ctx);
  80. if ( BOOST_LIKELY( nullptr == props) ) {
  81. // TODO: would be great if PROPS could be allocated on the new
  82. // fiber's stack somehow
  83. props = new_properties( ctx);
  84. // It is not good for new_properties() to return 0.
  85. BOOST_ASSERT_MSG( props, "new_properties() must return non-NULL");
  86. // new_properties() must return instance of (a subclass of) PROPS
  87. BOOST_ASSERT_MSG( dynamic_cast< PROPS * >( props),
  88. "new_properties() must return properties class");
  89. super::set_properties( ctx, props);
  90. }
  91. // Set algo_ again every time this fiber becomes READY. That
  92. // handles the case of a fiber migrating to a new thread with a new
  93. // algorithm subclass instance.
  94. props->set_algorithm( this);
  95. // Okay, now forward the call to subclass override.
  96. awakened( ctx, properties( ctx) );
  97. }
  98. // subclasses override this method instead of the original awakened()
  99. virtual void awakened( context *, PROPS &) noexcept = 0;
  100. // used for all internal calls
  101. PROPS & properties( context * ctx) noexcept {
  102. return static_cast< PROPS & >( * super::get_properties( ctx) );
  103. }
  104. // override this to be notified by PROPS::notify()
  105. virtual void property_change( context * /* ctx */, PROPS & /* props */) noexcept {
  106. }
  107. // implementation for algorithm_with_properties_base method
  108. void property_change_( context * ctx, fiber_properties * props) noexcept final {
  109. property_change( ctx, * static_cast< PROPS * >( props) );
  110. }
  111. // Override this to customize instantiation of PROPS, e.g. use a different
  112. // allocator. Each PROPS instance is associated with a particular
  113. // context.
  114. virtual fiber_properties * new_properties( context * ctx) {
  115. return new PROPS( ctx);
  116. }
  117. };
  118. }}}
  119. #ifdef BOOST_HAS_ABI_HEADERS
  120. # include BOOST_ABI_SUFFIX
  121. #endif
  122. #endif // BOOST_FIBERS_ALGO_ALGORITHM_H