2 // detail/epoll_reactor.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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_EPOLL_REACTOR_HPP
12 #define BOOST_ASIO_DETAIL_EPOLL_REACTOR_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_HAS_EPOLL)
22 #include <boost/asio/io_service.hpp>
23 #include <boost/asio/detail/atomic_count.hpp>
24 #include <boost/asio/detail/limits.hpp>
25 #include <boost/asio/detail/mutex.hpp>
26 #include <boost/asio/detail/object_pool.hpp>
27 #include <boost/asio/detail/op_queue.hpp>
28 #include <boost/asio/detail/reactor_op.hpp>
29 #include <boost/asio/detail/select_interrupter.hpp>
30 #include <boost/asio/detail/socket_types.hpp>
31 #include <boost/asio/detail/timer_queue_base.hpp>
32 #include <boost/asio/detail/timer_queue_set.hpp>
33 #include <boost/asio/detail/wait_op.hpp>
35 #include <boost/asio/detail/push_options.hpp>
42 : public boost::asio::detail::service_base<epoll_reactor>
45 enum op_types { read_op = 0, write_op = 1,
46 connect_op = 1, except_op = 2, max_ops = 3 };
48 // Per-descriptor queues.
49 class descriptor_state : operation
51 friend class epoll_reactor;
52 friend class object_pool_access;
54 descriptor_state* next_;
55 descriptor_state* prev_;
58 epoll_reactor* reactor_;
60 uint32_t registered_events_;
61 op_queue<reactor_op> op_queue_[max_ops];
64 BOOST_ASIO_DECL descriptor_state();
65 void set_ready_events(uint32_t events) { task_result_ = events; }
66 BOOST_ASIO_DECL operation* perform_io(uint32_t events);
67 BOOST_ASIO_DECL static void do_complete(
68 io_service_impl* owner, operation* base,
69 const boost::system::error_code& ec, std::size_t bytes_transferred);
72 // Per-descriptor data.
73 typedef descriptor_state* per_descriptor_data;
76 BOOST_ASIO_DECL epoll_reactor(boost::asio::io_service& io_service);
79 BOOST_ASIO_DECL ~epoll_reactor();
81 // Destroy all user-defined handler objects owned by the service.
82 BOOST_ASIO_DECL void shutdown_service();
84 // Recreate internal descriptors following a fork.
85 BOOST_ASIO_DECL void fork_service(
86 boost::asio::io_service::fork_event fork_ev);
88 // Initialise the task.
89 BOOST_ASIO_DECL void init_task();
91 // Register a socket with the reactor. Returns 0 on success, system error
93 BOOST_ASIO_DECL int register_descriptor(socket_type descriptor,
94 per_descriptor_data& descriptor_data);
96 // Register a descriptor with an associated single operation. Returns 0 on
97 // success, system error code on failure.
98 BOOST_ASIO_DECL int register_internal_descriptor(
99 int op_type, socket_type descriptor,
100 per_descriptor_data& descriptor_data, reactor_op* op);
102 // Move descriptor registration from one descriptor_data object to another.
103 BOOST_ASIO_DECL void move_descriptor(socket_type descriptor,
104 per_descriptor_data& target_descriptor_data,
105 per_descriptor_data& source_descriptor_data);
107 // Post a reactor operation for immediate completion.
108 void post_immediate_completion(reactor_op* op, bool is_continuation)
110 io_service_.post_immediate_completion(op, is_continuation);
113 // Start a new operation. The reactor operation will be performed when the
114 // given descriptor is flagged as ready, or an error has occurred.
115 BOOST_ASIO_DECL void start_op(int op_type, socket_type descriptor,
116 per_descriptor_data& descriptor_data, reactor_op* op,
117 bool is_continuation, bool allow_speculative);
119 // Cancel all operations associated with the given descriptor. The
120 // handlers associated with the descriptor will be invoked with the
121 // operation_aborted error.
122 BOOST_ASIO_DECL void cancel_ops(socket_type descriptor,
123 per_descriptor_data& descriptor_data);
125 // Cancel any operations that are running against the descriptor and remove
126 // its registration from the reactor.
127 BOOST_ASIO_DECL void deregister_descriptor(socket_type descriptor,
128 per_descriptor_data& descriptor_data, bool closing);
130 // Remote the descriptor's registration from the reactor.
131 BOOST_ASIO_DECL void deregister_internal_descriptor(
132 socket_type descriptor, per_descriptor_data& descriptor_data);
134 // Add a new timer queue to the reactor.
135 template <typename Time_Traits>
136 void add_timer_queue(timer_queue<Time_Traits>& timer_queue);
138 // Remove a timer queue from the reactor.
139 template <typename Time_Traits>
140 void remove_timer_queue(timer_queue<Time_Traits>& timer_queue);
142 // Schedule a new operation in the given timer queue to expire at the
143 // specified absolute time.
144 template <typename Time_Traits>
145 void schedule_timer(timer_queue<Time_Traits>& queue,
146 const typename Time_Traits::time_type& time,
147 typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
149 // Cancel the timer operations associated with the given token. Returns the
150 // number of operations that have been posted or dispatched.
151 template <typename Time_Traits>
152 std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
153 typename timer_queue<Time_Traits>::per_timer_data& timer,
154 std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
156 // Run epoll once until interrupted or events are ready to be dispatched.
157 BOOST_ASIO_DECL void run(bool block, op_queue<operation>& ops);
159 // Interrupt the select loop.
160 BOOST_ASIO_DECL void interrupt();
163 // The hint to pass to epoll_create to size its data structures.
164 enum { epoll_size = 20000 };
166 // Create the epoll file descriptor. Throws an exception if the descriptor
167 // cannot be created.
168 BOOST_ASIO_DECL static int do_epoll_create();
170 // Create the timerfd file descriptor. Does not throw.
171 BOOST_ASIO_DECL static int do_timerfd_create();
173 // Allocate a new descriptor state object.
174 BOOST_ASIO_DECL descriptor_state* allocate_descriptor_state();
176 // Free an existing descriptor state object.
177 BOOST_ASIO_DECL void free_descriptor_state(descriptor_state* s);
179 // Helper function to add a new timer queue.
180 BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
182 // Helper function to remove a timer queue.
183 BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
185 // Called to recalculate and update the timeout.
186 BOOST_ASIO_DECL void update_timeout();
188 // Get the timeout value for the epoll_wait call. The timeout value is
189 // returned as a number of milliseconds. A return value of -1 indicates
190 // that epoll_wait should block indefinitely.
191 BOOST_ASIO_DECL int get_timeout();
193 #if defined(BOOST_ASIO_HAS_TIMERFD)
194 // Get the timeout value for the timer descriptor. The return value is the
195 // flag argument to be used when calling timerfd_settime.
196 BOOST_ASIO_DECL int get_timeout(itimerspec& ts);
197 #endif // defined(BOOST_ASIO_HAS_TIMERFD)
199 // The io_service implementation used to post completions.
200 io_service_impl& io_service_;
202 // Mutex to protect access to internal data.
205 // The interrupter is used to break a blocking epoll_wait call.
206 select_interrupter interrupter_;
208 // The epoll file descriptor.
211 // The timer file descriptor.
215 timer_queue_set timer_queues_;
217 // Whether the service has been shut down.
220 // Mutex to protect access to the registered descriptors.
221 mutex registered_descriptors_mutex_;
223 // Keep track of all registered descriptors.
224 object_pool<descriptor_state> registered_descriptors_;
226 // Helper class to do post-perform_io cleanup.
227 struct perform_io_cleanup_on_block_exit;
228 friend struct perform_io_cleanup_on_block_exit;
231 } // namespace detail
235 #include <boost/asio/detail/pop_options.hpp>
237 #include <boost/asio/detail/impl/epoll_reactor.hpp>
238 #if defined(BOOST_ASIO_HEADER_ONLY)
239 # include <boost/asio/detail/impl/epoll_reactor.ipp>
240 #endif // defined(BOOST_ASIO_HEADER_ONLY)
242 #endif // defined(BOOST_ASIO_HAS_EPOLL)
244 #endif // BOOST_ASIO_DETAIL_EPOLL_REACTOR_HPP