std_event.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // detail/std_event.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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 BOOST_ASIO_DETAIL_STD_EVENT_HPP
  11. #define BOOST_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 <boost/asio/detail/config.hpp>
  16. #include <chrono>
  17. #include <condition_variable>
  18. #include <boost/asio/detail/assert.hpp>
  19. #include <boost/asio/detail/noncopyable.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  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. BOOST_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. BOOST_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. // Unlock the mutex and signal one waiter who may destroy us.
  64. template <typename Lock>
  65. void unlock_and_signal_one_for_destruction(Lock& lock)
  66. {
  67. BOOST_ASIO_ASSERT(lock.locked());
  68. state_ |= 1;
  69. bool have_waiters = (state_ > 1);
  70. if (have_waiters)
  71. cond_.notify_one();
  72. lock.unlock();
  73. }
  74. // If there's a waiter, unlock the mutex and signal it.
  75. template <typename Lock>
  76. bool maybe_unlock_and_signal_one(Lock& lock)
  77. {
  78. BOOST_ASIO_ASSERT(lock.locked());
  79. state_ |= 1;
  80. if (state_ > 1)
  81. {
  82. lock.unlock();
  83. cond_.notify_one();
  84. return true;
  85. }
  86. return false;
  87. }
  88. // Reset the event.
  89. template <typename Lock>
  90. void clear(Lock& lock)
  91. {
  92. BOOST_ASIO_ASSERT(lock.locked());
  93. (void)lock;
  94. state_ &= ~std::size_t(1);
  95. }
  96. // Wait for the event to become signalled.
  97. template <typename Lock>
  98. void wait(Lock& lock)
  99. {
  100. BOOST_ASIO_ASSERT(lock.locked());
  101. unique_lock_adapter u_lock(lock);
  102. while ((state_ & 1) == 0)
  103. {
  104. waiter w(state_);
  105. cond_.wait(u_lock.unique_lock_);
  106. }
  107. }
  108. // Timed wait for the event to become signalled.
  109. template <typename Lock>
  110. bool wait_for_usec(Lock& lock, long usec)
  111. {
  112. BOOST_ASIO_ASSERT(lock.locked());
  113. unique_lock_adapter u_lock(lock);
  114. if ((state_ & 1) == 0)
  115. {
  116. waiter w(state_);
  117. cond_.wait_for(u_lock.unique_lock_, std::chrono::microseconds(usec));
  118. }
  119. return (state_ & 1) != 0;
  120. }
  121. private:
  122. // Helper class to temporarily adapt a scoped_lock into a unique_lock so that
  123. // it can be passed to std::condition_variable::wait().
  124. struct unique_lock_adapter
  125. {
  126. template <typename Lock>
  127. explicit unique_lock_adapter(Lock& lock)
  128. : unique_lock_(lock.mutex().mutex_, std::adopt_lock)
  129. {
  130. }
  131. ~unique_lock_adapter()
  132. {
  133. unique_lock_.release();
  134. }
  135. std::unique_lock<std::mutex> unique_lock_;
  136. };
  137. // Helper to increment and decrement the state to track outstanding waiters.
  138. class waiter
  139. {
  140. public:
  141. explicit waiter(std::size_t& state)
  142. : state_(state)
  143. {
  144. state_ += 2;
  145. }
  146. ~waiter()
  147. {
  148. state_ -= 2;
  149. }
  150. private:
  151. std::size_t& state_;
  152. };
  153. std::condition_variable cond_;
  154. std::size_t state_;
  155. };
  156. } // namespace detail
  157. } // namespace asio
  158. } // namespace boost
  159. #include <boost/asio/detail/pop_options.hpp>
  160. #endif // BOOST_ASIO_DETAIL_STD_EVENT_HPP