conditionally_enabled_event.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // detail/conditionally_enabled_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_CONDITIONALLY_ENABLED_EVENT_HPP
  11. #define BOOST_ASIO_DETAIL_CONDITIONALLY_ENABLED_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. #include <boost/asio/detail/conditionally_enabled_mutex.hpp>
  17. #include <boost/asio/detail/event.hpp>
  18. #include <boost/asio/detail/noncopyable.hpp>
  19. #include <boost/asio/detail/null_event.hpp>
  20. #include <boost/asio/detail/scoped_lock.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. // Mutex adapter used to conditionally enable or disable locking.
  26. class conditionally_enabled_event
  27. : private noncopyable
  28. {
  29. public:
  30. // Constructor.
  31. conditionally_enabled_event()
  32. {
  33. }
  34. // Destructor.
  35. ~conditionally_enabled_event()
  36. {
  37. }
  38. // Signal the event. (Retained for backward compatibility.)
  39. void signal(conditionally_enabled_mutex::scoped_lock& lock)
  40. {
  41. if (lock.mutex_.enabled_)
  42. event_.signal(lock);
  43. }
  44. // Signal all waiters.
  45. void signal_all(conditionally_enabled_mutex::scoped_lock& lock)
  46. {
  47. if (lock.mutex_.enabled_)
  48. event_.signal_all(lock);
  49. }
  50. // Unlock the mutex and signal one waiter.
  51. void unlock_and_signal_one(
  52. conditionally_enabled_mutex::scoped_lock& lock)
  53. {
  54. if (lock.mutex_.enabled_)
  55. event_.unlock_and_signal_one(lock);
  56. }
  57. // Unlock the mutex and signal one waiter who may destroy us.
  58. void unlock_and_signal_one_for_destruction(
  59. conditionally_enabled_mutex::scoped_lock& lock)
  60. {
  61. if (lock.mutex_.enabled_)
  62. event_.unlock_and_signal_one(lock);
  63. }
  64. // If there's a waiter, unlock the mutex and signal it.
  65. bool maybe_unlock_and_signal_one(
  66. conditionally_enabled_mutex::scoped_lock& lock)
  67. {
  68. if (lock.mutex_.enabled_)
  69. return event_.maybe_unlock_and_signal_one(lock);
  70. else
  71. return false;
  72. }
  73. // Reset the event.
  74. void clear(conditionally_enabled_mutex::scoped_lock& lock)
  75. {
  76. if (lock.mutex_.enabled_)
  77. event_.clear(lock);
  78. }
  79. // Wait for the event to become signalled.
  80. void wait(conditionally_enabled_mutex::scoped_lock& lock)
  81. {
  82. if (lock.mutex_.enabled_)
  83. event_.wait(lock);
  84. else
  85. null_event().wait(lock);
  86. }
  87. // Timed wait for the event to become signalled.
  88. bool wait_for_usec(
  89. conditionally_enabled_mutex::scoped_lock& lock, long usec)
  90. {
  91. if (lock.mutex_.enabled_)
  92. return event_.wait_for_usec(lock, usec);
  93. else
  94. return null_event().wait_for_usec(lock, usec);
  95. }
  96. private:
  97. boost::asio::detail::event event_;
  98. };
  99. } // namespace detail
  100. } // namespace asio
  101. } // namespace boost
  102. #include <boost/asio/detail/pop_options.hpp>
  103. #endif // BOOST_ASIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP