]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | // |
2 | // strand.hpp | |
3 | // ~~~~~~~~~~ | |
4 | // | |
11fdf7f2 | 5 | // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
b32b8144 FG |
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)), | |
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); | |
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=( | |
132 | const strand<OtherExecutor>&& other) BOOST_ASIO_NOEXCEPT | |
133 | { | |
134 | executor_ = BOOST_ASIO_MOVE_CAST(OtherExecutor)(other); | |
135 | impl_ = BOOST_ASIO_MOVE_CAST(implementation_type)(other.impl_); | |
136 | return *this; | |
137 | } | |
138 | #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) | |
139 | ||
140 | /// Destructor. | |
141 | ~strand() | |
142 | { | |
143 | } | |
144 | ||
145 | /// Obtain the underlying executor. | |
146 | inner_executor_type get_inner_executor() const BOOST_ASIO_NOEXCEPT | |
147 | { | |
148 | return executor_; | |
149 | } | |
150 | ||
151 | /// Obtain the underlying execution context. | |
152 | execution_context& context() const BOOST_ASIO_NOEXCEPT | |
153 | { | |
154 | return executor_.context(); | |
155 | } | |
156 | ||
157 | /// Inform the strand that it has some outstanding work to do. | |
158 | /** | |
159 | * The strand delegates this call to its underlying executor. | |
160 | */ | |
161 | void on_work_started() const BOOST_ASIO_NOEXCEPT | |
162 | { | |
163 | executor_.on_work_started(); | |
164 | } | |
165 | ||
166 | /// Inform the strand that some work is no longer outstanding. | |
167 | /** | |
168 | * The strand delegates this call to its underlying executor. | |
169 | */ | |
170 | void on_work_finished() const BOOST_ASIO_NOEXCEPT | |
171 | { | |
172 | executor_.on_work_finished(); | |
173 | } | |
174 | ||
175 | /// Request the strand to invoke the given function object. | |
176 | /** | |
177 | * This function is used to ask the strand to execute the given function | |
178 | * object on its underlying executor. The function object will be executed | |
179 | * inside this function if the strand is not otherwise busy and if the | |
180 | * underlying executor's @c dispatch() function is also able to execute the | |
181 | * function before returning. | |
182 | * | |
183 | * @param f The function object to be called. The executor will make | |
184 | * a copy of the handler object as required. The function signature of the | |
185 | * function object must be: @code void function(); @endcode | |
186 | * | |
187 | * @param a An allocator that may be used by the executor to allocate the | |
188 | * internal storage needed for function invocation. | |
189 | */ | |
190 | template <typename Function, typename Allocator> | |
191 | void dispatch(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const | |
192 | { | |
193 | detail::strand_executor_service::dispatch(impl_, | |
194 | executor_, BOOST_ASIO_MOVE_CAST(Function)(f), a); | |
195 | } | |
196 | ||
197 | /// Request the strand to invoke the given function object. | |
198 | /** | |
199 | * This function is used to ask the executor to execute the given function | |
200 | * object. The function object will never be executed inside this function. | |
201 | * Instead, it will be scheduled by the underlying executor's defer function. | |
202 | * | |
203 | * @param f The function object to be called. The executor will make | |
204 | * a copy of the handler object as required. The function signature of the | |
205 | * function object must be: @code void function(); @endcode | |
206 | * | |
207 | * @param a An allocator that may be used by the executor to allocate the | |
208 | * internal storage needed for function invocation. | |
209 | */ | |
210 | template <typename Function, typename Allocator> | |
211 | void post(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const | |
212 | { | |
213 | detail::strand_executor_service::post(impl_, | |
214 | executor_, BOOST_ASIO_MOVE_CAST(Function)(f), a); | |
215 | } | |
216 | ||
217 | /// Request the strand to invoke the given function object. | |
218 | /** | |
219 | * This function is used to ask the executor to execute the given function | |
220 | * object. The function object will never be executed inside this function. | |
221 | * Instead, it will be scheduled by the underlying executor's defer function. | |
222 | * | |
223 | * @param f The function object to be called. The executor will make | |
224 | * a copy of the handler object as required. The function signature of the | |
225 | * function object must be: @code void function(); @endcode | |
226 | * | |
227 | * @param a An allocator that may be used by the executor to allocate the | |
228 | * internal storage needed for function invocation. | |
229 | */ | |
230 | template <typename Function, typename Allocator> | |
231 | void defer(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const | |
232 | { | |
233 | detail::strand_executor_service::defer(impl_, | |
234 | executor_, BOOST_ASIO_MOVE_CAST(Function)(f), a); | |
235 | } | |
236 | ||
237 | /// Determine whether the strand is running in the current thread. | |
238 | /** | |
239 | * @return @c true if the current thread is executing a function that was | |
240 | * submitted to the strand using post(), dispatch() or defer(). Otherwise | |
241 | * returns @c false. | |
242 | */ | |
243 | bool running_in_this_thread() const BOOST_ASIO_NOEXCEPT | |
244 | { | |
245 | return detail::strand_executor_service::running_in_this_thread(impl_); | |
246 | } | |
247 | ||
248 | /// Compare two strands for equality. | |
249 | /** | |
250 | * Two strands are equal if they refer to the same ordered, non-concurrent | |
251 | * state. | |
252 | */ | |
253 | friend bool operator==(const strand& a, const strand& b) BOOST_ASIO_NOEXCEPT | |
254 | { | |
255 | return a.impl_ == b.impl_; | |
256 | } | |
257 | ||
258 | /// Compare two strands for inequality. | |
259 | /** | |
260 | * Two strands are equal if they refer to the same ordered, non-concurrent | |
261 | * state. | |
262 | */ | |
263 | friend bool operator!=(const strand& a, const strand& b) BOOST_ASIO_NOEXCEPT | |
264 | { | |
265 | return a.impl_ != b.impl_; | |
266 | } | |
267 | ||
268 | private: | |
269 | Executor executor_; | |
270 | typedef detail::strand_executor_service::implementation_type | |
271 | implementation_type; | |
272 | implementation_type impl_; | |
273 | }; | |
274 | ||
275 | } // namespace asio | |
276 | } // namespace boost | |
277 | ||
278 | #include <boost/asio/detail/pop_options.hpp> | |
279 | ||
280 | // If both io_context.hpp and strand.hpp have been included, automatically | |
281 | // include the header file needed for the io_context::strand class. | |
282 | #if !defined(BOOST_ASIO_NO_EXTENSIONS) | |
283 | # if defined(BOOST_ASIO_IO_CONTEXT_HPP) | |
284 | # include <boost/asio/io_context_strand.hpp> | |
285 | # endif // defined(BOOST_ASIO_IO_CONTEXT_HPP) | |
286 | #endif // !defined(BOOST_ASIO_NO_EXTENSIONS) | |
287 | ||
288 | #endif // BOOST_ASIO_STRAND_HPP |