]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/strand.hpp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / boost / boost / asio / strand.hpp
1 //
2 // strand.hpp
3 // ~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #ifndef BOOST_ASIO_STRAND_HPP
12 #define BOOST_ASIO_STRAND_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/strand_executor_service.hpp>
20 #include <boost/asio/detail/type_traits.hpp>
21
22 #include <boost/asio/detail/push_options.hpp>
23
24 namespace boost {
25 namespace asio {
26
27 /// Provides serialised function invocation for any executor type.
28 template <typename Executor>
29 class strand
30 {
31 public:
32 /// The type of the underlying executor.
33 typedef Executor inner_executor_type;
34
35 /// Default constructor.
36 /**
37 * This constructor is only valid if the underlying executor type is default
38 * constructible.
39 */
40 strand()
41 : executor_(),
42 impl_(use_service<detail::strand_executor_service>(
43 executor_.context()).create_implementation())
44 {
45 }
46
47 /// Construct a strand for the specified executor.
48 explicit strand(const Executor& e)
49 : executor_(e),
50 impl_(use_service<detail::strand_executor_service>(
51 executor_.context()).create_implementation())
52 {
53 }
54
55 /// Copy constructor.
56 strand(const strand& other) BOOST_ASIO_NOEXCEPT
57 : executor_(other.executor_),
58 impl_(other.impl_)
59 {
60 }
61
62 /// Converting constructor.
63 /**
64 * This constructor is only valid if the @c OtherExecutor type is convertible
65 * to @c Executor.
66 */
67 template <class OtherExecutor>
68 strand(
69 const strand<OtherExecutor>& other) BOOST_ASIO_NOEXCEPT
70 : executor_(other.executor_),
71 impl_(other.impl_)
72 {
73 }
74
75 /// Assignment operator.
76 strand& operator=(const strand& other) BOOST_ASIO_NOEXCEPT
77 {
78 executor_ = other.executor_;
79 impl_ = other.impl_;
80 return *this;
81 }
82
83 /// Converting assignment operator.
84 /**
85 * This assignment operator is only valid if the @c OtherExecutor type is
86 * convertible to @c Executor.
87 */
88 template <class OtherExecutor>
89 strand& operator=(
90 const strand<OtherExecutor>& other) BOOST_ASIO_NOEXCEPT
91 {
92 executor_ = other.executor_;
93 impl_ = other.impl_;
94 return *this;
95 }
96
97 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
98 /// Move constructor.
99 strand(strand&& other) BOOST_ASIO_NOEXCEPT
100 : executor_(BOOST_ASIO_MOVE_CAST(Executor)(other.executor_)),
101 impl_(BOOST_ASIO_MOVE_CAST(implementation_type)(other.impl_))
102 {
103 }
104
105 /// Converting move constructor.
106 /**
107 * This constructor is only valid if the @c OtherExecutor type is convertible
108 * to @c Executor.
109 */
110 template <class OtherExecutor>
111 strand(strand<OtherExecutor>&& other) BOOST_ASIO_NOEXCEPT
112 : executor_(BOOST_ASIO_MOVE_CAST(OtherExecutor)(other.executor_)),
113 impl_(BOOST_ASIO_MOVE_CAST(implementation_type)(other.impl_))
114 {
115 }
116
117 /// Move assignment operator.
118 strand& operator=(strand&& other) BOOST_ASIO_NOEXCEPT
119 {
120 executor_ = BOOST_ASIO_MOVE_CAST(Executor)(other.executor_);
121 impl_ = BOOST_ASIO_MOVE_CAST(implementation_type)(other.impl_);
122 return *this;
123 }
124
125 /// Converting move assignment operator.
126 /**
127 * This assignment operator is only valid if the @c OtherExecutor type is
128 * convertible to @c Executor.
129 */
130 template <class OtherExecutor>
131 strand& operator=(strand<OtherExecutor>&& other) BOOST_ASIO_NOEXCEPT
132 {
133 executor_ = BOOST_ASIO_MOVE_CAST(OtherExecutor)(other.executor_);
134 impl_ = BOOST_ASIO_MOVE_CAST(implementation_type)(other.impl_);
135 return *this;
136 }
137 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
138
139 /// Destructor.
140 ~strand()
141 {
142 }
143
144 /// Obtain the underlying executor.
145 inner_executor_type get_inner_executor() const BOOST_ASIO_NOEXCEPT
146 {
147 return executor_;
148 }
149
150 /// Obtain the underlying execution context.
151 execution_context& context() const BOOST_ASIO_NOEXCEPT
152 {
153 return executor_.context();
154 }
155
156 /// Inform the strand that it has some outstanding work to do.
157 /**
158 * The strand delegates this call to its underlying executor.
159 */
160 void on_work_started() const BOOST_ASIO_NOEXCEPT
161 {
162 executor_.on_work_started();
163 }
164
165 /// Inform the strand that some work is no longer outstanding.
166 /**
167 * The strand delegates this call to its underlying executor.
168 */
169 void on_work_finished() const BOOST_ASIO_NOEXCEPT
170 {
171 executor_.on_work_finished();
172 }
173
174 /// Request the strand to invoke the given function object.
175 /**
176 * This function is used to ask the strand to execute the given function
177 * object on its underlying executor. The function object will be executed
178 * inside this function if the strand is not otherwise busy and if the
179 * underlying executor's @c dispatch() function is also able to execute the
180 * function before returning.
181 *
182 * @param f The function object to be called. The executor will make
183 * a copy of the handler object as required. The function signature of the
184 * function object must be: @code void function(); @endcode
185 *
186 * @param a An allocator that may be used by the executor to allocate the
187 * internal storage needed for function invocation.
188 */
189 template <typename Function, typename Allocator>
190 void dispatch(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
191 {
192 detail::strand_executor_service::dispatch(impl_,
193 executor_, BOOST_ASIO_MOVE_CAST(Function)(f), a);
194 }
195
196 /// Request the strand to invoke the given function object.
197 /**
198 * This function is used to ask the executor to execute the given function
199 * object. The function object will never be executed inside this function.
200 * Instead, it will be scheduled by the underlying executor's defer function.
201 *
202 * @param f The function object to be called. The executor will make
203 * a copy of the handler object as required. The function signature of the
204 * function object must be: @code void function(); @endcode
205 *
206 * @param a An allocator that may be used by the executor to allocate the
207 * internal storage needed for function invocation.
208 */
209 template <typename Function, typename Allocator>
210 void post(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
211 {
212 detail::strand_executor_service::post(impl_,
213 executor_, BOOST_ASIO_MOVE_CAST(Function)(f), a);
214 }
215
216 /// Request the strand to invoke the given function object.
217 /**
218 * This function is used to ask the executor to execute the given function
219 * object. The function object will never be executed inside this function.
220 * Instead, it will be scheduled by the underlying executor's defer function.
221 *
222 * @param f The function object to be called. The executor will make
223 * a copy of the handler object as required. The function signature of the
224 * function object must be: @code void function(); @endcode
225 *
226 * @param a An allocator that may be used by the executor to allocate the
227 * internal storage needed for function invocation.
228 */
229 template <typename Function, typename Allocator>
230 void defer(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
231 {
232 detail::strand_executor_service::defer(impl_,
233 executor_, BOOST_ASIO_MOVE_CAST(Function)(f), a);
234 }
235
236 /// Determine whether the strand is running in the current thread.
237 /**
238 * @return @c true if the current thread is executing a function that was
239 * submitted to the strand using post(), dispatch() or defer(). Otherwise
240 * returns @c false.
241 */
242 bool running_in_this_thread() const BOOST_ASIO_NOEXCEPT
243 {
244 return detail::strand_executor_service::running_in_this_thread(impl_);
245 }
246
247 /// Compare two strands for equality.
248 /**
249 * Two strands are equal if they refer to the same ordered, non-concurrent
250 * state.
251 */
252 friend bool operator==(const strand& a, const strand& b) BOOST_ASIO_NOEXCEPT
253 {
254 return a.impl_ == b.impl_;
255 }
256
257 /// Compare two strands for inequality.
258 /**
259 * Two strands are equal if they refer to the same ordered, non-concurrent
260 * state.
261 */
262 friend bool operator!=(const strand& a, const strand& b) BOOST_ASIO_NOEXCEPT
263 {
264 return a.impl_ != b.impl_;
265 }
266
267 #if defined(GENERATING_DOCUMENTATION)
268 private:
269 #endif // defined(GENERATING_DOCUMENTATION)
270 Executor executor_;
271 typedef detail::strand_executor_service::implementation_type
272 implementation_type;
273 implementation_type impl_;
274 };
275
276 /** @defgroup make_strand boost::asio::make_strand
277 *
278 * @brief The boost::asio::make_strand function creates a @ref strand object for
279 * an executor or execution context.
280 */
281 /*@{*/
282
283 /// Create a @ref strand object for an executor.
284 template <typename Executor>
285 inline strand<Executor> make_strand(const Executor& ex,
286 typename enable_if<is_executor<Executor>::value>::type* = 0)
287 {
288 return strand<Executor>(ex);
289 }
290
291 /// Create a @ref strand object for an execution context.
292 template <typename ExecutionContext>
293 inline strand<typename ExecutionContext::executor_type>
294 make_strand(ExecutionContext& ctx,
295 typename enable_if<
296 is_convertible<ExecutionContext&, execution_context&>::value>::type* = 0)
297 {
298 return strand<typename ExecutionContext::executor_type>(ctx.get_executor());
299 }
300
301 /*@}*/
302
303 } // namespace asio
304 } // namespace boost
305
306 #include <boost/asio/detail/pop_options.hpp>
307
308 // If both io_context.hpp and strand.hpp have been included, automatically
309 // include the header file needed for the io_context::strand class.
310 #if !defined(BOOST_ASIO_NO_EXTENSIONS)
311 # if defined(BOOST_ASIO_IO_CONTEXT_HPP)
312 # include <boost/asio/io_context_strand.hpp>
313 # endif // defined(BOOST_ASIO_IO_CONTEXT_HPP)
314 #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
315
316 #endif // BOOST_ASIO_STRAND_HPP