win_thread.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // win_thread.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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_WIN_THREAD_HPP
  11. #define 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 "asio/detail/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <boost/config.hpp>
  18. #include "asio/detail/pop_options.hpp"
  19. #if defined(BOOST_WINDOWS) && !defined(UNDER_CE)
  20. #include "asio/error.hpp"
  21. #include "asio/system_error.hpp"
  22. #include "asio/detail/noncopyable.hpp"
  23. #include "asio/detail/socket_types.hpp"
  24. #include "asio/detail/push_options.hpp"
  25. #include <boost/throw_exception.hpp>
  26. #include <memory>
  27. #include <process.h>
  28. #include "asio/detail/pop_options.hpp"
  29. namespace asio {
  30. namespace detail {
  31. unsigned int __stdcall win_thread_function(void* arg);
  32. #if defined(WINVER) && (WINVER < 0x0500)
  33. void __stdcall apc_function(ULONG data);
  34. #else
  35. void __stdcall apc_function(ULONG_PTR data);
  36. #endif
  37. template <typename T>
  38. class win_thread_base
  39. {
  40. public:
  41. static bool terminate_threads()
  42. {
  43. return ::InterlockedExchangeAdd(&terminate_threads_, 0) != 0;
  44. }
  45. static void set_terminate_threads(bool b)
  46. {
  47. ::InterlockedExchange(&terminate_threads_, b ? 1 : 0);
  48. }
  49. private:
  50. static long terminate_threads_;
  51. };
  52. template <typename T>
  53. long win_thread_base<T>::terminate_threads_ = 0;
  54. class win_thread
  55. : private noncopyable,
  56. public win_thread_base<win_thread>
  57. {
  58. public:
  59. // Constructor.
  60. template <typename Function>
  61. win_thread(Function f)
  62. : exit_event_(0)
  63. {
  64. std::auto_ptr<func_base> arg(new func<Function>(f));
  65. ::HANDLE entry_event = 0;
  66. arg->entry_event_ = entry_event = ::CreateEvent(0, true, false, 0);
  67. if (!entry_event)
  68. {
  69. DWORD last_error = ::GetLastError();
  70. asio::system_error e(
  71. asio::error_code(last_error,
  72. asio::error::get_system_category()),
  73. "thread.entry_event");
  74. boost::throw_exception(e);
  75. }
  76. arg->exit_event_ = exit_event_ = ::CreateEvent(0, true, false, 0);
  77. if (!exit_event_)
  78. {
  79. DWORD last_error = ::GetLastError();
  80. ::CloseHandle(entry_event);
  81. asio::system_error e(
  82. asio::error_code(last_error,
  83. asio::error::get_system_category()),
  84. "thread.exit_event");
  85. boost::throw_exception(e);
  86. }
  87. unsigned int thread_id = 0;
  88. thread_ = reinterpret_cast<HANDLE>(::_beginthreadex(0, 0,
  89. win_thread_function, arg.get(), 0, &thread_id));
  90. if (!thread_)
  91. {
  92. DWORD last_error = ::GetLastError();
  93. if (entry_event)
  94. ::CloseHandle(entry_event);
  95. if (exit_event_)
  96. ::CloseHandle(exit_event_);
  97. asio::system_error e(
  98. asio::error_code(last_error,
  99. asio::error::get_system_category()),
  100. "thread");
  101. boost::throw_exception(e);
  102. }
  103. arg.release();
  104. if (entry_event)
  105. {
  106. ::WaitForSingleObject(entry_event, INFINITE);
  107. ::CloseHandle(entry_event);
  108. }
  109. }
  110. // Destructor.
  111. ~win_thread()
  112. {
  113. ::CloseHandle(thread_);
  114. // The exit_event_ handle is deliberately allowed to leak here since it
  115. // is an error for the owner of an internal thread not to join() it.
  116. }
  117. // Wait for the thread to exit.
  118. void join()
  119. {
  120. ::WaitForSingleObject(exit_event_, INFINITE);
  121. ::CloseHandle(exit_event_);
  122. if (terminate_threads())
  123. {
  124. ::TerminateThread(thread_, 0);
  125. }
  126. else
  127. {
  128. ::QueueUserAPC(apc_function, thread_, 0);
  129. ::WaitForSingleObject(thread_, INFINITE);
  130. }
  131. }
  132. private:
  133. friend unsigned int __stdcall win_thread_function(void* arg);
  134. #if defined(WINVER) && (WINVER < 0x0500)
  135. friend void __stdcall apc_function(ULONG);
  136. #else
  137. friend void __stdcall apc_function(ULONG_PTR);
  138. #endif
  139. class func_base
  140. {
  141. public:
  142. virtual ~func_base() {}
  143. virtual void run() = 0;
  144. ::HANDLE entry_event_;
  145. ::HANDLE exit_event_;
  146. };
  147. template <typename Function>
  148. class func
  149. : public func_base
  150. {
  151. public:
  152. func(Function f)
  153. : f_(f)
  154. {
  155. }
  156. virtual void run()
  157. {
  158. f_();
  159. }
  160. private:
  161. Function f_;
  162. };
  163. ::HANDLE thread_;
  164. ::HANDLE exit_event_;
  165. };
  166. inline unsigned int __stdcall win_thread_function(void* arg)
  167. {
  168. std::auto_ptr<win_thread::func_base> func(
  169. static_cast<win_thread::func_base*>(arg));
  170. ::SetEvent(func->entry_event_);
  171. func->run();
  172. // Signal that the thread has finished its work, but rather than returning go
  173. // to sleep to put the thread into a well known state. If the thread is being
  174. // joined during global object destruction then it may be killed using
  175. // TerminateThread (to avoid a deadlock in DllMain). Otherwise, the SleepEx
  176. // call will be interrupted using QueueUserAPC and the thread will shut down
  177. // cleanly.
  178. HANDLE exit_event = func->exit_event_;
  179. func.reset();
  180. ::SetEvent(exit_event);
  181. ::SleepEx(INFINITE, TRUE);
  182. return 0;
  183. }
  184. #if defined(WINVER) && (WINVER < 0x0500)
  185. inline void __stdcall apc_function(ULONG) {}
  186. #else
  187. inline void __stdcall apc_function(ULONG_PTR) {}
  188. #endif
  189. } // namespace detail
  190. } // namespace asio
  191. #endif // defined(BOOST_WINDOWS) && !defined(UNDER_CE)
  192. #include "asio/detail/pop_options.hpp"
  193. #endif // ASIO_DETAIL_WIN_THREAD_HPP