strand_service.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // detail/strand_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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_STRAND_SERVICE_HPP
  11. #define BOOST_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 <boost/asio/detail/config.hpp>
  16. #include <boost/asio/io_context.hpp>
  17. #include <boost/asio/detail/mutex.hpp>
  18. #include <boost/asio/detail/op_queue.hpp>
  19. #include <boost/asio/detail/operation.hpp>
  20. #include <boost/asio/detail/scoped_ptr.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. // Default service implementation for a strand.
  26. class strand_service
  27. : public boost::asio::detail::service_base<strand_service>
  28. {
  29. private:
  30. // Helper class to re-post the strand on exit.
  31. struct on_do_complete_exit;
  32. // Helper class to re-post the strand on exit.
  33. struct on_dispatch_exit;
  34. public:
  35. // The underlying implementation of a strand.
  36. class strand_impl
  37. : public operation
  38. {
  39. public:
  40. strand_impl();
  41. private:
  42. // Only this service will have access to the internal values.
  43. friend class strand_service;
  44. friend struct on_do_complete_exit;
  45. friend struct on_dispatch_exit;
  46. // Mutex to protect access to internal data.
  47. boost::asio::detail::mutex mutex_;
  48. // Indicates whether the strand is currently "locked" by a handler. This
  49. // means that there is a handler upcall in progress, or that the strand
  50. // itself has been scheduled in order to invoke some pending handlers.
  51. bool locked_;
  52. // The handlers that are waiting on the strand but should not be run until
  53. // after the next time the strand is scheduled. This queue must only be
  54. // modified while the mutex is locked.
  55. op_queue<operation> waiting_queue_;
  56. // The handlers that are ready to be run. Logically speaking, these are the
  57. // handlers that hold the strand's lock. The ready queue is only modified
  58. // from within the strand and so may be accessed without locking the mutex.
  59. op_queue<operation> ready_queue_;
  60. };
  61. typedef strand_impl* implementation_type;
  62. // Construct a new strand service for the specified io_context.
  63. BOOST_ASIO_DECL explicit strand_service(boost::asio::io_context& io_context);
  64. // Destroy all user-defined handler objects owned by the service.
  65. BOOST_ASIO_DECL void shutdown();
  66. // Construct a new strand implementation.
  67. BOOST_ASIO_DECL void construct(implementation_type& impl);
  68. // Request the io_context to invoke the given handler.
  69. template <typename Handler>
  70. void dispatch(implementation_type& impl, Handler& handler);
  71. // Request the io_context to invoke the given handler and return immediately.
  72. template <typename Handler>
  73. void post(implementation_type& impl, Handler& handler);
  74. // Determine whether the strand is running in the current thread.
  75. BOOST_ASIO_DECL bool running_in_this_thread(
  76. const implementation_type& impl) const;
  77. private:
  78. // Helper function to dispatch a handler. Returns true if the handler should
  79. // be dispatched immediately.
  80. BOOST_ASIO_DECL bool do_dispatch(implementation_type& impl, operation* op);
  81. // Helper fiunction to post a handler.
  82. BOOST_ASIO_DECL void do_post(implementation_type& impl,
  83. operation* op, bool is_continuation);
  84. BOOST_ASIO_DECL static void do_complete(void* owner,
  85. operation* base, const boost::system::error_code& ec,
  86. std::size_t bytes_transferred);
  87. // The io_context implementation used to post completions.
  88. io_context_impl& io_context_;
  89. // Mutex to protect access to the array of implementations.
  90. boost::asio::detail::mutex mutex_;
  91. // Number of implementations shared between all strand objects.
  92. #if defined(BOOST_ASIO_STRAND_IMPLEMENTATIONS)
  93. enum { num_implementations = BOOST_ASIO_STRAND_IMPLEMENTATIONS };
  94. #else // defined(BOOST_ASIO_STRAND_IMPLEMENTATIONS)
  95. enum { num_implementations = 193 };
  96. #endif // defined(BOOST_ASIO_STRAND_IMPLEMENTATIONS)
  97. // Pool of implementations.
  98. scoped_ptr<strand_impl> implementations_[num_implementations];
  99. // Extra value used when hashing to prevent recycled memory locations from
  100. // getting the same strand implementation.
  101. std::size_t salt_;
  102. };
  103. } // namespace detail
  104. } // namespace asio
  105. } // namespace boost
  106. #include <boost/asio/detail/pop_options.hpp>
  107. #include <boost/asio/detail/impl/strand_service.hpp>
  108. #if defined(BOOST_ASIO_HEADER_ONLY)
  109. # include <boost/asio/detail/impl/strand_service.ipp>
  110. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  111. #endif // BOOST_ASIO_DETAIL_STRAND_SERVICE_HPP