2 // detail/conditionally_enabled_event.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP
12 #define BOOST_ASIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/conditionally_enabled_mutex.hpp>
20 #include <boost/asio/detail/event.hpp>
21 #include <boost/asio/detail/noncopyable.hpp>
22 #include <boost/asio/detail/null_event.hpp>
23 #include <boost/asio/detail/scoped_lock.hpp>
25 #include <boost/asio/detail/push_options.hpp>
31 // Mutex adapter used to conditionally enable or disable locking.
32 class conditionally_enabled_event
37 conditionally_enabled_event()
42 ~conditionally_enabled_event()
46 // Signal the event. (Retained for backward compatibility.)
47 void signal(conditionally_enabled_mutex::scoped_lock& lock)
49 if (lock.mutex_.enabled_)
53 // Signal all waiters.
54 void signal_all(conditionally_enabled_mutex::scoped_lock& lock)
56 if (lock.mutex_.enabled_)
57 event_.signal_all(lock);
60 // Unlock the mutex and signal one waiter.
61 void unlock_and_signal_one(
62 conditionally_enabled_mutex::scoped_lock& lock)
64 if (lock.mutex_.enabled_)
65 event_.unlock_and_signal_one(lock);
68 // Unlock the mutex and signal one waiter who may destroy us.
69 void unlock_and_signal_one_for_destruction(
70 conditionally_enabled_mutex::scoped_lock& lock)
72 if (lock.mutex_.enabled_)
73 event_.unlock_and_signal_one(lock);
76 // If there's a waiter, unlock the mutex and signal it.
77 bool maybe_unlock_and_signal_one(
78 conditionally_enabled_mutex::scoped_lock& lock)
80 if (lock.mutex_.enabled_)
81 return event_.maybe_unlock_and_signal_one(lock);
87 void clear(conditionally_enabled_mutex::scoped_lock& lock)
89 if (lock.mutex_.enabled_)
93 // Wait for the event to become signalled.
94 void wait(conditionally_enabled_mutex::scoped_lock& lock)
96 if (lock.mutex_.enabled_)
99 null_event().wait(lock);
102 // Timed wait for the event to become signalled.
104 conditionally_enabled_mutex::scoped_lock& lock, long usec)
106 if (lock.mutex_.enabled_)
107 return event_.wait_for_usec(lock, usec);
109 return null_event().wait_for_usec(lock, usec);
113 boost::asio::detail::event event_;
116 } // namespace detail
120 #include <boost/asio/detail/pop_options.hpp>
122 #endif // BOOST_ASIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP