win_thread.ipp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // detail/impl/win_thread.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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 ASIO_DETAIL_IMPL_WIN_THREAD_IPP
  11. #define 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 "asio/detail/config.hpp"
  16. #if defined(ASIO_WINDOWS) && !defined(UNDER_CE)
  17. #include <process.h>
  18. #include "asio/detail/throw_error.hpp"
  19. #include "asio/detail/win_thread.hpp"
  20. #include "asio/error.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. namespace detail {
  24. win_thread::~win_thread()
  25. {
  26. ::CloseHandle(thread_);
  27. // The exit_event_ handle is deliberately allowed to leak here since it
  28. // is an error for the owner of an internal thread not to join() it.
  29. }
  30. void win_thread::join()
  31. {
  32. HANDLE handles[2] = { exit_event_, thread_ };
  33. ::WaitForMultipleObjects(2, handles, FALSE, INFINITE);
  34. ::CloseHandle(exit_event_);
  35. if (terminate_threads())
  36. {
  37. ::TerminateThread(thread_, 0);
  38. }
  39. else
  40. {
  41. ::QueueUserAPC(apc_function, thread_, 0);
  42. ::WaitForSingleObject(thread_, INFINITE);
  43. }
  44. }
  45. void win_thread::start_thread(func_base* arg, unsigned int stack_size)
  46. {
  47. ::HANDLE entry_event = 0;
  48. arg->entry_event_ = entry_event = ::CreateEvent(0, true, false, 0);
  49. if (!entry_event)
  50. {
  51. DWORD last_error = ::GetLastError();
  52. delete arg;
  53. asio::error_code ec(last_error,
  54. asio::error::get_system_category());
  55. asio::detail::throw_error(ec, "thread.entry_event");
  56. }
  57. arg->exit_event_ = exit_event_ = ::CreateEvent(0, true, false, 0);
  58. if (!exit_event_)
  59. {
  60. DWORD last_error = ::GetLastError();
  61. delete arg;
  62. asio::error_code ec(last_error,
  63. asio::error::get_system_category());
  64. asio::detail::throw_error(ec, "thread.exit_event");
  65. }
  66. unsigned int thread_id = 0;
  67. thread_ = reinterpret_cast<HANDLE>(::_beginthreadex(0,
  68. stack_size, win_thread_function, arg, 0, &thread_id));
  69. if (!thread_)
  70. {
  71. DWORD last_error = ::GetLastError();
  72. delete arg;
  73. if (entry_event)
  74. ::CloseHandle(entry_event);
  75. if (exit_event_)
  76. ::CloseHandle(exit_event_);
  77. asio::error_code ec(last_error,
  78. asio::error::get_system_category());
  79. asio::detail::throw_error(ec, "thread");
  80. }
  81. if (entry_event)
  82. {
  83. ::WaitForSingleObject(entry_event, INFINITE);
  84. ::CloseHandle(entry_event);
  85. }
  86. }
  87. unsigned int __stdcall win_thread_function(void* arg)
  88. {
  89. win_thread::auto_func_base_ptr func = {
  90. static_cast<win_thread::func_base*>(arg) };
  91. ::SetEvent(func.ptr->entry_event_);
  92. func.ptr->run();
  93. // Signal that the thread has finished its work, but rather than returning go
  94. // to sleep to put the thread into a well known state. If the thread is being
  95. // joined during global object destruction then it may be killed using
  96. // TerminateThread (to avoid a deadlock in DllMain). Otherwise, the SleepEx
  97. // call will be interrupted using QueueUserAPC and the thread will shut down
  98. // cleanly.
  99. HANDLE exit_event = func.ptr->exit_event_;
  100. delete func.ptr;
  101. func.ptr = 0;
  102. ::SetEvent(exit_event);
  103. ::SleepEx(INFINITE, TRUE);
  104. return 0;
  105. }
  106. #if defined(WINVER) && (WINVER < 0x0500)
  107. void __stdcall apc_function(ULONG) {}
  108. #else
  109. void __stdcall apc_function(ULONG_PTR) {}
  110. #endif
  111. } // namespace detail
  112. } // namespace asio
  113. #include "asio/detail/pop_options.hpp"
  114. #endif // defined(ASIO_WINDOWS) && !defined(UNDER_CE)
  115. #endif // ASIO_DETAIL_IMPL_WIN_THREAD_IPP