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.

win_event.hpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // detail/win_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_WIN_EVENT_HPP
  11. #define ASIO_DETAIL_WIN_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_WINDOWS)
  17. #include "asio/detail/assert.hpp"
  18. #include "asio/detail/noncopyable.hpp"
  19. #include "asio/detail/socket_types.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace detail {
  23. class win_event
  24. : private noncopyable
  25. {
  26. public:
  27. // Constructor.
  28. ASIO_DECL win_event();
  29. // Destructor.
  30. ASIO_DECL ~win_event();
  31. // Signal the event. (Retained for backward compatibility.)
  32. template <typename Lock>
  33. void signal(Lock& lock)
  34. {
  35. this->signal_all(lock);
  36. }
  37. // Signal all waiters.
  38. template <typename Lock>
  39. void signal_all(Lock& lock)
  40. {
  41. ASIO_ASSERT(lock.locked());
  42. (void)lock;
  43. state_ |= 1;
  44. ::SetEvent(events_[0]);
  45. }
  46. // Unlock the mutex and signal one waiter.
  47. template <typename Lock>
  48. void unlock_and_signal_one(Lock& lock)
  49. {
  50. ASIO_ASSERT(lock.locked());
  51. state_ |= 1;
  52. bool have_waiters = (state_ > 1);
  53. lock.unlock();
  54. if (have_waiters)
  55. ::SetEvent(events_[1]);
  56. }
  57. // If there's a waiter, unlock the mutex and signal it.
  58. template <typename Lock>
  59. bool maybe_unlock_and_signal_one(Lock& lock)
  60. {
  61. ASIO_ASSERT(lock.locked());
  62. state_ |= 1;
  63. if (state_ > 1)
  64. {
  65. lock.unlock();
  66. ::SetEvent(events_[1]);
  67. return true;
  68. }
  69. return false;
  70. }
  71. // Reset the event.
  72. template <typename Lock>
  73. void clear(Lock& lock)
  74. {
  75. ASIO_ASSERT(lock.locked());
  76. (void)lock;
  77. ::ResetEvent(events_[0]);
  78. state_ &= ~std::size_t(1);
  79. }
  80. // Wait for the event to become signalled.
  81. template <typename Lock>
  82. void wait(Lock& lock)
  83. {
  84. ASIO_ASSERT(lock.locked());
  85. while ((state_ & 1) == 0)
  86. {
  87. state_ += 2;
  88. lock.unlock();
  89. #if defined(ASIO_WINDOWS_APP)
  90. ::WaitForMultipleObjectsEx(2, events_, false, INFINITE, false);
  91. #else // defined(ASIO_WINDOWS_APP)
  92. ::WaitForMultipleObjects(2, events_, false, INFINITE);
  93. #endif // defined(ASIO_WINDOWS_APP)
  94. lock.lock();
  95. state_ -= 2;
  96. }
  97. }
  98. // Timed wait for the event to become signalled.
  99. template <typename Lock>
  100. bool wait_for_usec(Lock& lock, long usec)
  101. {
  102. ASIO_ASSERT(lock.locked());
  103. if ((state_ & 1) == 0)
  104. {
  105. state_ += 2;
  106. lock.unlock();
  107. DWORD msec = usec > 0 ? (usec < 1000 ? 1 : usec / 1000) : 0;
  108. #if defined(ASIO_WINDOWS_APP)
  109. ::WaitForMultipleObjectsEx(2, events_, false, msec, false);
  110. #else // defined(ASIO_WINDOWS_APP)
  111. ::WaitForMultipleObjects(2, events_, false, msec);
  112. #endif // defined(ASIO_WINDOWS_APP)
  113. lock.lock();
  114. state_ -= 2;
  115. }
  116. return (state_ & 1) != 0;
  117. }
  118. private:
  119. HANDLE events_[2];
  120. std::size_t state_;
  121. };
  122. } // namespace detail
  123. } // namespace asio
  124. #include "asio/detail/pop_options.hpp"
  125. #if defined(ASIO_HEADER_ONLY)
  126. # include "asio/detail/impl/win_event.ipp"
  127. #endif // defined(ASIO_HEADER_ONLY)
  128. #endif // defined(ASIO_WINDOWS)
  129. #endif // ASIO_DETAIL_WIN_EVENT_HPP