std_event.hpp 3.7 KB

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