conditionally_enabled_event.hpp 2.9 KB

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