strand_service.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //
  2. // strand_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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_SERVICE_HPP
  11. #define ASIO_DETAIL_STRAND_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/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <boost/functional/hash.hpp>
  18. #include <boost/scoped_ptr.hpp>
  19. #include "asio/detail/pop_options.hpp"
  20. #include "asio/io_service.hpp"
  21. #include "asio/detail/call_stack.hpp"
  22. #include "asio/detail/completion_handler.hpp"
  23. #include "asio/detail/fenced_block.hpp"
  24. #include "asio/detail/handler_alloc_helpers.hpp"
  25. #include "asio/detail/handler_invoke_helpers.hpp"
  26. #include "asio/detail/mutex.hpp"
  27. #include "asio/detail/op_queue.hpp"
  28. #include "asio/detail/operation.hpp"
  29. #include "asio/detail/service_base.hpp"
  30. namespace asio {
  31. namespace detail {
  32. // Default service implementation for a strand.
  33. class strand_service
  34. : public asio::detail::service_base<strand_service>
  35. {
  36. private:
  37. struct on_do_complete_exit;
  38. struct on_dispatch_exit;
  39. public:
  40. // The underlying implementation of a strand.
  41. class strand_impl
  42. : public operation
  43. {
  44. public:
  45. strand_impl()
  46. : operation(&strand_service::do_complete),
  47. count_(0)
  48. {
  49. }
  50. private:
  51. // Only this service will have access to the internal values.
  52. friend class strand_service;
  53. friend struct on_do_complete_exit;
  54. friend struct on_dispatch_exit;
  55. // Mutex to protect access to internal data.
  56. asio::detail::mutex mutex_;
  57. // The count of handlers in the strand, including the upcall (if any).
  58. std::size_t count_;
  59. // The handlers waiting on the strand.
  60. op_queue<operation> queue_;
  61. };
  62. typedef strand_impl* implementation_type;
  63. // Construct a new strand service for the specified io_service.
  64. explicit strand_service(asio::io_service& io_service)
  65. : asio::detail::service_base<strand_service>(io_service),
  66. io_service_(asio::use_service<io_service_impl>(io_service)),
  67. mutex_(),
  68. salt_(0)
  69. {
  70. }
  71. // Destroy all user-defined handler objects owned by the service.
  72. void shutdown_service()
  73. {
  74. op_queue<operation> ops;
  75. asio::detail::mutex::scoped_lock lock(mutex_);
  76. for (std::size_t i = 0; i < num_implementations; ++i)
  77. if (strand_impl* impl = implementations_[i].get())
  78. ops.push(impl->queue_);
  79. }
  80. // Construct a new strand implementation.
  81. void construct(implementation_type& impl)
  82. {
  83. std::size_t index = boost::hash_value(&impl);
  84. boost::hash_combine(index, salt_++);
  85. index = index % num_implementations;
  86. asio::detail::mutex::scoped_lock lock(mutex_);
  87. if (!implementations_[index])
  88. implementations_[index].reset(new strand_impl);
  89. impl = implementations_[index].get();
  90. }
  91. // Destroy a strand implementation.
  92. void destroy(implementation_type& impl)
  93. {
  94. impl = 0;
  95. }
  96. // Request the io_service to invoke the given handler.
  97. template <typename Handler>
  98. void dispatch(implementation_type& impl, Handler handler)
  99. {
  100. // If we are already in the strand then the handler can run immediately.
  101. if (call_stack<strand_impl>::contains(impl))
  102. {
  103. asio::detail::fenced_block b;
  104. asio_handler_invoke_helpers::invoke(handler, handler);
  105. return;
  106. }
  107. // Allocate and construct an object to wrap the handler.
  108. typedef completion_handler<Handler> value_type;
  109. typedef handler_alloc_traits<Handler, value_type> alloc_traits;
  110. raw_handler_ptr<alloc_traits> raw_ptr(handler);
  111. handler_ptr<alloc_traits> ptr(raw_ptr, handler);
  112. // If we are running inside the io_service, and no other handler is queued
  113. // or running, then the handler can run immediately.
  114. bool can_dispatch = call_stack<io_service_impl>::contains(&io_service_);
  115. impl->mutex_.lock();
  116. bool first = (++impl->count_ == 1);
  117. if (can_dispatch && first)
  118. {
  119. // Immediate invocation is allowed.
  120. impl->mutex_.unlock();
  121. // Memory must be releaesed before any upcall is made.
  122. ptr.reset();
  123. // Indicate that this strand is executing on the current thread.
  124. call_stack<strand_impl>::context ctx(impl);
  125. // Ensure the next handler, if any, is scheduled on block exit.
  126. on_dispatch_exit on_exit = { &io_service_, impl };
  127. (void)on_exit;
  128. asio::detail::fenced_block b;
  129. asio_handler_invoke_helpers::invoke(handler, handler);
  130. return;
  131. }
  132. // Immediate invocation is not allowed, so enqueue for later.
  133. impl->queue_.push(ptr.get());
  134. impl->mutex_.unlock();
  135. ptr.release();
  136. // The first handler to be enqueued is responsible for scheduling the
  137. // strand.
  138. if (first)
  139. io_service_.post_immediate_completion(impl);
  140. }
  141. // Request the io_service to invoke the given handler and return immediately.
  142. template <typename Handler>
  143. void post(implementation_type& impl, Handler handler)
  144. {
  145. // Allocate and construct an object to wrap the handler.
  146. typedef completion_handler<Handler> value_type;
  147. typedef handler_alloc_traits<Handler, value_type> alloc_traits;
  148. raw_handler_ptr<alloc_traits> raw_ptr(handler);
  149. handler_ptr<alloc_traits> ptr(raw_ptr, handler);
  150. // Add the handler to the queue.
  151. impl->mutex_.lock();
  152. bool first = (++impl->count_ == 1);
  153. impl->queue_.push(ptr.get());
  154. impl->mutex_.unlock();
  155. ptr.release();
  156. // The first handler to be enqueue is responsible for scheduling the strand.
  157. if (first)
  158. io_service_.post_immediate_completion(impl);
  159. }
  160. private:
  161. static void do_complete(io_service_impl* owner, operation* base,
  162. asio::error_code /*ec*/, std::size_t /*bytes_transferred*/)
  163. {
  164. if (owner)
  165. {
  166. strand_impl* impl = static_cast<strand_impl*>(base);
  167. // Get the next handler to be executed.
  168. impl->mutex_.lock();
  169. operation* o = impl->queue_.front();
  170. impl->queue_.pop();
  171. impl->mutex_.unlock();
  172. // Indicate that this strand is executing on the current thread.
  173. call_stack<strand_impl>::context ctx(impl);
  174. // Ensure the next handler, if any, is scheduled on block exit.
  175. on_do_complete_exit on_exit = { owner, impl };
  176. (void)on_exit;
  177. o->complete(*owner);
  178. }
  179. }
  180. // Helper class to re-post the strand on exit.
  181. struct on_do_complete_exit
  182. {
  183. io_service_impl* owner_;
  184. strand_impl* impl_;
  185. ~on_do_complete_exit()
  186. {
  187. impl_->mutex_.lock();
  188. bool more_handlers = (--impl_->count_ > 0);
  189. impl_->mutex_.unlock();
  190. if (more_handlers)
  191. owner_->post_immediate_completion(impl_);
  192. }
  193. };
  194. // Helper class to re-post the strand on exit.
  195. struct on_dispatch_exit
  196. {
  197. io_service_impl* io_service_;
  198. strand_impl* impl_;
  199. ~on_dispatch_exit()
  200. {
  201. impl_->mutex_.lock();
  202. bool more_handlers = (--impl_->count_ > 0);
  203. impl_->mutex_.unlock();
  204. if (more_handlers)
  205. io_service_->post_immediate_completion(impl_);
  206. }
  207. };
  208. // The io_service implementation used to post completions.
  209. io_service_impl& io_service_;
  210. // Mutex to protect access to the array of implementations.
  211. asio::detail::mutex mutex_;
  212. // Number of implementations shared between all strand objects.
  213. enum { num_implementations = 193 };
  214. // The head of a linked list of all implementations.
  215. boost::scoped_ptr<strand_impl> implementations_[num_implementations];
  216. // Extra value used when hashing to prevent recycled memory locations from
  217. // getting the same strand implementation.
  218. std::size_t salt_;
  219. };
  220. } // namespace detail
  221. } // namespace asio
  222. #include "asio/detail/pop_options.hpp"
  223. #endif // ASIO_DETAIL_STRAND_SERVICE_HPP