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_event.hpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // detail/posix_event.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_POSIX_EVENT_HPP
  11. #define ASIO_DETAIL_POSIX_EVENT_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 <pthread.h>
  18. #include "asio/detail/assert.hpp"
  19. #include "asio/detail/noncopyable.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace detail {
  23. class posix_event
  24. : private noncopyable
  25. {
  26. public:
  27. // Constructor.
  28. ASIO_DECL posix_event();
  29. // Destructor.
  30. ~posix_event()
  31. {
  32. ::pthread_cond_destroy(&cond_);
  33. }
  34. // Signal the event. (Retained for backward compatibility.)
  35. template <typename Lock>
  36. void signal(Lock& lock)
  37. {
  38. this->signal_all(lock);
  39. }
  40. // Signal all waiters.
  41. template <typename Lock>
  42. void signal_all(Lock& lock)
  43. {
  44. ASIO_ASSERT(lock.locked());
  45. (void)lock;
  46. state_ |= 1;
  47. ::pthread_cond_broadcast(&cond_); // Ignore EINVAL.
  48. }
  49. // Unlock the mutex and signal one waiter.
  50. template <typename Lock>
  51. void unlock_and_signal_one(Lock& lock)
  52. {
  53. ASIO_ASSERT(lock.locked());
  54. state_ |= 1;
  55. bool have_waiters = (state_ > 1);
  56. lock.unlock();
  57. if (have_waiters)
  58. ::pthread_cond_signal(&cond_); // Ignore EINVAL.
  59. }
  60. // If there's a waiter, unlock the mutex and signal it.
  61. template <typename Lock>
  62. bool maybe_unlock_and_signal_one(Lock& lock)
  63. {
  64. ASIO_ASSERT(lock.locked());
  65. state_ |= 1;
  66. if (state_ > 1)
  67. {
  68. lock.unlock();
  69. ::pthread_cond_signal(&cond_); // Ignore EINVAL.
  70. return true;
  71. }
  72. return false;
  73. }
  74. // Reset the event.
  75. template <typename Lock>
  76. void clear(Lock& lock)
  77. {
  78. ASIO_ASSERT(lock.locked());
  79. (void)lock;
  80. state_ &= ~std::size_t(1);
  81. }
  82. // Wait for the event to become signalled.
  83. template <typename Lock>
  84. void wait(Lock& lock)
  85. {
  86. ASIO_ASSERT(lock.locked());
  87. while ((state_ & 1) == 0)
  88. {
  89. state_ += 2;
  90. ::pthread_cond_wait(&cond_, &lock.mutex().mutex_); // Ignore EINVAL.
  91. state_ -= 2;
  92. }
  93. }
  94. // Timed wait for the event to become signalled.
  95. template <typename Lock>
  96. bool wait_for_usec(Lock& lock, long usec)
  97. {
  98. ASIO_ASSERT(lock.locked());
  99. if ((state_ & 1) == 0)
  100. {
  101. state_ += 2;
  102. timespec ts;
  103. #if (defined(__MACH__) && defined(__APPLE__)) \
  104. || (defined(__ANDROID__) && (__ANDROID_API__ < 21) \
  105. && defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE))
  106. ts.tv_sec = usec / 1000000;
  107. ts.tv_nsec = (usec % 1000000) * 1000;
  108. ::pthread_cond_timedwait_relative_np(
  109. &cond_, &lock.mutex().mutex_, &ts); // Ignore EINVAL.
  110. #else // (defined(__MACH__) && defined(__APPLE__))
  111. // || (defined(__ANDROID__) && (__ANDROID_API__ < 21)
  112. // && defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE))
  113. if (::clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
  114. {
  115. ts.tv_sec += usec / 1000000;
  116. ts.tv_nsec += (usec % 1000000) * 1000;
  117. ts.tv_sec += ts.tv_nsec / 1000000000;
  118. ts.tv_nsec = ts.tv_nsec % 1000000000;
  119. ::pthread_cond_timedwait(&cond_,
  120. &lock.mutex().mutex_, &ts); // Ignore EINVAL.
  121. }
  122. #endif // (defined(__MACH__) && defined(__APPLE__))
  123. // || (defined(__ANDROID__) && (__ANDROID_API__ < 21)
  124. // && defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE))
  125. state_ -= 2;
  126. }
  127. return (state_ & 1) != 0;
  128. }
  129. private:
  130. ::pthread_cond_t cond_;
  131. std::size_t state_;
  132. };
  133. } // namespace detail
  134. } // namespace asio
  135. #include "asio/detail/pop_options.hpp"
  136. #if defined(ASIO_HEADER_ONLY)
  137. # include "asio/detail/impl/posix_event.ipp"
  138. #endif // defined(ASIO_HEADER_ONLY)
  139. #endif // defined(ASIO_HAS_PTHREADS)
  140. #endif // ASIO_DETAIL_POSIX_EVENT_HPP