win_thread.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // detail/win_thread.hpp
  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 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 <boost/asio/detail/noncopyable.hpp>
  20. #include <boost/asio/detail/socket_types.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. BOOST_ASIO_DECL unsigned int __stdcall win_thread_function(void* arg);
  26. #if defined(WINVER) && (WINVER < 0x0500)
  27. BOOST_ASIO_DECL void __stdcall apc_function(ULONG data);
  28. #else
  29. BOOST_ASIO_DECL void __stdcall apc_function(ULONG_PTR data);
  30. #endif
  31. template <typename T>
  32. class win_thread_base
  33. {
  34. public:
  35. static bool terminate_threads()
  36. {
  37. return ::InterlockedExchangeAdd(&terminate_threads_, 0) != 0;
  38. }
  39. static void set_terminate_threads(bool b)
  40. {
  41. ::InterlockedExchange(&terminate_threads_, b ? 1 : 0);
  42. }
  43. private:
  44. static long terminate_threads_;
  45. };
  46. template <typename T>
  47. long win_thread_base<T>::terminate_threads_ = 0;
  48. class win_thread
  49. : private noncopyable,
  50. public win_thread_base<win_thread>
  51. {
  52. public:
  53. // Constructor.
  54. template <typename Function>
  55. win_thread(Function f, unsigned int stack_size = 0)
  56. : thread_(0),
  57. exit_event_(0)
  58. {
  59. start_thread(new func<Function>(f), stack_size);
  60. }
  61. // Destructor.
  62. BOOST_ASIO_DECL ~win_thread();
  63. // Wait for the thread to exit.
  64. BOOST_ASIO_DECL void join();
  65. private:
  66. friend BOOST_ASIO_DECL unsigned int __stdcall win_thread_function(void* arg);
  67. #if defined(WINVER) && (WINVER < 0x0500)
  68. friend BOOST_ASIO_DECL void __stdcall apc_function(ULONG);
  69. #else
  70. friend BOOST_ASIO_DECL void __stdcall apc_function(ULONG_PTR);
  71. #endif
  72. class func_base
  73. {
  74. public:
  75. virtual ~func_base() {}
  76. virtual void run() = 0;
  77. ::HANDLE entry_event_;
  78. ::HANDLE exit_event_;
  79. };
  80. struct auto_func_base_ptr
  81. {
  82. func_base* ptr;
  83. ~auto_func_base_ptr() { delete ptr; }
  84. };
  85. template <typename Function>
  86. class func
  87. : public func_base
  88. {
  89. public:
  90. func(Function f)
  91. : f_(f)
  92. {
  93. }
  94. virtual void run()
  95. {
  96. f_();
  97. }
  98. private:
  99. Function f_;
  100. };
  101. BOOST_ASIO_DECL void start_thread(func_base* arg, unsigned int stack_size);
  102. ::HANDLE thread_;
  103. ::HANDLE exit_event_;
  104. };
  105. } // namespace detail
  106. } // namespace asio
  107. } // namespace boost
  108. #include <boost/asio/detail/pop_options.hpp>
  109. #if defined(BOOST_ASIO_HEADER_ONLY)
  110. # include <boost/asio/detail/impl/win_thread.ipp>
  111. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  112. #endif // defined(BOOST_ASIO_WINDOWS)
  113. // && !defined(BOOST_ASIO_WINDOWS_APP)
  114. // && !defined(UNDER_CE)
  115. #endif // BOOST_ASIO_DETAIL_WIN_THREAD_HPP