strand_executor_service.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // detail/strand_executor_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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_STRAND_EXECUTOR_SERVICE_HPP
  11. #define ASIO_DETAIL_STRAND_EXECUTOR_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. #include "asio/detail/atomic_count.hpp"
  17. #include "asio/detail/executor_op.hpp"
  18. #include "asio/detail/memory.hpp"
  19. #include "asio/detail/mutex.hpp"
  20. #include "asio/detail/op_queue.hpp"
  21. #include "asio/detail/scheduler_operation.hpp"
  22. #include "asio/detail/scoped_ptr.hpp"
  23. #include "asio/detail/type_traits.hpp"
  24. #include "asio/execution.hpp"
  25. #include "asio/execution_context.hpp"
  26. #include "asio/detail/push_options.hpp"
  27. namespace asio {
  28. namespace detail {
  29. // Default service implementation for a strand.
  30. class strand_executor_service
  31. : public execution_context_service_base<strand_executor_service>
  32. {
  33. public:
  34. // The underlying implementation of a strand.
  35. class strand_impl
  36. {
  37. public:
  38. ASIO_DECL ~strand_impl();
  39. private:
  40. friend class strand_executor_service;
  41. // Mutex to protect access to internal data.
  42. mutex* mutex_;
  43. // Indicates whether the strand is currently "locked" by a handler. This
  44. // means that there is a handler upcall in progress, or that the strand
  45. // itself has been scheduled in order to invoke some pending handlers.
  46. bool locked_;
  47. // Indicates that the strand has been shut down and will accept no further
  48. // handlers.
  49. bool shutdown_;
  50. // The handlers that are waiting on the strand but should not be run until
  51. // after the next time the strand is scheduled. This queue must only be
  52. // modified while the mutex is locked.
  53. op_queue<scheduler_operation> waiting_queue_;
  54. // The handlers that are ready to be run. Logically speaking, these are the
  55. // handlers that hold the strand's lock. The ready queue is only modified
  56. // from within the strand and so may be accessed without locking the mutex.
  57. op_queue<scheduler_operation> ready_queue_;
  58. // Pointers to adjacent handle implementations in linked list.
  59. strand_impl* next_;
  60. strand_impl* prev_;
  61. // The strand service in where the implementation is held.
  62. strand_executor_service* service_;
  63. };
  64. typedef shared_ptr<strand_impl> implementation_type;
  65. // Construct a new strand service for the specified context.
  66. ASIO_DECL explicit strand_executor_service(execution_context& context);
  67. // Destroy all user-defined handler objects owned by the service.
  68. ASIO_DECL void shutdown();
  69. // Create a new strand_executor implementation.
  70. ASIO_DECL implementation_type create_implementation();
  71. // Request invocation of the given function.
  72. template <typename Executor, typename Function>
  73. static void execute(const implementation_type& impl, Executor& ex,
  74. Function&& function,
  75. enable_if_t<
  76. can_query<Executor, execution::allocator_t<void>>::value
  77. >* = 0);
  78. // Request invocation of the given function.
  79. template <typename Executor, typename Function>
  80. static void execute(const implementation_type& impl, Executor& ex,
  81. Function&& function,
  82. enable_if_t<
  83. !can_query<Executor, execution::allocator_t<void>>::value
  84. >* = 0);
  85. // Request invocation of the given function.
  86. template <typename Executor, typename Function, typename Allocator>
  87. static void dispatch(const implementation_type& impl, Executor& ex,
  88. Function&& function, const Allocator& a);
  89. // Request invocation of the given function and return immediately.
  90. template <typename Executor, typename Function, typename Allocator>
  91. static void post(const implementation_type& impl, Executor& ex,
  92. Function&& function, const Allocator& a);
  93. // Request invocation of the given function and return immediately.
  94. template <typename Executor, typename Function, typename Allocator>
  95. static void defer(const implementation_type& impl, Executor& ex,
  96. Function&& function, const Allocator& a);
  97. // Determine whether the strand is running in the current thread.
  98. ASIO_DECL static bool running_in_this_thread(
  99. const implementation_type& impl);
  100. private:
  101. friend class strand_impl;
  102. template <typename F, typename Allocator> class allocator_binder;
  103. template <typename Executor, typename = void> class invoker;
  104. // Adds a function to the strand. Returns true if it acquires the lock.
  105. ASIO_DECL static bool enqueue(const implementation_type& impl,
  106. scheduler_operation* op);
  107. // Transfers waiting handlers to the ready queue. Returns true if one or more
  108. // handlers were transferred.
  109. ASIO_DECL static bool push_waiting_to_ready(implementation_type& impl);
  110. // Invokes all ready-to-run handlers.
  111. ASIO_DECL static void run_ready_handlers(implementation_type& impl);
  112. // Helper function to request invocation of the given function.
  113. template <typename Executor, typename Function, typename Allocator>
  114. static void do_execute(const implementation_type& impl, Executor& ex,
  115. Function&& function, const Allocator& a);
  116. // Mutex to protect access to the service-wide state.
  117. mutex mutex_;
  118. // Number of mutexes shared between all strand objects.
  119. enum { num_mutexes = 193 };
  120. // Pool of mutexes.
  121. scoped_ptr<mutex> mutexes_[num_mutexes];
  122. // Extra value used when hashing to prevent recycled memory locations from
  123. // getting the same mutex.
  124. std::size_t salt_;
  125. // The head of a linked list of all implementations.
  126. strand_impl* impl_list_;
  127. };
  128. } // namespace detail
  129. } // namespace asio
  130. #include "asio/detail/pop_options.hpp"
  131. #include "asio/detail/impl/strand_executor_service.hpp"
  132. #if defined(ASIO_HEADER_ONLY)
  133. # include "asio/detail/impl/strand_executor_service.ipp"
  134. #endif // defined(ASIO_HEADER_ONLY)
  135. #endif // ASIO_DETAIL_STRAND_EXECUTOR_SERVICE_HPP