]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/fiber/algo/algorithm.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / fiber / algo / algorithm.hpp
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
6 #ifndef BOOST_FIBERS_ALGO_ALGORITHM_H
7 #define BOOST_FIBERS_ALGO_ALGORITHM_H
8
9 #include <atomic>
10 #include <chrono>
11 #include <cstddef>
12
13 #include <boost/assert.hpp>
14 #include <boost/config.hpp>
15 #include <boost/intrusive_ptr.hpp>
16
17 #include <boost/fiber/properties.hpp>
18 #include <boost/fiber/detail/config.hpp>
19
20 #ifdef BOOST_HAS_ABI_HEADERS
21 # include BOOST_ABI_PREFIX
22 #endif
23
24 namespace boost {
25 namespace fibers {
26
27 class context;
28
29 namespace algo {
30
31 class BOOST_FIBERS_DECL algorithm {
32 private:
33 std::atomic< std::size_t > use_count_{ 0 };
34
35 public:
36 typedef intrusive_ptr< algorithm > ptr_t;
37
38 virtual ~algorithm() = default;
39
40 virtual void awakened( context *) noexcept = 0;
41
42 virtual context * pick_next() noexcept = 0;
43
44 virtual bool has_ready_fibers() const noexcept = 0;
45
46 virtual void suspend_until( std::chrono::steady_clock::time_point const&) noexcept = 0;
47
48 virtual void notify() noexcept = 0;
49
50 #if !defined(BOOST_EMBTC)
51
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);
55 }
56
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);
61 delete algo;
62 }
63 }
64
65 #else
66
67 friend void intrusive_ptr_add_ref( algorithm * algo) noexcept;
68 friend void intrusive_ptr_release( algorithm * algo) noexcept;
69
70 #endif
71
72 };
73
74 #if defined(BOOST_EMBTC)
75
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);
79 }
80
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);
85 delete algo;
86 }
87 }
88
89 #endif
90
91 class BOOST_FIBERS_DECL algorithm_with_properties_base : public algorithm {
92 public:
93 // called by fiber_properties::notify() -- don't directly call
94 virtual void property_change_( context * ctx, fiber_properties * props) noexcept = 0;
95
96 protected:
97 static fiber_properties* get_properties( context * ctx) noexcept;
98 static void set_properties( context * ctx, fiber_properties * p) noexcept;
99 };
100
101 template< typename PROPS >
102 struct algorithm_with_properties : public algorithm_with_properties_base {
103 typedef algorithm_with_properties_base super;
104
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);
121 }
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);
126
127 // Okay, now forward the call to subclass override.
128 awakened( ctx, properties( ctx) );
129 }
130
131 // subclasses override this method instead of the original awakened()
132 virtual void awakened( context *, PROPS &) noexcept = 0;
133
134 // used for all internal calls
135 PROPS & properties( context * ctx) noexcept {
136 return static_cast< PROPS & >( * super::get_properties( ctx) );
137 }
138
139 // override this to be notified by PROPS::notify()
140 virtual void property_change( context * /* ctx */, PROPS & /* props */) noexcept {
141 }
142
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) );
146 }
147
148 // Override this to customize instantiation of PROPS, e.g. use a different
149 // allocator. Each PROPS instance is associated with a particular
150 // context.
151 virtual fiber_properties * new_properties( context * ctx) {
152 return new PROPS( ctx);
153 }
154 };
155
156 }}}
157
158 #ifdef BOOST_HAS_ABI_HEADERS
159 # include BOOST_ABI_SUFFIX
160 #endif
161
162 #endif // BOOST_FIBERS_ALGO_ALGORITHM_H