scheduler.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // detail/scheduler.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. bool can_dispatch()
  80. {
  81. return thread_call_stack::contains(this) != 0;
  82. }
  83. // Request invocation of the given operation and return immediately. Assumes
  84. // that work_started() has not yet been called for the operation.
  85. BOOST_ASIO_DECL void post_immediate_completion(
  86. operation* op, bool is_continuation);
  87. // Request invocation of the given operation and return immediately. Assumes
  88. // that work_started() was previously called for the operation.
  89. BOOST_ASIO_DECL void post_deferred_completion(operation* op);
  90. // Request invocation of the given operations and return immediately. Assumes
  91. // that work_started() was previously called for each operation.
  92. BOOST_ASIO_DECL void post_deferred_completions(op_queue<operation>& ops);
  93. // Enqueue the given operation following a failed attempt to dispatch the
  94. // operation for immediate invocation.
  95. BOOST_ASIO_DECL void do_dispatch(operation* op);
  96. // Process unfinished operations as part of a shutdownoperation. Assumes that
  97. // work_started() was previously called for the operations.
  98. BOOST_ASIO_DECL void abandon_operations(op_queue<operation>& ops);
  99. // Get the concurrency hint that was used to initialise the scheduler.
  100. int concurrency_hint() const
  101. {
  102. return concurrency_hint_;
  103. }
  104. private:
  105. // The mutex type used by this scheduler.
  106. typedef conditionally_enabled_mutex mutex;
  107. // The event type used by this scheduler.
  108. typedef conditionally_enabled_event event;
  109. // Structure containing thread-specific data.
  110. typedef scheduler_thread_info thread_info;
  111. // Run at most one operation. May block.
  112. BOOST_ASIO_DECL std::size_t do_run_one(mutex::scoped_lock& lock,
  113. thread_info& this_thread, const boost::system::error_code& ec);
  114. // Run at most one operation with a timeout. May block.
  115. BOOST_ASIO_DECL std::size_t do_wait_one(mutex::scoped_lock& lock,
  116. thread_info& this_thread, long usec, const boost::system::error_code& ec);
  117. // Poll for at most one operation.
  118. BOOST_ASIO_DECL std::size_t do_poll_one(mutex::scoped_lock& lock,
  119. thread_info& this_thread, const boost::system::error_code& ec);
  120. // Stop the task and all idle threads.
  121. BOOST_ASIO_DECL void stop_all_threads(mutex::scoped_lock& lock);
  122. // Wake a single idle thread, or the task, and always unlock the mutex.
  123. BOOST_ASIO_DECL void wake_one_thread_and_unlock(
  124. mutex::scoped_lock& lock);
  125. // Helper class to run the scheduler in its own thread.
  126. class thread_function;
  127. friend class thread_function;
  128. // Helper class to perform task-related operations on block exit.
  129. struct task_cleanup;
  130. friend struct task_cleanup;
  131. // Helper class to call work-related operations on block exit.
  132. struct work_cleanup;
  133. friend struct work_cleanup;
  134. // Whether to optimise for single-threaded use cases.
  135. const bool one_thread_;
  136. // Mutex to protect access to internal data.
  137. mutable mutex mutex_;
  138. // Event to wake up blocked threads.
  139. event wakeup_event_;
  140. // The task to be run by this service.
  141. reactor* task_;
  142. // Operation object to represent the position of the task in the queue.
  143. struct task_operation : operation
  144. {
  145. task_operation() : operation(0) {}
  146. } task_operation_;
  147. // Whether the task has been interrupted.
  148. bool task_interrupted_;
  149. // The count of unfinished work.
  150. atomic_count outstanding_work_;
  151. // The queue of handlers that are ready to be delivered.
  152. op_queue<operation> op_queue_;
  153. // Flag to indicate that the dispatcher has been stopped.
  154. bool stopped_;
  155. // Flag to indicate that the dispatcher has been shut down.
  156. bool shutdown_;
  157. // The concurrency hint used to initialise the scheduler.
  158. const int concurrency_hint_;
  159. // The thread that is running the scheduler.
  160. boost::asio::detail::thread* thread_;
  161. };
  162. } // namespace detail
  163. } // namespace asio
  164. } // namespace boost
  165. #include <boost/asio/detail/pop_options.hpp>
  166. #if defined(BOOST_ASIO_HEADER_ONLY)
  167. # include <boost/asio/detail/impl/scheduler.ipp>
  168. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  169. #endif // BOOST_ASIO_DETAIL_SCHEDULER_HPP