Chris@16: // Chris@16: // detail/epoll_reactor.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_DETAIL_EPOLL_REACTOR_HPP Chris@16: #define BOOST_ASIO_DETAIL_EPOLL_REACTOR_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_EPOLL) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace detail { Chris@16: Chris@16: class epoll_reactor Chris@16: : public boost::asio::detail::service_base Chris@16: { Chris@16: public: Chris@16: enum op_types { read_op = 0, write_op = 1, Chris@16: connect_op = 1, except_op = 2, max_ops = 3 }; Chris@16: Chris@16: // Per-descriptor queues. Chris@16: class descriptor_state : operation Chris@16: { Chris@16: friend class epoll_reactor; Chris@16: friend class object_pool_access; Chris@16: Chris@16: descriptor_state* next_; Chris@16: descriptor_state* prev_; Chris@16: Chris@16: mutex mutex_; Chris@16: epoll_reactor* reactor_; Chris@16: int descriptor_; Chris@16: uint32_t registered_events_; Chris@16: op_queue op_queue_[max_ops]; Chris@16: bool shutdown_; Chris@16: Chris@16: BOOST_ASIO_DECL descriptor_state(); Chris@16: void set_ready_events(uint32_t events) { task_result_ = events; } Chris@16: BOOST_ASIO_DECL operation* perform_io(uint32_t events); Chris@16: BOOST_ASIO_DECL static void do_complete( Chris@16: io_service_impl* owner, operation* base, Chris@16: const boost::system::error_code& ec, std::size_t bytes_transferred); Chris@16: }; Chris@16: Chris@16: // Per-descriptor data. Chris@16: typedef descriptor_state* per_descriptor_data; Chris@16: Chris@16: // Constructor. Chris@16: BOOST_ASIO_DECL epoll_reactor(boost::asio::io_service& io_service); Chris@16: Chris@16: // Destructor. Chris@16: BOOST_ASIO_DECL ~epoll_reactor(); Chris@16: Chris@16: // Destroy all user-defined handler objects owned by the service. Chris@16: BOOST_ASIO_DECL void shutdown_service(); Chris@16: Chris@16: // Recreate internal descriptors following a fork. Chris@16: BOOST_ASIO_DECL void fork_service( Chris@16: boost::asio::io_service::fork_event fork_ev); Chris@16: Chris@16: // Initialise the task. Chris@16: BOOST_ASIO_DECL void init_task(); Chris@16: Chris@16: // Register a socket with the reactor. Returns 0 on success, system error Chris@16: // code on failure. Chris@16: BOOST_ASIO_DECL int register_descriptor(socket_type descriptor, Chris@16: per_descriptor_data& descriptor_data); Chris@16: Chris@16: // Register a descriptor with an associated single operation. Returns 0 on Chris@16: // success, system error code on failure. Chris@16: BOOST_ASIO_DECL int register_internal_descriptor( Chris@16: int op_type, socket_type descriptor, Chris@16: per_descriptor_data& descriptor_data, reactor_op* op); Chris@16: Chris@16: // Move descriptor registration from one descriptor_data object to another. Chris@16: BOOST_ASIO_DECL void move_descriptor(socket_type descriptor, Chris@16: per_descriptor_data& target_descriptor_data, Chris@16: per_descriptor_data& source_descriptor_data); Chris@16: Chris@16: // Post a reactor operation for immediate completion. Chris@16: void post_immediate_completion(reactor_op* op, bool is_continuation) Chris@16: { Chris@16: io_service_.post_immediate_completion(op, is_continuation); Chris@16: } Chris@16: Chris@16: // Start a new operation. The reactor operation will be performed when the Chris@16: // given descriptor is flagged as ready, or an error has occurred. Chris@16: BOOST_ASIO_DECL void start_op(int op_type, socket_type descriptor, Chris@16: per_descriptor_data& descriptor_data, reactor_op* op, Chris@16: bool is_continuation, bool allow_speculative); Chris@16: Chris@16: // Cancel all operations associated with the given descriptor. The Chris@16: // handlers associated with the descriptor will be invoked with the Chris@16: // operation_aborted error. Chris@16: BOOST_ASIO_DECL void cancel_ops(socket_type descriptor, Chris@16: per_descriptor_data& descriptor_data); Chris@16: Chris@16: // Cancel any operations that are running against the descriptor and remove Chris@16: // its registration from the reactor. Chris@16: BOOST_ASIO_DECL void deregister_descriptor(socket_type descriptor, Chris@16: per_descriptor_data& descriptor_data, bool closing); Chris@16: Chris@16: // Remote the descriptor's registration from the reactor. Chris@16: BOOST_ASIO_DECL void deregister_internal_descriptor( Chris@16: socket_type descriptor, per_descriptor_data& descriptor_data); Chris@16: Chris@16: // Add a new timer queue to the reactor. Chris@16: template Chris@16: void add_timer_queue(timer_queue& timer_queue); Chris@16: Chris@16: // Remove a timer queue from the reactor. Chris@16: template Chris@16: void remove_timer_queue(timer_queue& timer_queue); Chris@16: Chris@16: // Schedule a new operation in the given timer queue to expire at the Chris@16: // specified absolute time. Chris@16: template Chris@16: void schedule_timer(timer_queue& queue, Chris@16: const typename Time_Traits::time_type& time, Chris@16: typename timer_queue::per_timer_data& timer, wait_op* op); Chris@16: Chris@16: // Cancel the timer operations associated with the given token. Returns the Chris@16: // number of operations that have been posted or dispatched. Chris@16: template Chris@16: std::size_t cancel_timer(timer_queue& queue, Chris@16: typename timer_queue::per_timer_data& timer, Chris@16: std::size_t max_cancelled = (std::numeric_limits::max)()); Chris@16: Chris@16: // Run epoll once until interrupted or events are ready to be dispatched. Chris@16: BOOST_ASIO_DECL void run(bool block, op_queue& ops); Chris@16: Chris@16: // Interrupt the select loop. Chris@16: BOOST_ASIO_DECL void interrupt(); Chris@16: Chris@16: private: Chris@16: // The hint to pass to epoll_create to size its data structures. Chris@16: enum { epoll_size = 20000 }; Chris@16: Chris@16: // Create the epoll file descriptor. Throws an exception if the descriptor Chris@16: // cannot be created. Chris@16: BOOST_ASIO_DECL static int do_epoll_create(); Chris@16: Chris@16: // Create the timerfd file descriptor. Does not throw. Chris@16: BOOST_ASIO_DECL static int do_timerfd_create(); Chris@16: Chris@16: // Allocate a new descriptor state object. Chris@16: BOOST_ASIO_DECL descriptor_state* allocate_descriptor_state(); Chris@16: Chris@16: // Free an existing descriptor state object. Chris@16: BOOST_ASIO_DECL void free_descriptor_state(descriptor_state* s); Chris@16: Chris@16: // Helper function to add a new timer queue. Chris@16: BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue); Chris@16: Chris@16: // Helper function to remove a timer queue. Chris@16: BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue); Chris@16: Chris@16: // Called to recalculate and update the timeout. Chris@16: BOOST_ASIO_DECL void update_timeout(); Chris@16: Chris@16: // Get the timeout value for the epoll_wait call. The timeout value is Chris@16: // returned as a number of milliseconds. A return value of -1 indicates Chris@16: // that epoll_wait should block indefinitely. Chris@16: BOOST_ASIO_DECL int get_timeout(); Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_TIMERFD) Chris@16: // Get the timeout value for the timer descriptor. The return value is the Chris@16: // flag argument to be used when calling timerfd_settime. Chris@16: BOOST_ASIO_DECL int get_timeout(itimerspec& ts); Chris@16: #endif // defined(BOOST_ASIO_HAS_TIMERFD) Chris@16: Chris@16: // The io_service implementation used to post completions. Chris@16: io_service_impl& io_service_; Chris@16: Chris@16: // Mutex to protect access to internal data. Chris@16: mutex mutex_; Chris@16: Chris@16: // The interrupter is used to break a blocking epoll_wait call. Chris@16: select_interrupter interrupter_; Chris@16: Chris@16: // The epoll file descriptor. Chris@16: int epoll_fd_; Chris@16: Chris@16: // The timer file descriptor. Chris@16: int timer_fd_; Chris@16: Chris@16: // The timer queues. Chris@16: timer_queue_set timer_queues_; Chris@16: Chris@16: // Whether the service has been shut down. Chris@16: bool shutdown_; Chris@16: Chris@16: // Mutex to protect access to the registered descriptors. Chris@16: mutex registered_descriptors_mutex_; Chris@16: Chris@16: // Keep track of all registered descriptors. Chris@16: object_pool registered_descriptors_; Chris@16: Chris@16: // Helper class to do post-perform_io cleanup. Chris@16: struct perform_io_cleanup_on_block_exit; Chris@16: friend struct perform_io_cleanup_on_block_exit; Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #if defined(BOOST_ASIO_HEADER_ONLY) Chris@16: # include Chris@16: #endif // defined(BOOST_ASIO_HEADER_ONLY) Chris@16: Chris@16: #endif // defined(BOOST_ASIO_HAS_EPOLL) Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_EPOLL_REACTOR_HPP