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.

posix_thread.hpp 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // detail/posix_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_POSIX_THREAD_HPP
  11. #define ASIO_DETAIL_POSIX_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_HAS_PTHREADS)
  17. #include <cstddef>
  18. #include <pthread.h>
  19. #include "asio/detail/noncopyable.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace detail {
  23. extern "C"
  24. {
  25. ASIO_DECL void* asio_detail_posix_thread_function(void* arg);
  26. }
  27. class posix_thread
  28. : private noncopyable
  29. {
  30. public:
  31. // Constructor.
  32. template <typename Function>
  33. posix_thread(Function f, unsigned int = 0)
  34. : joined_(false)
  35. {
  36. start_thread(new func<Function>(f));
  37. }
  38. // Destructor.
  39. ASIO_DECL ~posix_thread();
  40. // Wait for the thread to exit.
  41. ASIO_DECL void join();
  42. // Get number of CPUs.
  43. ASIO_DECL static std::size_t hardware_concurrency();
  44. private:
  45. friend void* asio_detail_posix_thread_function(void* arg);
  46. class func_base
  47. {
  48. public:
  49. virtual ~func_base() {}
  50. virtual void run() = 0;
  51. };
  52. struct auto_func_base_ptr
  53. {
  54. func_base* ptr;
  55. ~auto_func_base_ptr() { delete ptr; }
  56. };
  57. template <typename Function>
  58. class func
  59. : public func_base
  60. {
  61. public:
  62. func(Function f)
  63. : f_(f)
  64. {
  65. }
  66. virtual void run()
  67. {
  68. f_();
  69. }
  70. private:
  71. Function f_;
  72. };
  73. ASIO_DECL void start_thread(func_base* arg);
  74. ::pthread_t thread_;
  75. bool joined_;
  76. };
  77. } // namespace detail
  78. } // namespace asio
  79. #include "asio/detail/pop_options.hpp"
  80. #if defined(ASIO_HEADER_ONLY)
  81. # include "asio/detail/impl/posix_thread.ipp"
  82. #endif // defined(ASIO_HEADER_ONLY)
  83. #endif // defined(ASIO_HAS_PTHREADS)
  84. #endif // ASIO_DETAIL_POSIX_THREAD_HPP