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.

191 lines
5.6KB

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