std_event.hpp 3.6 KB

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