executor_work_guard.hpp
7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//
// executor_work_guard.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef ASIO_EXECUTOR_WORK_GUARD_HPP
#define ASIO_EXECUTOR_WORK_GUARD_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/config.hpp"
#if !defined(ASIO_NO_TS_EXECUTORS)
#include "asio/associated_executor.hpp"
#include "asio/detail/type_traits.hpp"
#include "asio/execution.hpp"
#include "asio/is_executor.hpp"
#include "asio/detail/push_options.hpp"
namespace asio {
#if !defined(ASIO_EXECUTOR_WORK_GUARD_DECL)
#define ASIO_EXECUTOR_WORK_GUARD_DECL
template <typename Executor, typename = void>
class executor_work_guard;
#endif // !defined(ASIO_EXECUTOR_WORK_GUARD_DECL)
/// An object of type @c executor_work_guard controls ownership of executor work
/// within a scope.
#if defined(GENERATING_DOCUMENTATION)
template <typename Executor>
#else // defined(GENERATING_DOCUMENTATION)
template <typename Executor, typename>
#endif // defined(GENERATING_DOCUMENTATION)
class executor_work_guard
{
public:
/// The underlying executor type.
typedef Executor executor_type;
/// Constructs a @c executor_work_guard object for the specified executor.
/**
* Stores a copy of @c e and calls <tt>on_work_started()</tt> on it.
*/
explicit executor_work_guard(const executor_type& e) ASIO_NOEXCEPT
: executor_(e),
owns_(true)
{
executor_.on_work_started();
}
/// Copy constructor.
executor_work_guard(const executor_work_guard& other) ASIO_NOEXCEPT
: executor_(other.executor_),
owns_(other.owns_)
{
if (owns_)
executor_.on_work_started();
}
#if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
/// Move constructor.
executor_work_guard(executor_work_guard&& other) ASIO_NOEXCEPT
: executor_(ASIO_MOVE_CAST(Executor)(other.executor_)),
owns_(other.owns_)
{
other.owns_ = false;
}
#endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
/// Destructor.
/**
* Unless the object has already been reset, or is in a moved-from state,
* calls <tt>on_work_finished()</tt> on the stored executor.
*/
~executor_work_guard()
{
if (owns_)
executor_.on_work_finished();
}
/// Obtain the associated executor.
executor_type get_executor() const ASIO_NOEXCEPT
{
return executor_;
}
/// Whether the executor_work_guard object owns some outstanding work.
bool owns_work() const ASIO_NOEXCEPT
{
return owns_;
}
/// Indicate that the work is no longer outstanding.
/**
* Unless the object has already been reset, or is in a moved-from state,
* calls <tt>on_work_finished()</tt> on the stored executor.
*/
void reset() ASIO_NOEXCEPT
{
if (owns_)
{
executor_.on_work_finished();
owns_ = false;
}
}
private:
// Disallow assignment.
executor_work_guard& operator=(const executor_work_guard&);
executor_type executor_;
bool owns_;
};
#if !defined(GENERATING_DOCUMENTATION)
template <typename Executor>
class executor_work_guard<Executor,
typename enable_if<
!is_executor<Executor>::value && execution::is_executor<Executor>::value
>::type>
{
public:
typedef Executor executor_type;
explicit executor_work_guard(const executor_type& e) ASIO_NOEXCEPT
: executor_(e),
owns_(true)
{
new (&work_) work_type(asio::prefer(executor_,
execution::outstanding_work.tracked));
}
executor_work_guard(const executor_work_guard& other) ASIO_NOEXCEPT
: executor_(other.executor_),
owns_(other.owns_)
{
if (owns_)
{
new (&work_) work_type(asio::prefer(executor_,
execution::outstanding_work.tracked));
}
}
#if defined(ASIO_HAS_MOVE)
executor_work_guard(executor_work_guard&& other) ASIO_NOEXCEPT
: executor_(ASIO_MOVE_CAST(Executor)(other.executor_)),
owns_(other.owns_)
{
if (owns_)
{
new (&work_) work_type(
ASIO_MOVE_CAST(work_type)(
*static_cast<work_type*>(
static_cast<void*>(&other.work_))));
other.owns_ = false;
}
}
#endif // defined(ASIO_HAS_MOVE)
~executor_work_guard()
{
if (owns_)
static_cast<work_type*>(static_cast<void*>(&work_))->~work_type();
}
executor_type get_executor() const ASIO_NOEXCEPT
{
return executor_;
}
bool owns_work() const ASIO_NOEXCEPT
{
return owns_;
}
void reset() ASIO_NOEXCEPT
{
if (owns_)
{
static_cast<work_type*>(static_cast<void*>(&work_))->~work_type();
owns_ = false;
}
}
private:
// Disallow assignment.
executor_work_guard& operator=(const executor_work_guard&);
typedef typename decay<
typename prefer_result<
const executor_type&,
execution::outstanding_work_t::tracked_t
>::type
>::type work_type;
executor_type executor_;
typename aligned_storage<sizeof(work_type),
alignment_of<work_type>::value>::type work_;
bool owns_;
};
#endif // !defined(GENERATING_DOCUMENTATION)
/// Create an @ref executor_work_guard object.
template <typename Executor>
inline executor_work_guard<Executor> make_work_guard(const Executor& ex,
typename enable_if<
is_executor<Executor>::value || execution::is_executor<Executor>::value
>::type* = 0)
{
return executor_work_guard<Executor>(ex);
}
/// Create an @ref executor_work_guard object.
template <typename ExecutionContext>
inline executor_work_guard<typename ExecutionContext::executor_type>
make_work_guard(ExecutionContext& ctx,
typename enable_if<
is_convertible<ExecutionContext&, execution_context&>::value
>::type* = 0)
{
return executor_work_guard<typename ExecutionContext::executor_type>(
ctx.get_executor());
}
/// Create an @ref executor_work_guard object.
template <typename T>
inline executor_work_guard<typename associated_executor<T>::type>
make_work_guard(const T& t,
typename enable_if<
!is_executor<T>::value && !execution::is_executor<T>::value
&& !is_convertible<T&, execution_context&
>::value>::type* = 0)
{
return executor_work_guard<typename associated_executor<T>::type>(
associated_executor<T>::get(t));
}
/// Create an @ref executor_work_guard object.
template <typename T, typename Executor>
inline executor_work_guard<typename associated_executor<T, Executor>::type>
make_work_guard(const T& t, const Executor& ex,
typename enable_if<
is_executor<Executor>::value || execution::is_executor<Executor>::value
>::type* = 0)
{
return executor_work_guard<typename associated_executor<T, Executor>::type>(
associated_executor<T, Executor>::get(t, ex));
}
/// Create an @ref executor_work_guard object.
template <typename T, typename ExecutionContext>
inline executor_work_guard<typename associated_executor<T,
typename ExecutionContext::executor_type>::type>
make_work_guard(const T& t, ExecutionContext& ctx,
typename enable_if<
!is_executor<T>::value && !execution::is_executor<T>::value
&& !is_convertible<T&, execution_context&>::value
&& is_convertible<ExecutionContext&, execution_context&>::value
>::type* = 0)
{
return executor_work_guard<typename associated_executor<T,
typename ExecutionContext::executor_type>::type>(
associated_executor<T, typename ExecutionContext::executor_type>::get(
t, ctx.get_executor()));
}
} // namespace asio
#include "asio/detail/pop_options.hpp"
#endif // !defined(ASIO_NO_TS_EXECUTORS)
#endif // ASIO_EXECUTOR_WORK_GUARD_HPP