2 // detail/reactor_op_queue.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_REACTOR_OP_QUEUE_HPP
12 #define BOOST_ASIO_DETAIL_REACTOR_OP_QUEUE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/hash_map.hpp>
20 #include <boost/asio/detail/noncopyable.hpp>
21 #include <boost/asio/detail/op_queue.hpp>
22 #include <boost/asio/detail/reactor_op.hpp>
23 #include <boost/asio/error.hpp>
25 #include <boost/asio/detail/push_options.hpp>
31 template <typename Descriptor>
32 class reactor_op_queue
36 typedef Descriptor key_type;
38 struct mapped_type : op_queue<reactor_op>
41 mapped_type(const mapped_type&) {}
42 void operator=(const mapped_type&) {}
45 typedef typename hash_map<key_type, mapped_type>::value_type value_type;
46 typedef typename hash_map<key_type, mapped_type>::iterator iterator;
54 // Obtain iterators to all registered descriptors.
55 iterator begin() { return operations_.begin(); }
56 iterator end() { return operations_.end(); }
58 // Add a new operation to the queue. Returns true if this is the only
59 // operation for the given descriptor, in which case the reactor's event
60 // demultiplexing function call may need to be interrupted and restarted.
61 bool enqueue_operation(Descriptor descriptor, reactor_op* op)
63 std::pair<iterator, bool> entry =
64 operations_.insert(value_type(descriptor, mapped_type()));
65 entry.first->second.push(op);
69 // Cancel all operations associated with the descriptor identified by the
70 // supplied iterator. Any operations pending for the descriptor will be
71 // cancelled. Returns true if any operations were cancelled, in which case
72 // the reactor's event demultiplexing function may need to be interrupted and
74 bool cancel_operations(iterator i, op_queue<operation>& ops,
75 const boost::system::error_code& ec =
76 boost::asio::error::operation_aborted)
78 if (i != operations_.end())
80 while (reactor_op* op = i->second.front())
93 // Cancel all operations associated with the descriptor. Any operations
94 // pending for the descriptor will be cancelled. Returns true if any
95 // operations were cancelled, in which case the reactor's event
96 // demultiplexing function may need to be interrupted and restarted.
97 bool cancel_operations(Descriptor descriptor, op_queue<operation>& ops,
98 const boost::system::error_code& ec =
99 boost::asio::error::operation_aborted)
101 return this->cancel_operations(operations_.find(descriptor), ops, ec);
104 // Whether there are no operations in the queue.
107 return operations_.empty();
110 // Determine whether there are any operations associated with the descriptor.
111 bool has_operation(Descriptor descriptor) const
113 return operations_.find(descriptor) != operations_.end();
116 // Perform the operations corresponding to the descriptor identified by the
117 // supplied iterator. Returns true if there are still unfinished operations
118 // queued for the descriptor.
119 bool perform_operations(iterator i, op_queue<operation>& ops)
121 if (i != operations_.end())
123 while (reactor_op* op = i->second.front())
135 operations_.erase(i);
140 // Perform the operations corresponding to the descriptor. Returns true if
141 // there are still unfinished operations queued for the descriptor.
142 bool perform_operations(Descriptor descriptor, op_queue<operation>& ops)
144 return this->perform_operations(operations_.find(descriptor), ops);
147 // Get all operations owned by the queue.
148 void get_all_operations(op_queue<operation>& ops)
150 iterator i = operations_.begin();
151 while (i != operations_.end())
153 iterator op_iter = i++;
154 ops.push(op_iter->second);
155 operations_.erase(op_iter);
160 // The operations that are currently executing asynchronously.
161 hash_map<key_type, mapped_type> operations_;
164 } // namespace detail
168 #include <boost/asio/detail/pop_options.hpp>
170 #endif // BOOST_ASIO_DETAIL_REACTOR_OP_QUEUE_HPP