Chris@16: // Chris@16: // detail/win_iocp_io_service.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_WIN_IOCP_IO_SERVICE_HPP Chris@16: #define BOOST_ASIO_DETAIL_WIN_IOCP_IO_SERVICE_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_IOCP) 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: #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 wait_op; Chris@16: Chris@16: class win_iocp_io_service Chris@16: : public boost::asio::detail::service_base Chris@16: { Chris@16: public: Chris@16: Chris@16: // Constructor. Specifies a concurrency hint that is passed through to the Chris@16: // underlying I/O completion port. Chris@16: BOOST_ASIO_DECL win_iocp_io_service(boost::asio::io_service& io_service, Chris@16: size_t concurrency_hint = 0); 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: // Initialise the task. Nothing to do here. Chris@16: void init_task() Chris@16: { Chris@16: } Chris@16: Chris@16: // Register a handle with the IO completion port. Chris@16: BOOST_ASIO_DECL boost::system::error_code register_handle( Chris@16: HANDLE handle, boost::system::error_code& ec); Chris@16: Chris@16: // Run the event loop until stopped or no more work. Chris@16: BOOST_ASIO_DECL size_t run(boost::system::error_code& ec); Chris@16: Chris@16: // Run until stopped or one operation is performed. Chris@16: BOOST_ASIO_DECL size_t run_one(boost::system::error_code& ec); Chris@16: Chris@16: // Poll for operations without blocking. Chris@16: BOOST_ASIO_DECL size_t poll(boost::system::error_code& ec); Chris@16: Chris@16: // Poll for one operation without blocking. Chris@16: BOOST_ASIO_DECL size_t poll_one(boost::system::error_code& ec); Chris@16: Chris@16: // Stop the event processing loop. Chris@16: BOOST_ASIO_DECL void stop(); Chris@16: Chris@16: // Determine whether the io_service is stopped. Chris@16: bool stopped() const Chris@16: { Chris@16: return ::InterlockedExchangeAdd(&stopped_, 0) != 0; Chris@16: } Chris@16: Chris@16: // Reset in preparation for a subsequent run invocation. Chris@16: void reset() Chris@16: { Chris@16: ::InterlockedExchange(&stopped_, 0); Chris@16: } Chris@16: Chris@16: // Notify that some work has started. Chris@16: void work_started() Chris@16: { Chris@16: ::InterlockedIncrement(&outstanding_work_); Chris@16: } Chris@16: Chris@16: // Notify that some work has finished. Chris@16: void work_finished() Chris@16: { Chris@16: if (::InterlockedDecrement(&outstanding_work_) == 0) Chris@16: stop(); Chris@16: } Chris@16: Chris@16: // Return whether a handler can be dispatched immediately. Chris@16: bool can_dispatch() Chris@16: { Chris@16: return thread_call_stack::contains(this) != 0; Chris@16: } Chris@16: Chris@16: // Request invocation of the given handler. Chris@16: template Chris@16: void dispatch(Handler& handler); Chris@16: Chris@16: // Request invocation of the given handler and return immediately. Chris@16: template Chris@16: void post(Handler& handler); Chris@16: Chris@16: // Request invocation of the given operation and return immediately. Assumes Chris@16: // that work_started() has not yet been called for the operation. Chris@16: void post_immediate_completion(win_iocp_operation* op, bool) Chris@16: { Chris@16: work_started(); Chris@16: post_deferred_completion(op); Chris@16: } Chris@16: Chris@16: // Request invocation of the given operation and return immediately. Assumes Chris@16: // that work_started() was previously called for the operation. Chris@16: BOOST_ASIO_DECL void post_deferred_completion(win_iocp_operation* op); Chris@16: Chris@16: // Request invocation of the given operation and return immediately. Assumes Chris@16: // that work_started() was previously called for the operations. Chris@16: BOOST_ASIO_DECL void post_deferred_completions( Chris@16: op_queue& ops); Chris@16: Chris@16: // Request invocation of the given operation using the thread-private queue Chris@16: // and return immediately. Assumes that work_started() has not yet been Chris@16: // called for the operation. Chris@16: void post_private_immediate_completion(win_iocp_operation* op) Chris@16: { Chris@16: post_immediate_completion(op, false); Chris@16: } Chris@16: Chris@16: // Request invocation of the given operation using the thread-private queue Chris@16: // and return immediately. Assumes that work_started() was previously called Chris@16: // for the operation. Chris@16: void post_private_deferred_completion(win_iocp_operation* op) Chris@16: { Chris@16: post_deferred_completion(op); Chris@16: } Chris@16: Chris@16: // Process unfinished operations as part of a shutdown_service operation. Chris@16: // Assumes that work_started() was previously called for the operations. Chris@16: BOOST_ASIO_DECL void abandon_operations(op_queue& ops); Chris@16: Chris@16: // Called after starting an overlapped I/O operation that did not complete Chris@16: // immediately. The caller must have already called work_started() prior to Chris@16: // starting the operation. Chris@16: BOOST_ASIO_DECL void on_pending(win_iocp_operation* op); Chris@16: Chris@16: // Called after starting an overlapped I/O operation that completed Chris@16: // immediately. The caller must have already called work_started() prior to Chris@16: // starting the operation. Chris@16: BOOST_ASIO_DECL void on_completion(win_iocp_operation* op, Chris@16: DWORD last_error = 0, DWORD bytes_transferred = 0); Chris@16: Chris@16: // Called after starting an overlapped I/O operation that completed Chris@16: // immediately. The caller must have already called work_started() prior to Chris@16: // starting the operation. Chris@16: BOOST_ASIO_DECL void on_completion(win_iocp_operation* op, Chris@16: const boost::system::error_code& ec, DWORD bytes_transferred = 0); Chris@16: Chris@16: // Add a new timer queue to the service. Chris@16: template Chris@16: void add_timer_queue(timer_queue& timer_queue); Chris@16: Chris@16: // Remove a timer queue from the service. 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 associated with the given token. Returns the number of Chris@16: // handlers 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: private: Chris@16: #if defined(WINVER) && (WINVER < 0x0500) Chris@16: typedef DWORD dword_ptr_t; Chris@16: typedef ULONG ulong_ptr_t; Chris@16: #else // defined(WINVER) && (WINVER < 0x0500) Chris@16: typedef DWORD_PTR dword_ptr_t; Chris@16: typedef ULONG_PTR ulong_ptr_t; Chris@16: #endif // defined(WINVER) && (WINVER < 0x0500) Chris@16: Chris@16: // Dequeues at most one operation from the I/O completion port, and then Chris@16: // executes it. Returns the number of operations that were dequeued (i.e. Chris@16: // either 0 or 1). Chris@16: BOOST_ASIO_DECL size_t do_one(bool block, boost::system::error_code& ec); Chris@16: Chris@101: // Helper to calculate the GetQueuedCompletionStatus timeout. Chris@101: BOOST_ASIO_DECL static DWORD get_gqcs_timeout(); Chris@101: 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: // Helper class to call work_finished() on block exit. Chris@16: struct work_finished_on_block_exit; Chris@16: Chris@16: // Helper class for managing a HANDLE. Chris@16: struct auto_handle Chris@16: { Chris@16: HANDLE handle; Chris@16: auto_handle() : handle(0) {} Chris@16: ~auto_handle() { if (handle) ::CloseHandle(handle); } Chris@16: }; Chris@16: Chris@16: // The IO completion port used for queueing operations. Chris@16: auto_handle iocp_; Chris@16: Chris@16: // The count of unfinished work. Chris@16: long outstanding_work_; Chris@16: Chris@16: // Flag to indicate whether the event loop has been stopped. Chris@16: mutable long stopped_; Chris@16: Chris@16: // Flag to indicate whether there is an in-flight stop event. Every event Chris@16: // posted using PostQueuedCompletionStatus consumes non-paged pool, so to Chris@16: // avoid exhausting this resouce we limit the number of outstanding events. Chris@16: long stop_event_posted_; Chris@16: Chris@16: // Flag to indicate whether the service has been shut down. Chris@16: long shutdown_; Chris@16: Chris@16: enum Chris@16: { Chris@101: // Timeout to use with GetQueuedCompletionStatus on older versions of Chris@101: // Windows. Some versions of windows have a "bug" where a call to Chris@101: // GetQueuedCompletionStatus can appear stuck even though there are events Chris@101: // waiting on the queue. Using a timeout helps to work around the issue. Chris@101: default_gqcs_timeout = 500, Chris@16: Chris@16: // Maximum waitable timer timeout, in milliseconds. Chris@16: max_timeout_msec = 5 * 60 * 1000, Chris@16: Chris@16: // Maximum waitable timer timeout, in microseconds. Chris@16: max_timeout_usec = max_timeout_msec * 1000, Chris@16: Chris@16: // Completion key value used to wake up a thread to dispatch timers or Chris@16: // completed operations. Chris@16: wake_for_dispatch = 1, Chris@16: Chris@16: // Completion key value to indicate that an operation has posted with the Chris@16: // original last_error and bytes_transferred values stored in the fields of Chris@16: // the OVERLAPPED structure. Chris@16: overlapped_contains_result = 2 Chris@16: }; Chris@16: Chris@101: // Timeout to use with GetQueuedCompletionStatus. Chris@101: const DWORD gqcs_timeout_; Chris@101: Chris@16: // Function object for processing timeouts in a background thread. Chris@16: struct timer_thread_function; Chris@16: friend struct timer_thread_function; Chris@16: Chris@16: // Background thread used for processing timeouts. Chris@16: scoped_ptr timer_thread_; Chris@16: Chris@16: // A waitable timer object used for waiting for timeouts. Chris@16: auto_handle waitable_timer_; Chris@16: Chris@16: // Non-zero if timers or completed operations need to be dispatched. Chris@16: long dispatch_required_; Chris@16: Chris@16: // Mutex for protecting access to the timer queues and completed operations. Chris@16: mutex dispatch_mutex_; Chris@16: Chris@16: // The timer queues. Chris@16: timer_queue_set timer_queues_; Chris@16: Chris@16: // The operations that are ready to dispatch. Chris@16: op_queue completed_ops_; Chris@16: Chris@16: // Per-thread call stack to track the state of each thread in the io_service. Chris@16: typedef call_stack thread_call_stack; 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_IOCP) Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_WIN_IOCP_IO_SERVICE_HPP