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
|
//
// reactor_op_queue.hpp
// ~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2008 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_DETAIL_REACTOR_OP_QUEUE_HPP
#define BOOST_ASIO_DETAIL_REACTOR_OP_QUEUE_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/push_options.hpp>
#include <boost/asio/detail/push_options.hpp>
#include <memory>
#include <boost/asio/detail/pop_options.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio/detail/handler_alloc_helpers.hpp>
#include <boost/asio/detail/hash_map.hpp>
#include <boost/asio/detail/noncopyable.hpp>
namespace boost {
namespace asio {
namespace detail {
template <typename Descriptor>
class reactor_op_queue
: private noncopyable
{
public:
// Constructor.
reactor_op_queue()
: operations_(),
cancelled_operations_(0),
complete_operations_(0)
{
}
// Add a new operation to the queue. Returns true if this is the only
// operation for the given descriptor, in which case the reactor's event
// demultiplexing function call may need to be interrupted and restarted.
template <typename Operation>
bool enqueue_operation(Descriptor descriptor, Operation operation)
{
// Allocate and construct an object to wrap the handler.
typedef handler_alloc_traits<Operation, op<Operation> > alloc_traits;
raw_handler_ptr<alloc_traits> raw_ptr(operation);
handler_ptr<alloc_traits> ptr(raw_ptr, descriptor, operation);
typedef typename operation_map::iterator iterator;
typedef typename operation_map::value_type value_type;
std::pair<iterator, bool> entry =
operations_.insert(value_type(descriptor, ptr.get()));
if (entry.second)
{
ptr.release();
return true;
}
op_base* current_op = entry.first->second;
while (current_op->next_)
current_op = current_op->next_;
current_op->next_ = ptr.release();
return false;
}
// Cancel all operations associated with the descriptor. Any operations
// pending for the descriptor will be notified that they have been cancelled
// next time perform_cancellations is called. Returns true if any operations
// were cancelled, in which case the reactor's event demultiplexing function
// may need to be interrupted and restarted.
bool cancel_operations(Descriptor descriptor)
{
typename operation_map::iterator i = operations_.find(descriptor);
if (i != operations_.end())
{
op_base* last_op = i->second;
while (last_op->next_)
last_op = last_op->next_;
last_op->next_ = cancelled_operations_;
cancelled_operations_ = i->second;
operations_.erase(i);
return true;
}
return false;
}
// Whether there are no operations in the queue.
bool empty() const
{
return operations_.empty();
}
// Determine whether there are any operations associated with the descriptor.
bool has_operation(Descriptor descriptor) const
{
return operations_.find(descriptor) != operations_.end();
}
// Perform the first operation corresponding to the descriptor. Returns true
// if there are more operations queued for the descriptor.
bool perform_operation(Descriptor descriptor,
const boost::system::error_code& result)
{
typename operation_map::iterator i = operations_.find(descriptor);
if (i != operations_.end())
{
op_base* this_op = i->second;
i->second = this_op->next_;
this_op->next_ = complete_operations_;
complete_operations_ = this_op;
bool done = this_op->perform(result);
if (done)
{
// Operation has finished.
if (i->second)
{
return true;
}
else
{
operations_.erase(i);
return false;
}
}
else
{
// Operation wants to be called again. Leave it at the front of the
// queue for this descriptor, and remove from the completed list.
complete_operations_ = this_op->next_;
this_op->next_ = i->second;
i->second = this_op;
return true;
}
}
return false;
}
// Perform all operations corresponding to the descriptor.
void perform_all_operations(Descriptor descriptor,
const boost::system::error_code& result)
{
typename operation_map::iterator i = operations_.find(descriptor);
if (i != operations_.end())
{
while (i->second)
{
op_base* this_op = i->second;
i->second = this_op->next_;
this_op->next_ = complete_operations_;
complete_operations_ = this_op;
bool done = this_op->perform(result);
if (!done)
{
// Operation has not finished yet, so leave at front of queue, and
// remove from the completed list.
complete_operations_ = this_op->next_;
this_op->next_ = i->second;
i->second = this_op;
return;
}
}
operations_.erase(i);
}
}
// Fill a descriptor set with the descriptors corresponding to each active
// operation.
template <typename Descriptor_Set>
void get_descriptors(Descriptor_Set& descriptors)
{
typename operation_map::iterator i = operations_.begin();
while (i != operations_.end())
{
Descriptor descriptor = i->first;
++i;
if (!descriptors.set(descriptor))
{
boost::system::error_code ec(error::fd_set_failure);
perform_all_operations(descriptor, ec);
}
}
}
// Perform the operations corresponding to the ready file descriptors
// contained in the given descriptor set.
template <typename Descriptor_Set>
void perform_operations_for_descriptors(const Descriptor_Set& descriptors,
const boost::system::error_code& result)
{
typename operation_map::iterator i = operations_.begin();
while (i != operations_.end())
{
typename operation_map::iterator op_iter = i++;
if (descriptors.is_set(op_iter->first))
{
op_base* this_op = op_iter->second;
op_iter->second = this_op->next_;
this_op->next_ = complete_operations_;
complete_operations_ = this_op;
bool done = this_op->perform(result);
if (done)
{
if (!op_iter->second)
operations_.erase(op_iter);
}
else
{
// Operation has not finished yet, so leave at front of queue, and
// remove from the completed list.
complete_operations_ = this_op->next_;
this_op->next_ = op_iter->second;
op_iter->second = this_op;
}
}
}
}
// Perform any pending cancels for operations.
void perform_cancellations()
{
while (cancelled_operations_)
{
op_base* this_op = cancelled_operations_;
cancelled_operations_ = this_op->next_;
this_op->next_ = complete_operations_;
complete_operations_ = this_op;
this_op->perform(boost::asio::error::operation_aborted);
}
}
// Complete all operations that are waiting to be completed.
void complete_operations()
{
while (complete_operations_)
{
op_base* next_op = complete_operations_->next_;
complete_operations_->next_ = 0;
complete_operations_->complete();
complete_operations_ = next_op;
}
}
// Destroy all operations owned by the queue.
void destroy_operations()
{
while (cancelled_operations_)
{
op_base* next_op = cancelled_operations_->next_;
cancelled_operations_->next_ = 0;
cancelled_operations_->destroy();
cancelled_operations_ = next_op;
}
while (complete_operations_)
{
op_base* next_op = complete_operations_->next_;
complete_operations_->next_ = 0;
complete_operations_->destroy();
complete_operations_ = next_op;
}
typename operation_map::iterator i = operations_.begin();
while (i != operations_.end())
{
typename operation_map::iterator op_iter = i++;
op_base* curr_op = op_iter->second;
operations_.erase(op_iter);
while (curr_op)
{
op_base* next_op = curr_op->next_;
curr_op->next_ = 0;
curr_op->destroy();
curr_op = next_op;
}
}
}
private:
// Base class for reactor operations. A function pointer is used instead of
// virtual functions to avoid the associated overhead.
class op_base
{
public:
// Get the descriptor associated with the operation.
Descriptor descriptor() const
{
return descriptor_;
}
// Perform the operation.
bool perform(const boost::system::error_code& result)
{
result_ = result;
return perform_func_(this, result_, bytes_transferred_);
}
// Destroy the operation and post the handler.
void complete()
{
complete_func_(this, result_, bytes_transferred_);
}
// Destroy the operation.
void destroy()
{
destroy_func_(this);
}
protected:
typedef bool (*perform_func_type)(op_base*,
boost::system::error_code&, std::size_t&);
typedef void (*complete_func_type)(op_base*,
const boost::system::error_code&, std::size_t);
typedef void (*destroy_func_type)(op_base*);
// Construct an operation for the given descriptor.
op_base(perform_func_type perform_func, complete_func_type complete_func,
destroy_func_type destroy_func, Descriptor descriptor)
: perform_func_(perform_func),
complete_func_(complete_func),
destroy_func_(destroy_func),
descriptor_(descriptor),
result_(),
bytes_transferred_(0),
next_(0)
{
}
// Prevent deletion through this type.
~op_base()
{
}
private:
friend class reactor_op_queue<Descriptor>;
// The function to be called to perform the operation.
perform_func_type perform_func_;
// The function to be called to delete the operation and post the handler.
complete_func_type complete_func_;
// The function to be called to delete the operation.
destroy_func_type destroy_func_;
// The descriptor associated with the operation.
Descriptor descriptor_;
// The result of the operation.
boost::system::error_code result_;
// The number of bytes transferred in the operation.
std::size_t bytes_transferred_;
// The next operation for the same file descriptor.
op_base* next_;
};
// Adaptor class template for operations.
template <typename Operation>
class op
: public op_base
{
public:
// Constructor.
op(Descriptor descriptor, Operation operation)
: op_base(&op<Operation>::do_perform, &op<Operation>::do_complete,
&op<Operation>::do_destroy, descriptor),
operation_(operation)
{
}
// Perform the operation.
static bool do_perform(op_base* base,
boost::system::error_code& result, std::size_t& bytes_transferred)
{
return static_cast<op<Operation>*>(base)->operation_.perform(
result, bytes_transferred);
}
// Destroy the operation and post the handler.
static void do_complete(op_base* base,
const boost::system::error_code& result, std::size_t bytes_transferred)
{
// Take ownership of the operation object.
typedef op<Operation> this_type;
this_type* this_op(static_cast<this_type*>(base));
typedef handler_alloc_traits<Operation, this_type> alloc_traits;
handler_ptr<alloc_traits> ptr(this_op->operation_, this_op);
// Make a copy of the error_code and the operation so that the memory can
// be deallocated before the upcall is made.
boost::system::error_code ec(result);
Operation operation(this_op->operation_);
// Free the memory associated with the operation.
ptr.reset();
// Make the upcall.
operation.complete(ec, bytes_transferred);
}
// Destroy the operation.
static void do_destroy(op_base* base)
{
// Take ownership of the operation object.
typedef op<Operation> this_type;
this_type* this_op(static_cast<this_type*>(base));
typedef handler_alloc_traits<Operation, this_type> alloc_traits;
handler_ptr<alloc_traits> ptr(this_op->operation_, this_op);
// A sub-object of the operation may be the true owner of the memory
// associated with the operation. Consequently, a local copy of the
// operation is required to ensure that any owning sub-object remains
// valid until after we have deallocated the memory here.
Operation operation(this_op->operation_);
(void)operation;
// Free the memory associated with the operation.
ptr.reset();
}
private:
Operation operation_;
};
// The type for a map of operations.
typedef hash_map<Descriptor, op_base*> operation_map;
// The operations that are currently executing asynchronously.
operation_map operations_;
// The list of operations that have been cancelled.
op_base* cancelled_operations_;
// The list of operations waiting to be completed.
op_base* complete_operations_;
};
} // namespace detail
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_DETAIL_REACTOR_OP_QUEUE_HPP
|