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.

125 lines
2.5KB

  1. //
  2. // detail/winapp_thread.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_WINAPP_THREAD_HPP
  11. #define ASIO_DETAIL_WINAPP_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) && defined(ASIO_WINDOWS_APP)
  17. #include "asio/detail/noncopyable.hpp"
  18. #include "asio/detail/scoped_ptr.hpp"
  19. #include "asio/detail/socket_types.hpp"
  20. #include "asio/detail/throw_error.hpp"
  21. #include "asio/error.hpp"
  22. #include "asio/detail/push_options.hpp"
  23. namespace asio {
  24. namespace detail {
  25. DWORD WINAPI winapp_thread_function(LPVOID arg);
  26. class winapp_thread
  27. : private noncopyable
  28. {
  29. public:
  30. // Constructor.
  31. template <typename Function>
  32. winapp_thread(Function f, unsigned int = 0)
  33. {
  34. scoped_ptr<func_base> arg(new func<Function>(f));
  35. DWORD thread_id = 0;
  36. thread_ = ::CreateThread(0, 0, winapp_thread_function,
  37. arg.get(), 0, &thread_id);
  38. if (!thread_)
  39. {
  40. DWORD last_error = ::GetLastError();
  41. asio::error_code ec(last_error,
  42. asio::error::get_system_category());
  43. asio::detail::throw_error(ec, "thread");
  44. }
  45. arg.release();
  46. }
  47. // Destructor.
  48. ~winapp_thread()
  49. {
  50. ::CloseHandle(thread_);
  51. }
  52. // Wait for the thread to exit.
  53. void join()
  54. {
  55. ::WaitForSingleObjectEx(thread_, INFINITE, false);
  56. }
  57. // Get number of CPUs.
  58. static std::size_t hardware_concurrency()
  59. {
  60. SYSTEM_INFO system_info;
  61. ::GetNativeSystemInfo(&system_info);
  62. return system_info.dwNumberOfProcessors;
  63. }
  64. private:
  65. friend DWORD WINAPI winapp_thread_function(LPVOID arg);
  66. class func_base
  67. {
  68. public:
  69. virtual ~func_base() {}
  70. virtual void run() = 0;
  71. };
  72. template <typename Function>
  73. class func
  74. : public func_base
  75. {
  76. public:
  77. func(Function f)
  78. : f_(f)
  79. {
  80. }
  81. virtual void run()
  82. {
  83. f_();
  84. }
  85. private:
  86. Function f_;
  87. };
  88. ::HANDLE thread_;
  89. };
  90. inline DWORD WINAPI winapp_thread_function(LPVOID arg)
  91. {
  92. scoped_ptr<winapp_thread::func_base> func(
  93. static_cast<winapp_thread::func_base*>(arg));
  94. func->run();
  95. return 0;
  96. }
  97. } // namespace detail
  98. } // namespace asio
  99. #include "asio/detail/pop_options.hpp"
  100. #endif // defined(ASIO_WINDOWS) && defined(ASIO_WINDOWS_APP)
  101. #endif // ASIO_DETAIL_WINAPP_THREAD_HPP