conditionally_enabled_event.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // detail/conditionally_enabled_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_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. // If there's a waiter, unlock the mutex and signal it.
  58. bool maybe_unlock_and_signal_one(
  59. conditionally_enabled_mutex::scoped_lock& lock)
  60. {
  61. if (lock.mutex_.enabled_)
  62. return event_.maybe_unlock_and_signal_one(lock);
  63. else
  64. return false;
  65. }
  66. // Reset the event.
  67. void clear(conditionally_enabled_mutex::scoped_lock& lock)
  68. {
  69. if (lock.mutex_.enabled_)
  70. event_.clear(lock);
  71. }
  72. // Wait for the event to become signalled.
  73. void wait(conditionally_enabled_mutex::scoped_lock& lock)
  74. {
  75. if (lock.mutex_.enabled_)
  76. event_.wait(lock);
  77. else
  78. null_event().wait(lock);
  79. }
  80. // Timed wait for the event to become signalled.
  81. bool wait_for_usec(
  82. conditionally_enabled_mutex::scoped_lock& lock, long usec)
  83. {
  84. if (lock.mutex_.enabled_)
  85. return event_.wait_for_usec(lock, usec);
  86. else
  87. return null_event().wait_for_usec(lock, usec);
  88. }
  89. private:
  90. boost::asio::detail::event event_;
  91. };
  92. } // namespace detail
  93. } // namespace asio
  94. } // namespace boost
  95. #include <boost/asio/detail/pop_options.hpp>
  96. #endif // BOOST_ASIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP