std_event.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // detail/std_event.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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. : signalled_(false)
  32. {
  33. }
  34. // Destructor.
  35. ~std_event()
  36. {
  37. }
  38. // Signal the event.
  39. template <typename Lock>
  40. void signal(Lock& lock)
  41. {
  42. BOOST_ASIO_ASSERT(lock.locked());
  43. (void)lock;
  44. signalled_ = true;
  45. cond_.notify_one();
  46. }
  47. // Signal the event and unlock the mutex.
  48. template <typename Lock>
  49. void signal_and_unlock(Lock& lock)
  50. {
  51. BOOST_ASIO_ASSERT(lock.locked());
  52. signalled_ = true;
  53. lock.unlock();
  54. cond_.notify_one();
  55. }
  56. // Reset the event.
  57. template <typename Lock>
  58. void clear(Lock& lock)
  59. {
  60. BOOST_ASIO_ASSERT(lock.locked());
  61. (void)lock;
  62. signalled_ = false;
  63. }
  64. // Wait for the event to become signalled.
  65. template <typename Lock>
  66. void wait(Lock& lock)
  67. {
  68. BOOST_ASIO_ASSERT(lock.locked());
  69. unique_lock_adapter u_lock(lock);
  70. while (!signalled_)
  71. cond_.wait(u_lock.unique_lock_);
  72. }
  73. // Timed wait for the event to become signalled.
  74. template <typename Lock>
  75. bool wait_for_usec(Lock& lock, long usec)
  76. {
  77. BOOST_ASIO_ASSERT(lock.locked());
  78. unique_lock_adapter u_lock(lock);
  79. if (!signalled_)
  80. cond_.wait_for(u_lock.unique_lock_, std::chrono::microseconds(usec));
  81. return signalled_;
  82. }
  83. private:
  84. // Helper class to temporarily adapt a scoped_lock into a unique_lock so that
  85. // it can be passed to std::condition_variable::wait().
  86. struct unique_lock_adapter
  87. {
  88. template <typename Lock>
  89. explicit unique_lock_adapter(Lock& lock)
  90. : unique_lock_(lock.mutex().mutex_, std::adopt_lock)
  91. {
  92. }
  93. ~unique_lock_adapter()
  94. {
  95. unique_lock_.release();
  96. }
  97. std::unique_lock<std::mutex> unique_lock_;
  98. };
  99. std::condition_variable cond_;
  100. bool signalled_;
  101. };
  102. } // namespace detail
  103. } // namespace asio
  104. } // namespace boost
  105. #include <boost/asio/detail/pop_options.hpp>
  106. #endif // defined(BOOST_ASIO_HAS_STD_MUTEX_AND_CONDVAR)
  107. #endif // BOOST_ASIO_DETAIL_STD_EVENT_HPP