win_event.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // detail/win_event.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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 <boost/asio/detail/assert.hpp>
  18. #include <boost/asio/detail/noncopyable.hpp>
  19. #include <boost/asio/detail/socket_types.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. class win_event
  25. : private noncopyable
  26. {
  27. public:
  28. // Constructor.
  29. BOOST_ASIO_DECL win_event();
  30. // Destructor.
  31. BOOST_ASIO_DECL ~win_event();
  32. // Signal the event. (Retained for backward compatibility.)
  33. template <typename Lock>
  34. void signal(Lock& lock)
  35. {
  36. this->signal_all(lock);
  37. }
  38. // Signal all waiters.
  39. template <typename Lock>
  40. void signal_all(Lock& lock)
  41. {
  42. BOOST_ASIO_ASSERT(lock.locked());
  43. (void)lock;
  44. state_ |= 1;
  45. ::SetEvent(events_[0]);
  46. }
  47. // Unlock the mutex and signal one waiter.
  48. template <typename Lock>
  49. void unlock_and_signal_one(Lock& lock)
  50. {
  51. BOOST_ASIO_ASSERT(lock.locked());
  52. state_ |= 1;
  53. bool have_waiters = (state_ > 1);
  54. lock.unlock();
  55. if (have_waiters)
  56. ::SetEvent(events_[1]);
  57. }
  58. // If there's a waiter, unlock the mutex and signal it.
  59. template <typename Lock>
  60. bool maybe_unlock_and_signal_one(Lock& lock)
  61. {
  62. BOOST_ASIO_ASSERT(lock.locked());
  63. state_ |= 1;
  64. if (state_ > 1)
  65. {
  66. lock.unlock();
  67. ::SetEvent(events_[1]);
  68. return true;
  69. }
  70. return false;
  71. }
  72. // Reset the event.
  73. template <typename Lock>
  74. void clear(Lock& lock)
  75. {
  76. BOOST_ASIO_ASSERT(lock.locked());
  77. (void)lock;
  78. ::ResetEvent(events_[0]);
  79. state_ &= ~std::size_t(1);
  80. }
  81. // Wait for the event to become signalled.
  82. template <typename Lock>
  83. void wait(Lock& lock)
  84. {
  85. BOOST_ASIO_ASSERT(lock.locked());
  86. while ((state_ & 1) == 0)
  87. {
  88. state_ += 2;
  89. lock.unlock();
  90. #if defined(BOOST_ASIO_WINDOWS_APP)
  91. ::WaitForMultipleObjectsEx(2, events_, false, INFINITE, false);
  92. #else // defined(BOOST_ASIO_WINDOWS_APP)
  93. ::WaitForMultipleObjects(2, events_, false, INFINITE);
  94. #endif // defined(BOOST_ASIO_WINDOWS_APP)
  95. lock.lock();
  96. state_ -= 2;
  97. }
  98. }
  99. private:
  100. HANDLE events_[2];
  101. std::size_t state_;
  102. };
  103. } // namespace detail
  104. } // namespace asio
  105. } // namespace boost
  106. #include <boost/asio/detail/pop_options.hpp>
  107. #if defined(BOOST_ASIO_HEADER_ONLY)
  108. # include <boost/asio/detail/impl/win_event.ipp>
  109. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  110. #endif // defined(BOOST_ASIO_WINDOWS)
  111. #endif // BOOST_ASIO_DETAIL_WIN_EVENT_HPP