win_thread.hpp 3.2 KB

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