task_io_service.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // detail/task_io_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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_TASK_IO_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_TASK_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/system/error_code.hpp>
  18. #include <boost/asio/io_service.hpp>
  19. #include <boost/asio/detail/atomic_count.hpp>
  20. #include <boost/asio/detail/call_stack.hpp>
  21. #include <boost/asio/detail/mutex.hpp>
  22. #include <boost/asio/detail/op_queue.hpp>
  23. #include <boost/asio/detail/reactor_fwd.hpp>
  24. #include <boost/asio/detail/task_io_service_operation.hpp>
  25. #include <boost/asio/detail/push_options.hpp>
  26. namespace boost {
  27. namespace asio {
  28. namespace detail {
  29. struct task_io_service_thread_info;
  30. class task_io_service
  31. : public boost::asio::detail::service_base<task_io_service>
  32. {
  33. public:
  34. typedef task_io_service_operation operation;
  35. // Constructor. Specifies the number of concurrent threads that are likely to
  36. // run the io_service. If set to 1 certain optimisation are performed.
  37. BOOST_ASIO_DECL task_io_service(boost::asio::io_service& io_service,
  38. std::size_t concurrency_hint = 0);
  39. // Destroy all user-defined handler objects owned by the service.
  40. BOOST_ASIO_DECL void shutdown_service();
  41. // Initialise the task, if required.
  42. BOOST_ASIO_DECL void init_task();
  43. // Run the event loop until interrupted or no more work.
  44. BOOST_ASIO_DECL std::size_t run(boost::system::error_code& ec);
  45. // Run until interrupted or one operation is performed.
  46. BOOST_ASIO_DECL std::size_t run_one(boost::system::error_code& ec);
  47. // Poll for operations without blocking.
  48. BOOST_ASIO_DECL std::size_t poll(boost::system::error_code& ec);
  49. // Poll for one operation without blocking.
  50. BOOST_ASIO_DECL std::size_t poll_one(boost::system::error_code& ec);
  51. // Interrupt the event processing loop.
  52. BOOST_ASIO_DECL void stop();
  53. // Determine whether the io_service is stopped.
  54. BOOST_ASIO_DECL bool stopped() const;
  55. // Reset in preparation for a subsequent run invocation.
  56. BOOST_ASIO_DECL void reset();
  57. // Notify that some work has started.
  58. void work_started()
  59. {
  60. ++outstanding_work_;
  61. }
  62. // Notify that some work has finished.
  63. void work_finished()
  64. {
  65. if (--outstanding_work_ == 0)
  66. stop();
  67. }
  68. // Return whether a handler can be dispatched immediately.
  69. bool can_dispatch()
  70. {
  71. return thread_call_stack::contains(this) != 0;
  72. }
  73. // Request invocation of the given handler.
  74. template <typename Handler>
  75. void dispatch(Handler& handler);
  76. // Request invocation of the given handler and return immediately.
  77. template <typename Handler>
  78. void post(Handler& handler);
  79. // Request invocation of the given operation and return immediately. Assumes
  80. // that work_started() has not yet been called for the operation.
  81. BOOST_ASIO_DECL void post_immediate_completion(
  82. operation* op, bool is_continuation);
  83. // Request invocation of the given operation and return immediately. Assumes
  84. // that work_started() was previously called for the operation.
  85. BOOST_ASIO_DECL void post_deferred_completion(operation* op);
  86. // Request invocation of the given operations and return immediately. Assumes
  87. // that work_started() was previously called for each operation.
  88. BOOST_ASIO_DECL void post_deferred_completions(op_queue<operation>& ops);
  89. // Process unfinished operations as part of a shutdown_service operation.
  90. // Assumes that work_started() was previously called for the operations.
  91. BOOST_ASIO_DECL void abandon_operations(op_queue<operation>& ops);
  92. private:
  93. // Structure containing information about an idle thread.
  94. typedef task_io_service_thread_info thread_info;
  95. // Enqueue the given operation following a failed attempt to dispatch the
  96. // operation for immediate invocation.
  97. BOOST_ASIO_DECL void do_dispatch(operation* op);
  98. // Run at most one operation. May block.
  99. BOOST_ASIO_DECL std::size_t do_run_one(mutex::scoped_lock& lock,
  100. thread_info& this_thread, const boost::system::error_code& ec);
  101. // Poll for at most one operation.
  102. BOOST_ASIO_DECL std::size_t do_poll_one(mutex::scoped_lock& lock,
  103. thread_info& this_thread, const boost::system::error_code& ec);
  104. // Stop the task and all idle threads.
  105. BOOST_ASIO_DECL void stop_all_threads(mutex::scoped_lock& lock);
  106. // Wakes a single idle thread and unlocks the mutex. Returns true if an idle
  107. // thread was found. If there is no idle thread, returns false and leaves the
  108. // mutex locked.
  109. BOOST_ASIO_DECL bool wake_one_idle_thread_and_unlock(
  110. mutex::scoped_lock& lock);
  111. // Wake a single idle thread, or the task, and always unlock the mutex.
  112. BOOST_ASIO_DECL void wake_one_thread_and_unlock(
  113. mutex::scoped_lock& lock);
  114. // Helper class to perform task-related operations on block exit.
  115. struct task_cleanup;
  116. friend struct task_cleanup;
  117. // Helper class to call work-related operations on block exit.
  118. struct work_cleanup;
  119. friend struct work_cleanup;
  120. // Whether to optimise for single-threaded use cases.
  121. const bool one_thread_;
  122. // Mutex to protect access to internal data.
  123. mutable mutex mutex_;
  124. // The task to be run by this service.
  125. reactor* task_;
  126. // Operation object to represent the position of the task in the queue.
  127. struct task_operation : operation
  128. {
  129. task_operation() : operation(0) {}
  130. } task_operation_;
  131. // Whether the task has been interrupted.
  132. bool task_interrupted_;
  133. // The count of unfinished work.
  134. atomic_count outstanding_work_;
  135. // The queue of handlers that are ready to be delivered.
  136. op_queue<operation> op_queue_;
  137. // Flag to indicate that the dispatcher has been stopped.
  138. bool stopped_;
  139. // Flag to indicate that the dispatcher has been shut down.
  140. bool shutdown_;
  141. // Per-thread call stack to track the state of each thread in the io_service.
  142. typedef call_stack<task_io_service, thread_info> thread_call_stack;
  143. // The threads that are currently idle.
  144. thread_info* first_idle_thread_;
  145. };
  146. } // namespace detail
  147. } // namespace asio
  148. } // namespace boost
  149. #include <boost/asio/detail/pop_options.hpp>
  150. #include <boost/asio/detail/impl/task_io_service.hpp>
  151. #if defined(BOOST_ASIO_HEADER_ONLY)
  152. # include <boost/asio/detail/impl/task_io_service.ipp>
  153. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  154. #endif // !defined(BOOST_ASIO_HAS_IOCP)
  155. #endif // BOOST_ASIO_DETAIL_TASK_IO_SERVICE_HPP