task_io_service.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // detail/task_io_service.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_TASK_IO_SERVICE_HPP
  11. #define ASIO_DETAIL_TASK_IO_SERVICE_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. #if !defined(ASIO_HAS_IOCP)
  17. #include "asio/error_code.hpp"
  18. #include "asio/io_service.hpp"
  19. #include "asio/detail/atomic_count.hpp"
  20. #include "asio/detail/call_stack.hpp"
  21. #include "asio/detail/event.hpp"
  22. #include "asio/detail/mutex.hpp"
  23. #include "asio/detail/op_queue.hpp"
  24. #include "asio/detail/reactor_fwd.hpp"
  25. #include "asio/detail/task_io_service_operation.hpp"
  26. #include "asio/detail/push_options.hpp"
  27. namespace asio {
  28. namespace detail {
  29. struct task_io_service_thread_info;
  30. class task_io_service
  31. : public asio::detail::service_base<task_io_service>
  32. {
  33. public:
  34. typedef task_io_service_operation operation;
  35. // Constructor. Specifies the number of concurrent threads that are likely to
  36. // run the io_service. If set to 1 certain optimisation are performed.
  37. ASIO_DECL task_io_service(asio::io_service& io_service,
  38. std::size_t concurrency_hint = 0);
  39. // Destroy all user-defined handler objects owned by the service.
  40. ASIO_DECL void shutdown_service();
  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 io_service is stopped.
  54. ASIO_DECL bool stopped() const;
  55. // Reset in preparation for a subsequent run invocation.
  56. ASIO_DECL void reset();
  57. // Notify that some work has started.
  58. void work_started()
  59. {
  60. ++outstanding_work_;
  61. }
  62. // Notify that some work has finished.
  63. void work_finished()
  64. {
  65. if (--outstanding_work_ == 0)
  66. stop();
  67. }
  68. // Return whether a handler can be dispatched immediately.
  69. bool can_dispatch()
  70. {
  71. return thread_call_stack::contains(this) != 0;
  72. }
  73. // Request invocation of the given handler.
  74. template <typename Handler>
  75. void dispatch(Handler& handler);
  76. // Request invocation of the given handler and return immediately.
  77. template <typename Handler>
  78. void post(Handler& handler);
  79. // Request invocation of the given operation and return immediately. Assumes
  80. // that work_started() has not yet been called for the operation.
  81. ASIO_DECL void post_immediate_completion(
  82. operation* op, bool is_continuation);
  83. // Request invocation of the given operation and return immediately. Assumes
  84. // that work_started() was previously called for the operation.
  85. ASIO_DECL void post_deferred_completion(operation* op);
  86. // Request invocation of the given operations and return immediately. Assumes
  87. // that work_started() was previously called for each operation.
  88. ASIO_DECL void post_deferred_completions(op_queue<operation>& ops);
  89. // Process unfinished operations as part of a shutdown_service operation.
  90. // Assumes that 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 task_io_service_thread_info thread_info;
  95. // Enqueue the given operation following a failed attempt to dispatch the
  96. // operation for immediate invocation.
  97. ASIO_DECL void do_dispatch(operation* op);
  98. // Run at most one operation. May block.
  99. ASIO_DECL std::size_t do_run_one(mutex::scoped_lock& lock,
  100. thread_info& this_thread, const asio::error_code& ec);
  101. // Poll for at most one operation.
  102. ASIO_DECL std::size_t do_poll_one(mutex::scoped_lock& lock,
  103. thread_info& this_thread, const asio::error_code& ec);
  104. // Stop the task and all idle threads.
  105. ASIO_DECL void stop_all_threads(mutex::scoped_lock& lock);
  106. // Wake a single idle thread, or the task, and always unlock the mutex.
  107. ASIO_DECL void wake_one_thread_and_unlock(
  108. mutex::scoped_lock& lock);
  109. // Helper class to perform task-related operations on block exit.
  110. struct task_cleanup;
  111. friend struct task_cleanup;
  112. // Helper class to call work-related operations on block exit.
  113. struct work_cleanup;
  114. friend struct work_cleanup;
  115. // Whether to optimise for single-threaded use cases.
  116. const bool one_thread_;
  117. // Mutex to protect access to internal data.
  118. mutable mutex mutex_;
  119. // Event to wake up blocked threads.
  120. event wakeup_event_;
  121. // The task to be run by this service.
  122. reactor* task_;
  123. // Operation object to represent the position of the task in the queue.
  124. struct task_operation : operation
  125. {
  126. task_operation() : operation(0) {}
  127. } task_operation_;
  128. // Whether the task has been interrupted.
  129. bool task_interrupted_;
  130. // The count of unfinished work.
  131. atomic_count outstanding_work_;
  132. // The queue of handlers that are ready to be delivered.
  133. op_queue<operation> op_queue_;
  134. // Flag to indicate that the dispatcher has been stopped.
  135. bool stopped_;
  136. // Flag to indicate that the dispatcher has been shut down.
  137. bool shutdown_;
  138. // Per-thread call stack to track the state of each thread in the io_service.
  139. typedef call_stack<task_io_service, thread_info> thread_call_stack;
  140. };
  141. } // namespace detail
  142. } // namespace asio
  143. #include "asio/detail/pop_options.hpp"
  144. #include "asio/detail/impl/task_io_service.hpp"
  145. #if defined(ASIO_HEADER_ONLY)
  146. # include "asio/detail/impl/task_io_service.ipp"
  147. #endif // defined(ASIO_HEADER_ONLY)
  148. #endif // !defined(ASIO_HAS_IOCP)
  149. #endif // ASIO_DETAIL_TASK_IO_SERVICE_HPP