scheduler.hpp 6.7 KB

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