/Src/Dependencies/Boost/boost/asio/detail/impl/strand_service.hpp

http://hadesmem.googlecode.com/ · C++ Header · 120 lines · 82 code · 23 blank · 15 comment · 4 complexity · c5e6cc82b5f45dd2dddf9d63cacf00a1 MD5 · raw file

  1. //
  2. // detail/impl/strand_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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 BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/call_stack.hpp>
  16. #include <boost/asio/detail/completion_handler.hpp>
  17. #include <boost/asio/detail/fenced_block.hpp>
  18. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  19. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. inline strand_service::strand_impl::strand_impl()
  25. : operation(&strand_service::do_complete),
  26. count_(0)
  27. {
  28. }
  29. struct strand_service::on_dispatch_exit
  30. {
  31. io_service_impl* io_service_;
  32. strand_impl* impl_;
  33. ~on_dispatch_exit()
  34. {
  35. impl_->mutex_.lock();
  36. bool more_handlers = (--impl_->count_ > 0);
  37. impl_->mutex_.unlock();
  38. if (more_handlers)
  39. io_service_->post_immediate_completion(impl_);
  40. }
  41. };
  42. inline void strand_service::destroy(strand_service::implementation_type& impl)
  43. {
  44. impl = 0;
  45. }
  46. template <typename Handler>
  47. void strand_service::dispatch(strand_service::implementation_type& impl,
  48. Handler handler)
  49. {
  50. // If we are already in the strand then the handler can run immediately.
  51. if (call_stack<strand_impl>::contains(impl))
  52. {
  53. boost::asio::detail::fenced_block b;
  54. boost_asio_handler_invoke_helpers::invoke(handler, handler);
  55. return;
  56. }
  57. // Allocate and construct an operation to wrap the handler.
  58. typedef completion_handler<Handler> op;
  59. typename op::ptr p = { boost::addressof(handler),
  60. boost_asio_handler_alloc_helpers::allocate(
  61. sizeof(op), handler), 0 };
  62. p.p = new (p.v) op(handler);
  63. BOOST_ASIO_HANDLER_CREATION((p.p, "strand", impl, "dispatch"));
  64. bool dispatch_immediately = do_dispatch(impl, p.p);
  65. operation* o = p.p;
  66. p.v = p.p = 0;
  67. if (dispatch_immediately)
  68. {
  69. // Indicate that this strand is executing on the current thread.
  70. call_stack<strand_impl>::context ctx(impl);
  71. // Ensure the next handler, if any, is scheduled on block exit.
  72. on_dispatch_exit on_exit = { &io_service_, impl };
  73. (void)on_exit;
  74. completion_handler<Handler>::do_complete(
  75. &io_service_, o, boost::system::error_code(), 0);
  76. }
  77. }
  78. // Request the io_service to invoke the given handler and return immediately.
  79. template <typename Handler>
  80. void strand_service::post(strand_service::implementation_type& impl,
  81. Handler handler)
  82. {
  83. // Allocate and construct an operation to wrap the handler.
  84. typedef completion_handler<Handler> op;
  85. typename op::ptr p = { boost::addressof(handler),
  86. boost_asio_handler_alloc_helpers::allocate(
  87. sizeof(op), handler), 0 };
  88. p.p = new (p.v) op(handler);
  89. BOOST_ASIO_HANDLER_CREATION((p.p, "strand", impl, "post"));
  90. do_post(impl, p.p);
  91. p.v = p.p = 0;
  92. }
  93. } // namespace detail
  94. } // namespace asio
  95. } // namespace boost
  96. #include <boost/asio/detail/pop_options.hpp>
  97. #endif // BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP