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)
6 #ifndef BOOST_FIBERS_ALGO_ALGORITHM_H
7 #define BOOST_FIBERS_ALGO_ALGORITHM_H
13 #include <boost/assert.hpp>
14 #include <boost/config.hpp>
15 #include <boost/intrusive_ptr.hpp>
17 #include <boost/fiber/properties.hpp>
18 #include <boost/fiber/detail/config.hpp>
20 #ifdef BOOST_HAS_ABI_HEADERS
21 # include BOOST_ABI_PREFIX
31 class BOOST_FIBERS_DECL algorithm {
33 std::atomic< std::size_t > use_count_{ 0 };
36 typedef intrusive_ptr< algorithm > ptr_t;
38 virtual ~algorithm() = default;
40 virtual void awakened( context *) noexcept = 0;
42 virtual context * pick_next() noexcept = 0;
44 virtual bool has_ready_fibers() const noexcept = 0;
46 virtual void suspend_until( std::chrono::steady_clock::time_point const&) noexcept = 0;
48 virtual void notify() noexcept = 0;
50 #if !defined(BOOST_EMBTC)
52 friend void intrusive_ptr_add_ref( algorithm * algo) noexcept {
53 BOOST_ASSERT( nullptr != algo);
54 algo->use_count_.fetch_add( 1, std::memory_order_relaxed);
57 friend void intrusive_ptr_release( algorithm * algo) noexcept {
58 BOOST_ASSERT( nullptr != algo);
59 if ( 1 == algo->use_count_.fetch_sub( 1, std::memory_order_release) ) {
60 std::atomic_thread_fence( std::memory_order_acquire);
67 friend void intrusive_ptr_add_ref( algorithm * algo) noexcept;
68 friend void intrusive_ptr_release( algorithm * algo) noexcept;
74 #if defined(BOOST_EMBTC)
76 inline void intrusive_ptr_add_ref( algorithm * algo) noexcept {
77 BOOST_ASSERT( nullptr != algo);
78 algo->use_count_.fetch_add( 1, std::memory_order_relaxed);
81 inline void intrusive_ptr_release( algorithm * algo) noexcept {
82 BOOST_ASSERT( nullptr != algo);
83 if ( 1 == algo->use_count_.fetch_sub( 1, std::memory_order_release) ) {
84 std::atomic_thread_fence( std::memory_order_acquire);
91 class BOOST_FIBERS_DECL algorithm_with_properties_base : public algorithm {
93 // called by fiber_properties::notify() -- don't directly call
94 virtual void property_change_( context * ctx, fiber_properties * props) noexcept = 0;
97 static fiber_properties* get_properties( context * ctx) noexcept;
98 static void set_properties( context * ctx, fiber_properties * p) noexcept;
101 template< typename PROPS >
102 struct algorithm_with_properties : public algorithm_with_properties_base {
103 typedef algorithm_with_properties_base super;
105 // Mark this override 'final': algorithm_with_properties subclasses
106 // must override awakened() with properties parameter instead. Otherwise
107 // you'd have to remember to start every subclass awakened() override
108 // with: algorithm_with_properties<PROPS>::awakened(fb);
109 void awakened( context * ctx) noexcept final {
110 fiber_properties * props = super::get_properties( ctx);
111 if ( BOOST_LIKELY( nullptr == props) ) {
112 // TODO: would be great if PROPS could be allocated on the new
113 // fiber's stack somehow
114 props = new_properties( ctx);
115 // It is not good for new_properties() to return 0.
116 BOOST_ASSERT_MSG( props, "new_properties() must return non-NULL");
117 // new_properties() must return instance of (a subclass of) PROPS
118 BOOST_ASSERT_MSG( dynamic_cast< PROPS * >( props),
119 "new_properties() must return properties class");
120 super::set_properties( ctx, props);
122 // Set algo_ again every time this fiber becomes READY. That
123 // handles the case of a fiber migrating to a new thread with a new
124 // algorithm subclass instance.
125 props->set_algorithm( this);
127 // Okay, now forward the call to subclass override.
128 awakened( ctx, properties( ctx) );
131 // subclasses override this method instead of the original awakened()
132 virtual void awakened( context *, PROPS &) noexcept = 0;
134 // used for all internal calls
135 PROPS & properties( context * ctx) noexcept {
136 return static_cast< PROPS & >( * super::get_properties( ctx) );
139 // override this to be notified by PROPS::notify()
140 virtual void property_change( context * /* ctx */, PROPS & /* props */) noexcept {
143 // implementation for algorithm_with_properties_base method
144 void property_change_( context * ctx, fiber_properties * props) noexcept final {
145 property_change( ctx, * static_cast< PROPS * >( props) );
148 // Override this to customize instantiation of PROPS, e.g. use a different
149 // allocator. Each PROPS instance is associated with a particular
151 virtual fiber_properties * new_properties( context * ctx) {
152 return new PROPS( ctx);
158 #ifdef BOOST_HAS_ABI_HEADERS
159 # include BOOST_ABI_SUFFIX
162 #endif // BOOST_FIBERS_ALGO_ALGORITHM_H