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.

std_event.hpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // detail/std_event.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_STD_EVENT_HPP
  11. #define ASIO_DETAIL_STD_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_STD_MUTEX_AND_CONDVAR)
  17. #include <chrono>
  18. #include <condition_variable>
  19. #include "asio/detail/assert.hpp"
  20. #include "asio/detail/noncopyable.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. namespace detail {
  24. class std_event
  25. : private noncopyable
  26. {
  27. public:
  28. // Constructor.
  29. std_event()
  30. : state_(0)
  31. {
  32. }
  33. // Destructor.
  34. ~std_event()
  35. {
  36. }
  37. // Signal the event. (Retained for backward compatibility.)
  38. template <typename Lock>
  39. void signal(Lock& lock)
  40. {
  41. this->signal_all(lock);
  42. }
  43. // Signal all waiters.
  44. template <typename Lock>
  45. void signal_all(Lock& lock)
  46. {
  47. ASIO_ASSERT(lock.locked());
  48. (void)lock;
  49. state_ |= 1;
  50. cond_.notify_all();
  51. }
  52. // Unlock the mutex and signal one waiter.
  53. template <typename Lock>
  54. void unlock_and_signal_one(Lock& lock)
  55. {
  56. ASIO_ASSERT(lock.locked());
  57. state_ |= 1;
  58. bool have_waiters = (state_ > 1);
  59. lock.unlock();
  60. if (have_waiters)
  61. cond_.notify_one();
  62. }
  63. // If there's a waiter, unlock the mutex and signal it.
  64. template <typename Lock>
  65. bool maybe_unlock_and_signal_one(Lock& lock)
  66. {
  67. ASIO_ASSERT(lock.locked());
  68. state_ |= 1;
  69. if (state_ > 1)
  70. {
  71. lock.unlock();
  72. cond_.notify_one();
  73. return true;
  74. }
  75. return false;
  76. }
  77. // Reset the event.
  78. template <typename Lock>
  79. void clear(Lock& lock)
  80. {
  81. ASIO_ASSERT(lock.locked());
  82. (void)lock;
  83. state_ &= ~std::size_t(1);
  84. }
  85. // Wait for the event to become signalled.
  86. template <typename Lock>
  87. void wait(Lock& lock)
  88. {
  89. ASIO_ASSERT(lock.locked());
  90. unique_lock_adapter u_lock(lock);
  91. while ((state_ & 1) == 0)
  92. {
  93. waiter w(state_);
  94. cond_.wait(u_lock.unique_lock_);
  95. }
  96. }
  97. // Timed wait for the event to become signalled.
  98. template <typename Lock>
  99. bool wait_for_usec(Lock& lock, long usec)
  100. {
  101. ASIO_ASSERT(lock.locked());
  102. unique_lock_adapter u_lock(lock);
  103. if ((state_ & 1) == 0)
  104. {
  105. waiter w(state_);
  106. cond_.wait_for(u_lock.unique_lock_, std::chrono::microseconds(usec));
  107. }
  108. return (state_ & 1) != 0;
  109. }
  110. private:
  111. // Helper class to temporarily adapt a scoped_lock into a unique_lock so that
  112. // it can be passed to std::condition_variable::wait().
  113. struct unique_lock_adapter
  114. {
  115. template <typename Lock>
  116. explicit unique_lock_adapter(Lock& lock)
  117. : unique_lock_(lock.mutex().mutex_, std::adopt_lock)
  118. {
  119. }
  120. ~unique_lock_adapter()
  121. {
  122. unique_lock_.release();
  123. }
  124. std::unique_lock<std::mutex> unique_lock_;
  125. };
  126. // Helper to increment and decrement the state to track outstanding waiters.
  127. class waiter
  128. {
  129. public:
  130. explicit waiter(std::size_t& state)
  131. : state_(state)
  132. {
  133. state_ += 2;
  134. }
  135. ~waiter()
  136. {
  137. state_ -= 2;
  138. }
  139. private:
  140. std::size_t& state_;
  141. };
  142. std::condition_variable cond_;
  143. std::size_t state_;
  144. };
  145. } // namespace detail
  146. } // namespace asio
  147. #include "asio/detail/pop_options.hpp"
  148. #endif // defined(ASIO_HAS_STD_MUTEX_AND_CONDVAR)
  149. #endif // ASIO_DETAIL_STD_EVENT_HPP