File indexing completed on 2025-01-18 09:28:38
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_ASIO_DETAIL_EPOLL_REACTOR_HPP
0012 #define BOOST_ASIO_DETAIL_EPOLL_REACTOR_HPP
0013
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif
0017
0018 #include <boost/asio/detail/config.hpp>
0019
0020 #if defined(BOOST_ASIO_HAS_EPOLL)
0021
0022 #include <boost/asio/detail/atomic_count.hpp>
0023 #include <boost/asio/detail/conditionally_enabled_mutex.hpp>
0024 #include <boost/asio/detail/limits.hpp>
0025 #include <boost/asio/detail/object_pool.hpp>
0026 #include <boost/asio/detail/op_queue.hpp>
0027 #include <boost/asio/detail/reactor_op.hpp>
0028 #include <boost/asio/detail/scheduler_task.hpp>
0029 #include <boost/asio/detail/select_interrupter.hpp>
0030 #include <boost/asio/detail/socket_types.hpp>
0031 #include <boost/asio/detail/timer_queue_base.hpp>
0032 #include <boost/asio/detail/timer_queue_set.hpp>
0033 #include <boost/asio/detail/wait_op.hpp>
0034 #include <boost/asio/execution_context.hpp>
0035
0036 #if defined(BOOST_ASIO_HAS_TIMERFD)
0037 # include <sys/timerfd.h>
0038 #endif
0039
0040 #include <boost/asio/detail/push_options.hpp>
0041
0042 namespace boost {
0043 namespace asio {
0044 namespace detail {
0045
0046 class epoll_reactor
0047 : public execution_context_service_base<epoll_reactor>,
0048 public scheduler_task
0049 {
0050 private:
0051
0052 typedef conditionally_enabled_mutex mutex;
0053
0054 public:
0055 enum op_types { read_op = 0, write_op = 1,
0056 connect_op = 1, except_op = 2, max_ops = 3 };
0057
0058
0059 class descriptor_state : operation
0060 {
0061 friend class epoll_reactor;
0062 friend class object_pool_access;
0063
0064 descriptor_state* next_;
0065 descriptor_state* prev_;
0066
0067 mutex mutex_;
0068 epoll_reactor* reactor_;
0069 int descriptor_;
0070 uint32_t registered_events_;
0071 op_queue<reactor_op> op_queue_[max_ops];
0072 bool try_speculative_[max_ops];
0073 bool shutdown_;
0074
0075 BOOST_ASIO_DECL descriptor_state(bool locking);
0076 void set_ready_events(uint32_t events) { task_result_ = events; }
0077 void add_ready_events(uint32_t events) { task_result_ |= events; }
0078 BOOST_ASIO_DECL operation* perform_io(uint32_t events);
0079 BOOST_ASIO_DECL static void do_complete(
0080 void* owner, operation* base,
0081 const boost::system::error_code& ec, std::size_t bytes_transferred);
0082 };
0083
0084
0085 typedef descriptor_state* per_descriptor_data;
0086
0087
0088 BOOST_ASIO_DECL epoll_reactor(boost::asio::execution_context& ctx);
0089
0090
0091 BOOST_ASIO_DECL ~epoll_reactor();
0092
0093
0094 BOOST_ASIO_DECL void shutdown();
0095
0096
0097 BOOST_ASIO_DECL void notify_fork(
0098 boost::asio::execution_context::fork_event fork_ev);
0099
0100
0101 BOOST_ASIO_DECL void init_task();
0102
0103
0104
0105 BOOST_ASIO_DECL int register_descriptor(socket_type descriptor,
0106 per_descriptor_data& descriptor_data);
0107
0108
0109
0110 BOOST_ASIO_DECL int register_internal_descriptor(
0111 int op_type, socket_type descriptor,
0112 per_descriptor_data& descriptor_data, reactor_op* op);
0113
0114
0115 BOOST_ASIO_DECL void move_descriptor(socket_type descriptor,
0116 per_descriptor_data& target_descriptor_data,
0117 per_descriptor_data& source_descriptor_data);
0118
0119
0120 void post_immediate_completion(operation* op, bool is_continuation) const;
0121
0122
0123 BOOST_ASIO_DECL static void call_post_immediate_completion(
0124 operation* op, bool is_continuation, const void* self);
0125
0126
0127
0128 BOOST_ASIO_DECL void start_op(int op_type, socket_type descriptor,
0129 per_descriptor_data& descriptor_data, reactor_op* op,
0130 bool is_continuation, bool allow_speculative,
0131 void (*on_immediate)(operation*, bool, const void*),
0132 const void* immediate_arg);
0133
0134
0135
0136 void start_op(int op_type, socket_type descriptor,
0137 per_descriptor_data& descriptor_data, reactor_op* op,
0138 bool is_continuation, bool allow_speculative)
0139 {
0140 start_op(op_type, descriptor, descriptor_data,
0141 op, is_continuation, allow_speculative,
0142 &epoll_reactor::call_post_immediate_completion, this);
0143 }
0144
0145
0146
0147
0148 BOOST_ASIO_DECL void cancel_ops(socket_type descriptor,
0149 per_descriptor_data& descriptor_data);
0150
0151
0152
0153
0154 BOOST_ASIO_DECL void cancel_ops_by_key(socket_type descriptor,
0155 per_descriptor_data& descriptor_data,
0156 int op_type, void* cancellation_key);
0157
0158
0159
0160
0161 BOOST_ASIO_DECL void deregister_descriptor(socket_type descriptor,
0162 per_descriptor_data& descriptor_data, bool closing);
0163
0164
0165
0166
0167 BOOST_ASIO_DECL void deregister_internal_descriptor(
0168 socket_type descriptor, per_descriptor_data& descriptor_data);
0169
0170
0171
0172 BOOST_ASIO_DECL void cleanup_descriptor_data(
0173 per_descriptor_data& descriptor_data);
0174
0175
0176 template <typename Time_Traits>
0177 void add_timer_queue(timer_queue<Time_Traits>& timer_queue);
0178
0179
0180 template <typename Time_Traits>
0181 void remove_timer_queue(timer_queue<Time_Traits>& timer_queue);
0182
0183
0184
0185 template <typename Time_Traits>
0186 void schedule_timer(timer_queue<Time_Traits>& queue,
0187 const typename Time_Traits::time_type& time,
0188 typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
0189
0190
0191
0192 template <typename Time_Traits>
0193 std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
0194 typename timer_queue<Time_Traits>::per_timer_data& timer,
0195 std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
0196
0197
0198 template <typename Time_Traits>
0199 void cancel_timer_by_key(timer_queue<Time_Traits>& queue,
0200 typename timer_queue<Time_Traits>::per_timer_data* timer,
0201 void* cancellation_key);
0202
0203
0204 template <typename Time_Traits>
0205 void move_timer(timer_queue<Time_Traits>& queue,
0206 typename timer_queue<Time_Traits>::per_timer_data& target,
0207 typename timer_queue<Time_Traits>::per_timer_data& source);
0208
0209
0210 BOOST_ASIO_DECL void run(long usec, op_queue<operation>& ops);
0211
0212
0213 BOOST_ASIO_DECL void interrupt();
0214
0215 private:
0216
0217 enum { epoll_size = 20000 };
0218
0219
0220
0221 BOOST_ASIO_DECL static int do_epoll_create();
0222
0223
0224 BOOST_ASIO_DECL static int do_timerfd_create();
0225
0226
0227 BOOST_ASIO_DECL descriptor_state* allocate_descriptor_state();
0228
0229
0230 BOOST_ASIO_DECL void free_descriptor_state(descriptor_state* s);
0231
0232
0233 BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
0234
0235
0236 BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
0237
0238
0239 BOOST_ASIO_DECL void update_timeout();
0240
0241
0242
0243
0244 BOOST_ASIO_DECL int get_timeout(int msec);
0245
0246 #if defined(BOOST_ASIO_HAS_TIMERFD)
0247
0248
0249 BOOST_ASIO_DECL int get_timeout(itimerspec& ts);
0250 #endif
0251
0252
0253 scheduler& scheduler_;
0254
0255
0256 mutex mutex_;
0257
0258
0259 select_interrupter interrupter_;
0260
0261
0262 int epoll_fd_;
0263
0264
0265 int timer_fd_;
0266
0267
0268 timer_queue_set timer_queues_;
0269
0270
0271 bool shutdown_;
0272
0273
0274 mutex registered_descriptors_mutex_;
0275
0276
0277 object_pool<descriptor_state> registered_descriptors_;
0278
0279
0280 struct perform_io_cleanup_on_block_exit;
0281 friend struct perform_io_cleanup_on_block_exit;
0282 };
0283
0284 }
0285 }
0286 }
0287
0288 #include <boost/asio/detail/pop_options.hpp>
0289
0290 #include <boost/asio/detail/impl/epoll_reactor.hpp>
0291 #if defined(BOOST_ASIO_HEADER_ONLY)
0292 # include <boost/asio/detail/impl/epoll_reactor.ipp>
0293 #endif
0294
0295 #endif
0296
0297 #endif