win_thread.hpp 5.5 KB

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