win_thread.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // detail/win_thread.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_WIN_THREAD_HPP
  11. #define BOOST_ASIO_DETAIL_WIN_THREAD_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. #if defined(BOOST_ASIO_WINDOWS) \
  17. && !defined(BOOST_ASIO_WINDOWS_APP) \
  18. && !defined(UNDER_CE)
  19. #include <cstddef>
  20. #include <boost/asio/detail/memory.hpp>
  21. #include <boost/asio/detail/socket_types.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace detail {
  26. BOOST_ASIO_DECL unsigned int __stdcall win_thread_function(void* arg);
  27. #if defined(WINVER) && (WINVER < 0x0500)
  28. BOOST_ASIO_DECL void __stdcall apc_function(ULONG data);
  29. #else
  30. BOOST_ASIO_DECL void __stdcall apc_function(ULONG_PTR data);
  31. #endif
  32. template <typename T>
  33. class win_thread_base
  34. {
  35. public:
  36. static bool terminate_threads()
  37. {
  38. return ::InterlockedExchangeAdd(&terminate_threads_, 0) != 0;
  39. }
  40. static void set_terminate_threads(bool b)
  41. {
  42. ::InterlockedExchange(&terminate_threads_, b ? 1 : 0);
  43. }
  44. private:
  45. static long terminate_threads_;
  46. };
  47. template <typename T>
  48. long win_thread_base<T>::terminate_threads_ = 0;
  49. class win_thread
  50. : public win_thread_base<win_thread>
  51. {
  52. public:
  53. // Construct in a non-joinable state.
  54. win_thread() noexcept
  55. : arg_(0)
  56. {
  57. }
  58. // Constructor.
  59. template <typename Function>
  60. win_thread(Function f, unsigned int stack_size = 0)
  61. : win_thread(std::allocator_arg, std::allocator<void>(), f, stack_size)
  62. {
  63. }
  64. // Construct with custom allocator.
  65. template <typename Allocator, typename Function>
  66. win_thread(allocator_arg_t, const Allocator& a,
  67. Function f, unsigned int stack_size = 0)
  68. : arg_(start_thread(allocate_object<func<Function, Allocator>>(a, f, a),
  69. stack_size))
  70. {
  71. }
  72. // Move constructor.
  73. win_thread(win_thread&& other) noexcept
  74. : arg_(other.arg_)
  75. {
  76. other.arg_ = 0;
  77. }
  78. // Destructor.
  79. BOOST_ASIO_DECL ~win_thread();
  80. // Move assignment.
  81. win_thread& operator=(win_thread&& other) noexcept
  82. {
  83. arg_ = other.arg_;
  84. other.arg_ = 0;
  85. return *this;
  86. }
  87. // Whether the thread can be joined.
  88. bool joinable() const
  89. {
  90. return !!arg_;
  91. }
  92. // Wait for the thread to exit.
  93. BOOST_ASIO_DECL void join();
  94. // Get number of CPUs.
  95. BOOST_ASIO_DECL static std::size_t hardware_concurrency();
  96. private:
  97. friend BOOST_ASIO_DECL unsigned int __stdcall win_thread_function(void* arg);
  98. #if defined(WINVER) && (WINVER < 0x0500)
  99. friend BOOST_ASIO_DECL void __stdcall apc_function(ULONG);
  100. #else
  101. friend BOOST_ASIO_DECL void __stdcall apc_function(ULONG_PTR);
  102. #endif
  103. class func_base
  104. {
  105. public:
  106. virtual ~func_base() {}
  107. virtual void run() = 0;
  108. virtual void destroy() = 0;
  109. ::HANDLE thread_;
  110. ::HANDLE entry_event_;
  111. ::HANDLE exit_event_;
  112. };
  113. template <typename Function, typename Allocator>
  114. class func
  115. : public func_base
  116. {
  117. public:
  118. func(Function f, const Allocator& a)
  119. : f_(f),
  120. allocator_(a)
  121. {
  122. }
  123. virtual void run()
  124. {
  125. f_();
  126. }
  127. virtual void destroy()
  128. {
  129. deallocate_object(allocator_, this);
  130. }
  131. private:
  132. Function f_;
  133. Allocator allocator_;
  134. };
  135. BOOST_ASIO_DECL func_base* start_thread(
  136. func_base* arg, unsigned int stack_size);
  137. func_base* arg_;
  138. };
  139. } // namespace detail
  140. } // namespace asio
  141. } // namespace boost
  142. #include <boost/asio/detail/pop_options.hpp>
  143. #if defined(BOOST_ASIO_HEADER_ONLY)
  144. # include <boost/asio/detail/impl/win_thread.ipp>
  145. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  146. #endif // defined(BOOST_ASIO_WINDOWS)
  147. // && !defined(BOOST_ASIO_WINDOWS_APP)
  148. // && !defined(UNDER_CE)
  149. #endif // BOOST_ASIO_DETAIL_WIN_THREAD_HPP