scheduler.hpp 7.8 KB

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