resolver_thread_pool.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // detail/resolver_thread_pool.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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_RESOLVER_THREAD_POOL_HPP
  11. #define BOOST_ASIO_DETAIL_RESOLVER_THREAD_POOL_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/execution_context.hpp>
  17. #include <boost/asio/detail/mutex.hpp>
  18. #include <boost/asio/detail/resolve_op.hpp>
  19. #include <boost/asio/detail/scheduler.hpp>
  20. #include <boost/asio/detail/thread_group.hpp>
  21. #if defined(BOOST_ASIO_HAS_IOCP)
  22. # include <boost/asio/detail/win_iocp_io_context.hpp>
  23. #else // defined(BOOST_ASIO_HAS_IOCP)
  24. # include <boost/asio/detail/scheduler.hpp>
  25. #endif // defined(BOOST_ASIO_HAS_IOCP)
  26. #include <boost/asio/detail/push_options.hpp>
  27. namespace boost {
  28. namespace asio {
  29. namespace detail {
  30. class resolver_thread_pool :
  31. public execution_context_service_base<resolver_thread_pool>
  32. {
  33. public:
  34. #if defined(BOOST_ASIO_HAS_IOCP)
  35. typedef class win_iocp_io_context scheduler_impl;
  36. #else
  37. typedef class scheduler scheduler_impl;
  38. #endif
  39. // Constructor.
  40. BOOST_ASIO_DECL resolver_thread_pool(execution_context& context);
  41. // Destructor.
  42. BOOST_ASIO_DECL ~resolver_thread_pool();
  43. // Destroy all user-defined handler objects owned by the service.
  44. BOOST_ASIO_DECL void shutdown();
  45. // Perform any fork-related housekeeping.
  46. BOOST_ASIO_DECL void notify_fork(execution_context::fork_event fork_ev);
  47. // Helper function to start an asynchronous resolve operation.
  48. BOOST_ASIO_DECL void start_resolve_op(resolve_op* op);
  49. // Get the underlying scheduler implementation.
  50. scheduler_impl& scheduler()
  51. {
  52. return scheduler_;
  53. }
  54. private:
  55. // Helper class to run the work scheduler in a thread.
  56. class work_scheduler_runner;
  57. // Start the work scheduler if it's not already running.
  58. BOOST_ASIO_DECL void start_work_threads();
  59. // The scheduler implementation used to post completions.
  60. scheduler_impl& scheduler_;
  61. // Mutex to protect access to internal data.
  62. boost::asio::detail::mutex mutex_;
  63. // Private scheduler used for performing asynchronous host resolution.
  64. scheduler_impl work_scheduler_;
  65. // Threads used for running the work scheduler's run loop.
  66. thread_group<execution_context::allocator<void>> work_threads_;
  67. // The number of threads used to run the work scheduler.
  68. unsigned int num_work_threads_;
  69. // Whether the scheduler locking is enabled.
  70. bool scheduler_locking_;
  71. // Whether the scheduler has been shut down.
  72. bool shutdown_;
  73. };
  74. } // namespace detail
  75. } // namespace asio
  76. } // namespace boost
  77. #include <boost/asio/detail/pop_options.hpp>
  78. #if defined(BOOST_ASIO_HEADER_ONLY)
  79. # include <boost/asio/detail/impl/resolver_thread_pool.ipp>
  80. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  81. #endif // BOOST_ASIO_DETAIL_RESOLVER_THREAD_POOL_HPP