std_event.hpp 4.0 KB

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