scheduler.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // detail/scheduler.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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_SCHEDULER_HPP
  11. #define BOOST_ASIO_DETAIL_SCHEDULER_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. #include <boost/system/error_code.hpp>
  17. #include <boost/asio/execution_context.hpp>
  18. #include <boost/asio/detail/atomic_count.hpp>
  19. #include <boost/asio/detail/conditionally_enabled_event.hpp>
  20. #include <boost/asio/detail/conditionally_enabled_mutex.hpp>
  21. #include <boost/asio/detail/op_queue.hpp>
  22. #include <boost/asio/detail/reactor_fwd.hpp>
  23. #include <boost/asio/detail/scheduler_operation.hpp>
  24. #include <boost/asio/detail/thread.hpp>
  25. #include <boost/asio/detail/thread_context.hpp>
  26. #include <boost/asio/detail/push_options.hpp>
  27. namespace boost {
  28. namespace asio {
  29. namespace detail {
  30. struct scheduler_thread_info;
  31. class scheduler
  32. : public execution_context_service_base<scheduler>,
  33. public thread_context
  34. {
  35. public:
  36. typedef scheduler_operation operation;
  37. // Constructor. Specifies the number of concurrent threads that are likely to
  38. // run the scheduler. If set to 1 certain optimisation are performed.
  39. BOOST_ASIO_DECL scheduler(boost::asio::execution_context& ctx,
  40. int concurrency_hint = 0, bool own_thread = true);
  41. // Destructor.
  42. BOOST_ASIO_DECL ~scheduler();
  43. // Destroy all user-defined handler objects owned by the service.
  44. BOOST_ASIO_DECL void shutdown();
  45. // Initialise the task, if required.
  46. BOOST_ASIO_DECL void init_task();
  47. // Run the event loop until interrupted or no more work.
  48. BOOST_ASIO_DECL std::size_t run(boost::system::error_code& ec);
  49. // Run until interrupted or one operation is performed.
  50. BOOST_ASIO_DECL std::size_t run_one(boost::system::error_code& ec);
  51. // Run until timeout, interrupted, or one operation is performed.
  52. BOOST_ASIO_DECL std::size_t wait_one(
  53. long usec, boost::system::error_code& ec);
  54. // Poll for operations without blocking.
  55. BOOST_ASIO_DECL std::size_t poll(boost::system::error_code& ec);
  56. // Poll for one operation without blocking.
  57. BOOST_ASIO_DECL std::size_t poll_one(boost::system::error_code& ec);
  58. // Interrupt the event processing loop.
  59. BOOST_ASIO_DECL void stop();
  60. // Determine whether the scheduler is stopped.
  61. BOOST_ASIO_DECL bool stopped() const;
  62. // Restart in preparation for a subsequent run invocation.
  63. BOOST_ASIO_DECL void restart();
  64. // Notify that some work has started.
  65. void work_started()
  66. {
  67. ++outstanding_work_;
  68. }
  69. // Used to compensate for a forthcoming work_finished call. Must be called
  70. // from within a scheduler-owned thread.
  71. BOOST_ASIO_DECL void compensating_work_started();
  72. // Notify that some work has finished.
  73. void work_finished()
  74. {
  75. if (--outstanding_work_ == 0)
  76. stop();
  77. }
  78. // Return whether a handler can be dispatched immediately.
  79. BOOST_ASIO_DECL bool can_dispatch();
  80. /// Capture the current exception so it can be rethrown from a run function.
  81. BOOST_ASIO_DECL void capture_current_exception();
  82. // Request invocation of the given operation and return immediately. Assumes
  83. // that work_started() has not yet been called for the operation.
  84. BOOST_ASIO_DECL void post_immediate_completion(
  85. operation* op, bool is_continuation);
  86. // Request invocation of the given operations and return immediately. Assumes
  87. // that work_started() has not yet been called for the operations.
  88. BOOST_ASIO_DECL void post_immediate_completions(std::size_t n,
  89. op_queue<operation>& ops, bool is_continuation);
  90. // Request invocation of the given operation and return immediately. Assumes
  91. // that work_started() was previously called for the operation.
  92. BOOST_ASIO_DECL void post_deferred_completion(operation* op);
  93. // Request invocation of the given operations and return immediately. Assumes
  94. // that work_started() was previously called for each operation.
  95. BOOST_ASIO_DECL void post_deferred_completions(op_queue<operation>& ops);
  96. // Enqueue the given operation following a failed attempt to dispatch the
  97. // operation for immediate invocation.
  98. BOOST_ASIO_DECL void do_dispatch(operation* op);
  99. // Process unfinished operations as part of a shutdownoperation. Assumes that
  100. // work_started() was previously called for the operations.
  101. BOOST_ASIO_DECL void abandon_operations(op_queue<operation>& ops);
  102. // Get the concurrency hint that was used to initialise the scheduler.
  103. int concurrency_hint() const
  104. {
  105. return concurrency_hint_;
  106. }
  107. private:
  108. // The mutex type used by this scheduler.
  109. typedef conditionally_enabled_mutex mutex;
  110. // The event type used by this scheduler.
  111. typedef conditionally_enabled_event event;
  112. // Structure containing thread-specific data.
  113. typedef scheduler_thread_info thread_info;
  114. // Run at most one operation. May block.
  115. BOOST_ASIO_DECL std::size_t do_run_one(mutex::scoped_lock& lock,
  116. thread_info& this_thread, const boost::system::error_code& ec);
  117. // Run at most one operation with a timeout. May block.
  118. BOOST_ASIO_DECL std::size_t do_wait_one(mutex::scoped_lock& lock,
  119. thread_info& this_thread, long usec, const boost::system::error_code& ec);
  120. // Poll for at most one operation.
  121. BOOST_ASIO_DECL std::size_t do_poll_one(mutex::scoped_lock& lock,
  122. thread_info& this_thread, const boost::system::error_code& ec);
  123. // Stop the task and all idle threads.
  124. BOOST_ASIO_DECL void stop_all_threads(mutex::scoped_lock& lock);
  125. // Wake a single idle thread, or the task, and always unlock the mutex.
  126. BOOST_ASIO_DECL void wake_one_thread_and_unlock(
  127. mutex::scoped_lock& lock);
  128. // Helper class to run the scheduler in its own thread.
  129. class thread_function;
  130. friend class thread_function;
  131. // Helper class to perform task-related operations on block exit.
  132. struct task_cleanup;
  133. friend struct task_cleanup;
  134. // Helper class to call work-related operations on block exit.
  135. struct work_cleanup;
  136. friend struct work_cleanup;
  137. // Whether to optimise for single-threaded use cases.
  138. const bool one_thread_;
  139. // Mutex to protect access to internal data.
  140. mutable mutex mutex_;
  141. // Event to wake up blocked threads.
  142. event wakeup_event_;
  143. // The task to be run by this service.
  144. reactor* task_;
  145. // Operation object to represent the position of the task in the queue.
  146. struct task_operation : operation
  147. {
  148. task_operation() : operation(0) {}
  149. } task_operation_;
  150. // Whether the task has been interrupted.
  151. bool task_interrupted_;
  152. // The count of unfinished work.
  153. atomic_count outstanding_work_;
  154. // The queue of handlers that are ready to be delivered.
  155. op_queue<operation> op_queue_;
  156. // Flag to indicate that the dispatcher has been stopped.
  157. bool stopped_;
  158. // Flag to indicate that the dispatcher has been shut down.
  159. bool shutdown_;
  160. // The concurrency hint used to initialise the scheduler.
  161. const int concurrency_hint_;
  162. // The thread that is running the scheduler.
  163. boost::asio::detail::thread* thread_;
  164. };
  165. } // namespace detail
  166. } // namespace asio
  167. } // namespace boost
  168. #include <boost/asio/detail/pop_options.hpp>
  169. #if defined(BOOST_ASIO_HEADER_ONLY)
  170. # include <boost/asio/detail/impl/scheduler.ipp>
  171. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  172. #endif // BOOST_ASIO_DETAIL_SCHEDULER_HPP