win_thread.ipp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // detail/impl/win_thread.ipp
  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_IMPL_WIN_THREAD_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_WIN_THREAD_IPP
  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 <process.h>
  20. #include <boost/asio/detail/throw_error.hpp>
  21. #include <boost/asio/detail/win_thread.hpp>
  22. #include <boost/asio/error.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace detail {
  27. win_thread::~win_thread()
  28. {
  29. if (arg_)
  30. std::terminate();
  31. }
  32. void win_thread::join()
  33. {
  34. if (arg_)
  35. {
  36. HANDLE handles[2] = { arg_->exit_event_, arg_->thread_ };
  37. ::WaitForMultipleObjects(2, handles, FALSE, INFINITE);
  38. ::CloseHandle(arg_->exit_event_);
  39. if (terminate_threads())
  40. {
  41. ::TerminateThread(arg_->thread_, 0);
  42. }
  43. else
  44. {
  45. ::QueueUserAPC(apc_function, arg_->thread_, 0);
  46. ::WaitForSingleObject(arg_->thread_, INFINITE);
  47. }
  48. ::CloseHandle(arg_->thread_);
  49. arg_->destroy();
  50. arg_ = 0;
  51. }
  52. }
  53. std::size_t win_thread::hardware_concurrency()
  54. {
  55. SYSTEM_INFO system_info;
  56. ::GetSystemInfo(&system_info);
  57. return system_info.dwNumberOfProcessors;
  58. }
  59. win_thread::func_base* win_thread::start_thread(
  60. func_base* arg, unsigned int stack_size)
  61. {
  62. arg->entry_event_ = ::CreateEventW(0, true, false, 0);
  63. if (!arg->entry_event_)
  64. {
  65. DWORD last_error = ::GetLastError();
  66. arg->destroy();
  67. boost::system::error_code ec(last_error,
  68. boost::asio::error::get_system_category());
  69. boost::asio::detail::throw_error(ec, "thread.entry_event");
  70. }
  71. arg->exit_event_ = ::CreateEventW(0, true, false, 0);
  72. if (!arg->exit_event_)
  73. {
  74. DWORD last_error = ::GetLastError();
  75. ::CloseHandle(arg->entry_event_);
  76. arg->destroy();
  77. boost::system::error_code ec(last_error,
  78. boost::asio::error::get_system_category());
  79. boost::asio::detail::throw_error(ec, "thread.exit_event");
  80. }
  81. unsigned int thread_id = 0;
  82. arg->thread_ = reinterpret_cast<HANDLE>(::_beginthreadex(0,
  83. stack_size, win_thread_function, arg, 0, &thread_id));
  84. if (!arg->thread_)
  85. {
  86. DWORD last_error = ::GetLastError();
  87. ::CloseHandle(arg->entry_event_);
  88. ::CloseHandle(arg->exit_event_);
  89. arg->destroy();
  90. boost::system::error_code ec(last_error,
  91. boost::asio::error::get_system_category());
  92. boost::asio::detail::throw_error(ec, "thread");
  93. }
  94. if (arg->entry_event_)
  95. {
  96. ::WaitForSingleObject(arg->entry_event_, INFINITE);
  97. ::CloseHandle(arg->entry_event_);
  98. arg->entry_event_ = 0;
  99. }
  100. return arg;
  101. }
  102. unsigned int __stdcall win_thread_function(void* arg)
  103. {
  104. win_thread::func_base* func = static_cast<win_thread::func_base*>(arg);
  105. ::SetEvent(func->entry_event_);
  106. func->run();
  107. // Signal that the thread has finished its work, but rather than returning go
  108. // to sleep to put the thread into a well known state. If the thread is being
  109. // joined during global object destruction then it may be killed using
  110. // TerminateThread (to avoid a deadlock in DllMain). Otherwise, the SleepEx
  111. // call will be interrupted using QueueUserAPC and the thread will shut down
  112. // cleanly.
  113. ::SetEvent(func->exit_event_);
  114. ::SleepEx(INFINITE, TRUE);
  115. return 0;
  116. }
  117. #if defined(WINVER) && (WINVER < 0x0500)
  118. void __stdcall apc_function(ULONG) {}
  119. #else
  120. void __stdcall apc_function(ULONG_PTR) {}
  121. #endif
  122. } // namespace detail
  123. } // namespace asio
  124. } // namespace boost
  125. #include <boost/asio/detail/pop_options.hpp>
  126. #endif // defined(BOOST_ASIO_WINDOWS)
  127. // && !defined(BOOST_ASIO_WINDOWS_APP)
  128. // && !defined(UNDER_CE)
  129. #endif // BOOST_ASIO_DETAIL_IMPL_WIN_THREAD_IPP