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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//
// experimental/basic_concurrent_channel.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2025 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 BOOST_ASIO_EXPERIMENTAL_BASIC_CONCURRENT_CHANNEL_HPP
#define BOOST_ASIO_EXPERIMENTAL_BASIC_CONCURRENT_CHANNEL_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/detail/non_const_lvalue.hpp>
#include <boost/asio/detail/mutex.hpp>
#include <boost/asio/execution/executor.hpp>
#include <boost/asio/execution_context.hpp>
#include <boost/asio/experimental/detail/channel_send_functions.hpp>
#include <boost/asio/experimental/detail/channel_service.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace experimental {
namespace detail {
} // namespace detail
/// A channel for messages.
/**
* The basic_concurrent_channel class template is used for sending messages
* between different parts of the same application. A <em>message</em> is
* defined as a collection of arguments to be passed to a completion handler,
* and the set of messages supported by a channel is specified by its @c Traits
* and <tt>Signatures...</tt> template parameters. Messages may be sent and
* received using asynchronous or non-blocking synchronous operations.
*
* Unless customising the traits, applications will typically use the @c
* experimental::concurrent_channel alias template. For example:
* @code void send_loop(int i, steady_timer& timer,
* concurrent_channel<void(error_code, int)>& ch)
* {
* if (i < 10)
* {
* timer.expires_after(chrono::seconds(1));
* timer.async_wait(
* [i, &timer, &ch](error_code error)
* {
* if (!error)
* {
* ch.async_send(error_code(), i,
* [i, &timer, &ch](error_code error)
* {
* if (!error)
* {
* send_loop(i + 1, timer, ch);
* }
* });
* }
* });
* }
* else
* {
* ch.close();
* }
* }
*
* void receive_loop(concurent_channel<void(error_code, int)>& ch)
* {
* ch.async_receive(
* [&ch](error_code error, int i)
* {
* if (!error)
* {
* std::cout << "Received " << i << "\n";
* receive_loop(ch);
* }
* });
* } @endcode
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Safe.
*
* The basic_concurrent_channel class template is thread-safe, and would
* typically be used for passing messages between application code that run on
* different threads. Consider using @ref basic_channel, and its alias template
* @c experimental::channel, to pass messages between code running in a single
* thread or on the same strand.
*/
template <typename Executor, typename Traits, typename... Signatures>
class basic_concurrent_channel
#if !defined(GENERATING_DOCUMENTATION)
: public detail::channel_send_functions<
basic_concurrent_channel<Executor, Traits, Signatures...>,
Executor, Signatures...>
#endif // !defined(GENERATING_DOCUMENTATION)
{
private:
class initiate_async_send;
class initiate_async_receive;
typedef detail::channel_service<boost::asio::detail::mutex> service_type;
typedef typename service_type::template implementation_type<
Traits, Signatures...>::payload_type payload_type;
template <typename... PayloadSignatures,
BOOST_ASIO_COMPLETION_TOKEN_FOR(PayloadSignatures...) CompletionToken>
auto do_async_receive(
boost::asio::detail::completion_payload<PayloadSignatures...>*,
CompletionToken&& token)
-> decltype(
async_initiate<CompletionToken, PayloadSignatures...>(
declval<initiate_async_receive>(), token))
{
return async_initiate<CompletionToken, PayloadSignatures...>(
initiate_async_receive(this), token);
}
public:
/// The type of the executor associated with the channel.
typedef Executor executor_type;
/// Rebinds the channel type to another executor.
template <typename Executor1>
struct rebind_executor
{
/// The channel type when rebound to the specified executor.
typedef basic_concurrent_channel<Executor1, Traits, Signatures...> other;
};
/// The traits type associated with the channel.
typedef typename Traits::template rebind<Signatures...>::other traits_type;
/// Construct a basic_concurrent_channel.
/**
* This constructor creates and channel.
*
* @param ex The I/O executor that the channel will use, by default, to
* dispatch handlers for any asynchronous operations performed on the channel.
*
* @param max_buffer_size The maximum number of messages that may be buffered
* in the channel.
*/
basic_concurrent_channel(const executor_type& ex,
std::size_t max_buffer_size = 0)
: service_(&boost::asio::use_service<service_type>(
basic_concurrent_channel::get_context(ex))),
impl_(),
executor_(ex)
{
service_->construct(impl_, max_buffer_size);
}
/// Construct and open a basic_concurrent_channel.
/**
* This constructor creates and opens a channel.
*
* @param context An execution context which provides the I/O executor that
* the channel will use, by default, to dispatch handlers for any asynchronous
* operations performed on the channel.
*
* @param max_buffer_size The maximum number of messages that may be buffered
* in the channel.
*/
template <typename ExecutionContext>
basic_concurrent_channel(ExecutionContext& context,
std::size_t max_buffer_size = 0,
constraint_t<
is_convertible<ExecutionContext&, execution_context&>::value,
defaulted_constraint
> = defaulted_constraint())
: service_(&boost::asio::use_service<service_type>(context)),
impl_(),
executor_(context.get_executor())
{
service_->construct(impl_, max_buffer_size);
}
/// Move-construct a basic_concurrent_channel from another.
/**
* This constructor moves a channel from one object to another.
*
* @param other The other basic_concurrent_channel object from which the move
* will occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_concurrent_channel(const executor_type&)
* constructor.
*/
basic_concurrent_channel(basic_concurrent_channel&& other)
: service_(other.service_),
executor_(other.executor_)
{
service_->move_construct(impl_, other.impl_);
}
/// Move-assign a basic_concurrent_channel from another.
/**
* This assignment operator moves a channel from one object to another.
* Cancels any outstanding asynchronous operations associated with the target
* object.
*
* @param other The other basic_concurrent_channel object from which the move
* will occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_concurrent_channel(const executor_type&)
* constructor.
*/
basic_concurrent_channel& operator=(basic_concurrent_channel&& other)
{
if (this != &other)
{
service_->move_assign(impl_, *other.service_, other.impl_);
executor_.~executor_type();
new (&executor_) executor_type(other.executor_);
service_ = other.service_;
}
return *this;
}
// All channels have access to each other's implementations.
template <typename, typename, typename...>
friend class basic_concurrent_channel;
/// Move-construct a basic_concurrent_channel from another.
/**
* This constructor moves a channel from one object to another.
*
* @param other The other basic_concurrent_channel object from which the move
* will occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_concurrent_channel(const executor_type&)
* constructor.
*/
template <typename Executor1>
basic_concurrent_channel(
basic_concurrent_channel<Executor1, Traits, Signatures...>&& other,
constraint_t<
is_convertible<Executor1, Executor>::value
> = 0)
: service_(other.service_),
executor_(other.executor_)
{
service_->move_construct(impl_, other.impl_);
}
/// Move-assign a basic_concurrent_channel from another.
/**
* This assignment operator moves a channel from one object to another.
* Cancels any outstanding asynchronous operations associated with the target
* object.
*
* @param other The other basic_concurrent_channel object from which the move
* will occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_concurrent_channel(const executor_type&)
* constructor.
*/
template <typename Executor1>
constraint_t<
is_convertible<Executor1, Executor>::value,
basic_concurrent_channel&
> operator=(
basic_concurrent_channel<Executor1, Traits, Signatures...>&& other)
{
if (this != &other)
{
service_->move_assign(impl_, *other.service_, other.impl_);
executor_.~executor_type();
new (&executor_) executor_type(other.executor_);
service_ = other.service_;
}
return *this;
}
/// Destructor.
~basic_concurrent_channel()
{
service_->destroy(impl_);
}
/// Get the executor associated with the object.
const executor_type& get_executor() noexcept
{
return executor_;
}
/// Get the capacity of the channel's buffer.
std::size_t capacity() noexcept
{
return service_->capacity(impl_);
}
/// Determine whether the channel is open.
bool is_open() const noexcept
{
return service_->is_open(impl_);
}
/// Reset the channel to its initial state.
void reset()
{
service_->reset(impl_);
}
/// Close the channel.
void close()
{
service_->close(impl_);
}
/// Cancel all asynchronous operations waiting on the channel.
/**
* All outstanding send operations will complete with the error
* @c boost::asio::experimental::error::channel_cancelled. Outstanding receive
* operations complete with the result as determined by the channel traits.
*/
void cancel()
{
service_->cancel(impl_);
}
/// Determine whether a message can be received without blocking.
bool ready() const noexcept
{
return service_->ready(impl_);
}
#if defined(GENERATING_DOCUMENTATION)
/// Try to send a message without blocking.
/**
* Fails if the buffer is full and there are no waiting receive operations.
*
* @returns @c true on success, @c false on failure.
*/
template <typename... Args>
bool try_send(Args&&... args);
/// Try to send a message without blocking, using dispatch semantics to call
/// the receive operation's completion handler.
/**
* Fails if the buffer is full and there are no waiting receive operations.
*
* The receive operation's completion handler may be called from inside this
* function.
*
* @returns @c true on success, @c false on failure.
*/
template <typename... Args>
bool try_send_via_dispatch(Args&&... args);
/// Try to send a number of messages without blocking.
/**
* @returns The number of messages that were sent.
*/
template <typename... Args>
std::size_t try_send_n(std::size_t count, Args&&... args);
/// Try to send a number of messages without blocking, using dispatch
/// semantics to call the receive operations' completion handlers.
/**
* The receive operations' completion handlers may be called from inside this
* function.
*
* @returns The number of messages that were sent.
*/
template <typename... Args>
std::size_t try_send_n_via_dispatch(std::size_t count, Args&&... args);
/// Asynchronously send a message.
template <typename... Args,
BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
auto async_send(Args&&... args,
CompletionToken&& token);
#endif // defined(GENERATING_DOCUMENTATION)
/// Try to receive a message without blocking.
/**
* Fails if the buffer is full and there are no waiting receive operations.
*
* @returns @c true on success, @c false on failure.
*/
template <typename Handler>
bool try_receive(Handler&& handler)
{
return service_->try_receive(impl_, static_cast<Handler&&>(handler));
}
/// Asynchronously receive a message.
template <typename CompletionToken
BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
auto async_receive(
CompletionToken&& token
BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor))
#if !defined(GENERATING_DOCUMENTATION)
-> decltype(
this->do_async_receive(static_cast<payload_type*>(0),
static_cast<CompletionToken&&>(token)))
#endif // !defined(GENERATING_DOCUMENTATION)
{
return this->do_async_receive(static_cast<payload_type*>(0),
static_cast<CompletionToken&&>(token));
}
private:
// Disallow copying and assignment.
basic_concurrent_channel(
const basic_concurrent_channel&) = delete;
basic_concurrent_channel& operator=(
const basic_concurrent_channel&) = delete;
template <typename, typename, typename...>
friend class detail::channel_send_functions;
// Helper function to get an executor's context.
template <typename T>
static execution_context& get_context(const T& t,
enable_if_t<execution::is_executor<T>::value>* = 0)
{
return boost::asio::query(t, execution::context);
}
// Helper function to get an executor's context.
template <typename T>
static execution_context& get_context(const T& t,
enable_if_t<!execution::is_executor<T>::value>* = 0)
{
return t.context();
}
class initiate_async_send
{
public:
typedef Executor executor_type;
explicit initiate_async_send(basic_concurrent_channel* self)
: self_(self)
{
}
const executor_type& get_executor() const noexcept
{
return self_->get_executor();
}
template <typename SendHandler>
void operator()(SendHandler&& handler,
payload_type&& payload) const
{
boost::asio::detail::non_const_lvalue<SendHandler> handler2(handler);
self_->service_->async_send(self_->impl_,
static_cast<payload_type&&>(payload),
handler2.value, self_->get_executor());
}
private:
basic_concurrent_channel* self_;
};
class initiate_async_receive
{
public:
typedef Executor executor_type;
explicit initiate_async_receive(basic_concurrent_channel* self)
: self_(self)
{
}
const executor_type& get_executor() const noexcept
{
return self_->get_executor();
}
template <typename ReceiveHandler>
void operator()(ReceiveHandler&& handler) const
{
boost::asio::detail::non_const_lvalue<ReceiveHandler> handler2(handler);
self_->service_->async_receive(self_->impl_,
handler2.value, self_->get_executor());
}
private:
basic_concurrent_channel* self_;
};
// The service associated with the I/O object.
service_type* service_;
// The underlying implementation of the I/O object.
typename service_type::template implementation_type<
Traits, Signatures...> impl_;
// The associated executor.
Executor executor_;
};
} // namespace experimental
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_EXPERIMENTAL_BASIC_CONCURRENT_CHANNEL_HPP