algorithm.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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() {}
  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. friend void intrusive_ptr_add_ref( algorithm * algo) noexcept {
  34. BOOST_ASSERT( nullptr != algo);
  35. algo->use_count_.fetch_add( 1, std::memory_order_relaxed);
  36. }
  37. friend void intrusive_ptr_release( algorithm * algo) noexcept {
  38. BOOST_ASSERT( nullptr != algo);
  39. if ( 1 == algo->use_count_.fetch_sub( 1, std::memory_order_release) ) {
  40. std::atomic_thread_fence( std::memory_order_acquire);
  41. delete algo;
  42. }
  43. }
  44. };
  45. class BOOST_FIBERS_DECL algorithm_with_properties_base : public algorithm {
  46. public:
  47. // called by fiber_properties::notify() -- don't directly call
  48. virtual void property_change_( context * ctx, fiber_properties * props) noexcept = 0;
  49. protected:
  50. static fiber_properties* get_properties( context * ctx) noexcept;
  51. static void set_properties( context * ctx, fiber_properties * p) noexcept;
  52. };
  53. template< typename PROPS >
  54. struct algorithm_with_properties : public algorithm_with_properties_base {
  55. typedef algorithm_with_properties_base super;
  56. // Mark this override 'final': algorithm_with_properties subclasses
  57. // must override awakened() with properties parameter instead. Otherwise
  58. // you'd have to remember to start every subclass awakened() override
  59. // with: algorithm_with_properties<PROPS>::awakened(fb);
  60. virtual void awakened( context * ctx) noexcept override final {
  61. fiber_properties * props = super::get_properties( ctx);
  62. if ( BOOST_LIKELY( nullptr == props) ) {
  63. // TODO: would be great if PROPS could be allocated on the new
  64. // fiber's stack somehow
  65. props = new_properties( ctx);
  66. // It is not good for new_properties() to return 0.
  67. BOOST_ASSERT_MSG( props, "new_properties() must return non-NULL");
  68. // new_properties() must return instance of (a subclass of) PROPS
  69. BOOST_ASSERT_MSG( dynamic_cast< PROPS * >( props),
  70. "new_properties() must return properties class");
  71. super::set_properties( ctx, props);
  72. }
  73. // Set algo_ again every time this fiber becomes READY. That
  74. // handles the case of a fiber migrating to a new thread with a new
  75. // algorithm subclass instance.
  76. props->set_algorithm( this);
  77. // Okay, now forward the call to subclass override.
  78. awakened( ctx, properties( ctx) );
  79. }
  80. // subclasses override this method instead of the original awakened()
  81. virtual void awakened( context *, PROPS &) noexcept = 0;
  82. // used for all internal calls
  83. PROPS & properties( context * ctx) noexcept {
  84. return static_cast< PROPS & >( * super::get_properties( ctx) );
  85. }
  86. // override this to be notified by PROPS::notify()
  87. virtual void property_change( context * ctx, PROPS & props) noexcept {
  88. }
  89. // implementation for algorithm_with_properties_base method
  90. void property_change_( context * ctx, fiber_properties * props) noexcept override final {
  91. property_change( ctx, * static_cast< PROPS * >( props) );
  92. }
  93. // Override this to customize instantiation of PROPS, e.g. use a different
  94. // allocator. Each PROPS instance is associated with a particular
  95. // context.
  96. virtual fiber_properties * new_properties( context * ctx) {
  97. return new PROPS( ctx);
  98. }
  99. };
  100. }}}
  101. #ifdef BOOST_HAS_ABI_HEADERS
  102. # include BOOST_ABI_SUFFIX
  103. #endif
  104. #endif // BOOST_FIBERS_ALGO_ALGORITHM_H