win_event.hpp 3.8 KB

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