Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
6.7KB

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