win_thread.hpp 3.0 KB

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