/Src/Dependencies/Boost/boost/asio/detail/win_iocp_io_service.hpp

http://hadesmem.googlecode.com/ · C++ Header · 279 lines · 139 code · 63 blank · 77 comment · 6 complexity · d2b2d6bb89218ca6e807f6001d75780e MD5 · raw file

  1. //
  2. // detail/win_iocp_io_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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. #ifndef BOOST_ASIO_DETAIL_WIN_IOCP_IO_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_WIN_IOCP_IO_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_IOCP)
  17. #include <boost/limits.hpp>
  18. #include <boost/asio/io_service.hpp>
  19. #include <boost/asio/detail/mutex.hpp>
  20. #include <boost/asio/detail/op_queue.hpp>
  21. #include <boost/asio/detail/scoped_ptr.hpp>
  22. #include <boost/asio/detail/socket_types.hpp>
  23. #include <boost/asio/detail/timer_op.hpp>
  24. #include <boost/asio/detail/timer_queue_base.hpp>
  25. #include <boost/asio/detail/timer_queue_fwd.hpp>
  26. #include <boost/asio/detail/timer_queue_set.hpp>
  27. #include <boost/asio/detail/win_iocp_io_service_fwd.hpp>
  28. #include <boost/asio/detail/win_iocp_operation.hpp>
  29. #include <boost/asio/detail/thread.hpp>
  30. #include <boost/asio/detail/push_options.hpp>
  31. namespace boost {
  32. namespace asio {
  33. namespace detail {
  34. class timer_op;
  35. class win_iocp_io_service
  36. : public boost::asio::detail::service_base<win_iocp_io_service>
  37. {
  38. public:
  39. // Constructor.
  40. BOOST_ASIO_DECL win_iocp_io_service(boost::asio::io_service& io_service);
  41. BOOST_ASIO_DECL void init(size_t concurrency_hint);
  42. // Destroy all user-defined handler objects owned by the service.
  43. BOOST_ASIO_DECL void shutdown_service();
  44. // Initialise the task. Nothing to do here.
  45. void init_task()
  46. {
  47. }
  48. // Register a handle with the IO completion port.
  49. BOOST_ASIO_DECL boost::system::error_code register_handle(
  50. HANDLE handle, boost::system::error_code& ec);
  51. // Run the event loop until stopped or no more work.
  52. BOOST_ASIO_DECL size_t run(boost::system::error_code& ec);
  53. // Run until stopped or one operation is performed.
  54. BOOST_ASIO_DECL size_t run_one(boost::system::error_code& ec);
  55. // Poll for operations without blocking.
  56. BOOST_ASIO_DECL size_t poll(boost::system::error_code& ec);
  57. // Poll for one operation without blocking.
  58. BOOST_ASIO_DECL size_t poll_one(boost::system::error_code& ec);
  59. // Stop the event processing loop.
  60. BOOST_ASIO_DECL void stop();
  61. // Determine whether the io_service is stopped.
  62. bool stopped() const
  63. {
  64. return ::InterlockedExchangeAdd(&stopped_, 0) != 0;
  65. }
  66. // Reset in preparation for a subsequent run invocation.
  67. void reset()
  68. {
  69. ::InterlockedExchange(&stopped_, 0);
  70. }
  71. // Notify that some work has started.
  72. void work_started()
  73. {
  74. ::InterlockedIncrement(&outstanding_work_);
  75. }
  76. // Notify that some work has finished.
  77. void work_finished()
  78. {
  79. if (::InterlockedDecrement(&outstanding_work_) == 0)
  80. stop();
  81. }
  82. // Request invocation of the given handler.
  83. template <typename Handler>
  84. void dispatch(Handler handler);
  85. // Request invocation of the given handler and return immediately.
  86. template <typename Handler>
  87. void post(Handler handler);
  88. // Request invocation of the given operation and return immediately. Assumes
  89. // that work_started() has not yet been called for the operation.
  90. void post_immediate_completion(win_iocp_operation* op)
  91. {
  92. work_started();
  93. post_deferred_completion(op);
  94. }
  95. // Request invocation of the given operation and return immediately. Assumes
  96. // that work_started() was previously called for the operation.
  97. BOOST_ASIO_DECL void post_deferred_completion(win_iocp_operation* op);
  98. // Request invocation of the given operation and return immediately. Assumes
  99. // that work_started() was previously called for the operations.
  100. BOOST_ASIO_DECL void post_deferred_completions(
  101. op_queue<win_iocp_operation>& ops);
  102. // Process unfinished operations as part of a shutdown_service operation.
  103. // Assumes that work_started() was previously called for the operations.
  104. BOOST_ASIO_DECL void abandon_operations(op_queue<operation>& ops);
  105. // Called after starting an overlapped I/O operation that did not complete
  106. // immediately. The caller must have already called work_started() prior to
  107. // starting the operation.
  108. BOOST_ASIO_DECL void on_pending(win_iocp_operation* op);
  109. // Called after starting an overlapped I/O operation that completed
  110. // immediately. The caller must have already called work_started() prior to
  111. // starting the operation.
  112. BOOST_ASIO_DECL void on_completion(win_iocp_operation* op,
  113. DWORD last_error = 0, DWORD bytes_transferred = 0);
  114. // Called after starting an overlapped I/O operation that completed
  115. // immediately. The caller must have already called work_started() prior to
  116. // starting the operation.
  117. BOOST_ASIO_DECL void on_completion(win_iocp_operation* op,
  118. const boost::system::error_code& ec, DWORD bytes_transferred = 0);
  119. // Add a new timer queue to the service.
  120. template <typename Time_Traits>
  121. void add_timer_queue(timer_queue<Time_Traits>& timer_queue);
  122. // Remove a timer queue from the service.
  123. template <typename Time_Traits>
  124. void remove_timer_queue(timer_queue<Time_Traits>& timer_queue);
  125. // Schedule a new operation in the given timer queue to expire at the
  126. // specified absolute time.
  127. template <typename Time_Traits>
  128. void schedule_timer(timer_queue<Time_Traits>& queue,
  129. const typename Time_Traits::time_type& time,
  130. typename timer_queue<Time_Traits>::per_timer_data& timer, timer_op* op);
  131. // Cancel the timer associated with the given token. Returns the number of
  132. // handlers that have been posted or dispatched.
  133. template <typename Time_Traits>
  134. std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
  135. typename timer_queue<Time_Traits>::per_timer_data& timer,
  136. std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
  137. private:
  138. #if defined(WINVER) && (WINVER < 0x0500)
  139. typedef DWORD dword_ptr_t;
  140. typedef ULONG ulong_ptr_t;
  141. #else // defined(WINVER) && (WINVER < 0x0500)
  142. typedef DWORD_PTR dword_ptr_t;
  143. typedef ULONG_PTR ulong_ptr_t;
  144. #endif // defined(WINVER) && (WINVER < 0x0500)
  145. // Dequeues at most one operation from the I/O completion port, and then
  146. // executes it. Returns the number of operations that were dequeued (i.e.
  147. // either 0 or 1).
  148. BOOST_ASIO_DECL size_t do_one(bool block, boost::system::error_code& ec);
  149. // Helper function to add a new timer queue.
  150. BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  151. // Helper function to remove a timer queue.
  152. BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  153. // Called to recalculate and update the timeout.
  154. BOOST_ASIO_DECL void update_timeout();
  155. // Helper class to call work_finished() on block exit.
  156. struct work_finished_on_block_exit;
  157. // Helper class for managing a HANDLE.
  158. struct auto_handle
  159. {
  160. HANDLE handle;
  161. auto_handle() : handle(0) {}
  162. ~auto_handle() { if (handle) ::CloseHandle(handle); }
  163. };
  164. // The IO completion port used for queueing operations.
  165. auto_handle iocp_;
  166. // The count of unfinished work.
  167. long outstanding_work_;
  168. // Flag to indicate whether the event loop has been stopped.
  169. mutable long stopped_;
  170. // Flag to indicate whether the service has been shut down.
  171. long shutdown_;
  172. enum
  173. {
  174. // Timeout to use with GetQueuedCompletionStatus. Some versions of windows
  175. // have a "bug" where a call to GetQueuedCompletionStatus can appear stuck
  176. // even though there are events waiting on the queue. Using a timeout helps
  177. // to work around the issue.
  178. gqcs_timeout = 500,
  179. // Maximum waitable timer timeout, in milliseconds.
  180. max_timeout_msec = 5 * 60 * 1000,
  181. // Maximum waitable timer timeout, in microseconds.
  182. max_timeout_usec = max_timeout_msec * 1000,
  183. // Completion key value used to wake up a thread to dispatch timers or
  184. // completed operations.
  185. wake_for_dispatch = 1,
  186. // Completion key value to indicate that an operation has posted with the
  187. // original last_error and bytes_transferred values stored in the fields of
  188. // the OVERLAPPED structure.
  189. overlapped_contains_result = 2
  190. };
  191. // Function object for processing timeouts in a background thread.
  192. struct timer_thread_function;
  193. friend struct timer_thread_function;
  194. // Background thread used for processing timeouts.
  195. scoped_ptr<thread> timer_thread_;
  196. // A waitable timer object used for waiting for timeouts.
  197. auto_handle waitable_timer_;
  198. // Non-zero if timers or completed operations need to be dispatched.
  199. long dispatch_required_;
  200. // Mutex for protecting access to the timer queues and completed operations.
  201. mutex dispatch_mutex_;
  202. // The timer queues.
  203. timer_queue_set timer_queues_;
  204. // The operations that are ready to dispatch.
  205. op_queue<win_iocp_operation> completed_ops_;
  206. };
  207. } // namespace detail
  208. } // namespace asio
  209. } // namespace boost
  210. #include <boost/asio/detail/pop_options.hpp>
  211. #include <boost/asio/detail/impl/win_iocp_io_service.hpp>
  212. #if defined(BOOST_ASIO_HEADER_ONLY)
  213. # include <boost/asio/detail/impl/win_iocp_io_service.ipp>
  214. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  215. #endif // defined(BOOST_ASIO_HAS_IOCP)
  216. #endif // BOOST_ASIO_DETAIL_WIN_IOCP_IO_SERVICE_HPP