Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.0KB

  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 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/config.hpp"
  16. #if defined(ASIO_WINDOWS) \
  17. && !defined(ASIO_WINDOWS_APP) \
  18. && !defined(UNDER_CE)
  19. #include <cstddef>
  20. #include "asio/detail/noncopyable.hpp"
  21. #include "asio/detail/socket_types.hpp"
  22. #include "asio/detail/push_options.hpp"
  23. namespace asio {
  24. namespace detail {
  25. ASIO_DECL unsigned int __stdcall win_thread_function(void* arg);
  26. #if defined(WINVER) && (WINVER < 0x0500)
  27. ASIO_DECL void __stdcall apc_function(ULONG data);
  28. #else
  29. 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. ASIO_DECL ~win_thread();
  63. // Wait for the thread to exit.
  64. ASIO_DECL void join();
  65. // Get number of CPUs.
  66. ASIO_DECL static std::size_t hardware_concurrency();
  67. private:
  68. friend ASIO_DECL unsigned int __stdcall win_thread_function(void* arg);
  69. #if defined(WINVER) && (WINVER < 0x0500)
  70. friend ASIO_DECL void __stdcall apc_function(ULONG);
  71. #else
  72. friend ASIO_DECL void __stdcall apc_function(ULONG_PTR);
  73. #endif
  74. class func_base
  75. {
  76. public:
  77. virtual ~func_base() {}
  78. virtual void run() = 0;
  79. ::HANDLE entry_event_;
  80. ::HANDLE exit_event_;
  81. };
  82. struct auto_func_base_ptr
  83. {
  84. func_base* ptr;
  85. ~auto_func_base_ptr() { delete ptr; }
  86. };
  87. template <typename Function>
  88. class func
  89. : public func_base
  90. {
  91. public:
  92. func(Function f)
  93. : f_(f)
  94. {
  95. }
  96. virtual void run()
  97. {
  98. f_();
  99. }
  100. private:
  101. Function f_;
  102. };
  103. ASIO_DECL void start_thread(func_base* arg, unsigned int stack_size);
  104. ::HANDLE thread_;
  105. ::HANDLE exit_event_;
  106. };
  107. } // namespace detail
  108. } // namespace asio
  109. #include "asio/detail/pop_options.hpp"
  110. #if defined(ASIO_HEADER_ONLY)
  111. # include "asio/detail/impl/win_thread.ipp"
  112. #endif // defined(ASIO_HEADER_ONLY)
  113. #endif // defined(ASIO_WINDOWS)
  114. // && !defined(ASIO_WINDOWS_APP)
  115. // && !defined(UNDER_CE)
  116. #endif // ASIO_DETAIL_WIN_THREAD_HPP