Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / asio / detail / epoll_reactor.hpp
1 //
2 // detail/epoll_reactor.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
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)
9 //
10
11 #ifndef BOOST_ASIO_DETAIL_EPOLL_REACTOR_HPP
12 #define BOOST_ASIO_DETAIL_EPOLL_REACTOR_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19
20 #if defined(BOOST_ASIO_HAS_EPOLL)
21
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>
34
35 #include <boost/asio/detail/push_options.hpp>
36
37 namespace boost {
38 namespace asio {
39 namespace detail {
40
41 class epoll_reactor
42   : public boost::asio::detail::service_base<epoll_reactor>
43 {
44 public:
45   enum op_types { read_op = 0, write_op = 1,
46     connect_op = 1, except_op = 2, max_ops = 3 };
47
48   // Per-descriptor queues.
49   class descriptor_state : operation
50   {
51     friend class epoll_reactor;
52     friend class object_pool_access;
53
54     descriptor_state* next_;
55     descriptor_state* prev_;
56
57     mutex mutex_;
58     epoll_reactor* reactor_;
59     int descriptor_;
60     uint32_t registered_events_;
61     op_queue<reactor_op> op_queue_[max_ops];
62     bool shutdown_;
63
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);
70   };
71
72   // Per-descriptor data.
73   typedef descriptor_state* per_descriptor_data;
74
75   // Constructor.
76   BOOST_ASIO_DECL epoll_reactor(boost::asio::io_service& io_service);
77
78   // Destructor.
79   BOOST_ASIO_DECL ~epoll_reactor();
80
81   // Destroy all user-defined handler objects owned by the service.
82   BOOST_ASIO_DECL void shutdown_service();
83
84   // Recreate internal descriptors following a fork.
85   BOOST_ASIO_DECL void fork_service(
86       boost::asio::io_service::fork_event fork_ev);
87
88   // Initialise the task.
89   BOOST_ASIO_DECL void init_task();
90
91   // Register a socket with the reactor. Returns 0 on success, system error
92   // code on failure.
93   BOOST_ASIO_DECL int register_descriptor(socket_type descriptor,
94       per_descriptor_data& descriptor_data);
95
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);
101
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);
106
107   // Post a reactor operation for immediate completion.
108   void post_immediate_completion(reactor_op* op, bool is_continuation)
109   {
110     io_service_.post_immediate_completion(op, is_continuation);
111   }
112
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);
118
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);
124
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);
129
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);
133
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);
137
138   // Remove a timer queue from the reactor.
139   template <typename Time_Traits>
140   void remove_timer_queue(timer_queue<Time_Traits>& timer_queue);
141
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);
148
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)());
155
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);
158
159   // Interrupt the select loop.
160   BOOST_ASIO_DECL void interrupt();
161
162 private:
163   // The hint to pass to epoll_create to size its data structures.
164   enum { epoll_size = 20000 };
165
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();
169
170   // Create the timerfd file descriptor. Does not throw.
171   BOOST_ASIO_DECL static int do_timerfd_create();
172
173   // Allocate a new descriptor state object.
174   BOOST_ASIO_DECL descriptor_state* allocate_descriptor_state();
175
176   // Free an existing descriptor state object.
177   BOOST_ASIO_DECL void free_descriptor_state(descriptor_state* s);
178
179   // Helper function to add a new timer queue.
180   BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
181
182   // Helper function to remove a timer queue.
183   BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
184
185   // Called to recalculate and update the timeout.
186   BOOST_ASIO_DECL void update_timeout();
187
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();
192
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)
198
199   // The io_service implementation used to post completions.
200   io_service_impl& io_service_;
201
202   // Mutex to protect access to internal data.
203   mutex mutex_;
204
205   // The interrupter is used to break a blocking epoll_wait call.
206   select_interrupter interrupter_;
207
208   // The epoll file descriptor.
209   int epoll_fd_;
210
211   // The timer file descriptor.
212   int timer_fd_;
213
214   // The timer queues.
215   timer_queue_set timer_queues_;
216
217   // Whether the service has been shut down.
218   bool shutdown_;
219
220   // Mutex to protect access to the registered descriptors.
221   mutex registered_descriptors_mutex_;
222
223   // Keep track of all registered descriptors.
224   object_pool<descriptor_state> registered_descriptors_;
225
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;
229 };
230
231 } // namespace detail
232 } // namespace asio
233 } // namespace boost
234
235 #include <boost/asio/detail/pop_options.hpp>
236
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)
241
242 #endif // defined(BOOST_ASIO_HAS_EPOLL)
243
244 #endif // BOOST_ASIO_DETAIL_EPOLL_REACTOR_HPP