strand_executor_service.hpp 4.9 KB

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