scheduler.hpp 7.4 KB

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