2 // detail/conditionally_enabled_event.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 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 // If there's a waiter, unlock the mutex and signal it.
69 bool maybe_unlock_and_signal_one(
70 conditionally_enabled_mutex::scoped_lock& lock)
72 if (lock.mutex_.enabled_)
73 return event_.maybe_unlock_and_signal_one(lock);
79 void clear(conditionally_enabled_mutex::scoped_lock& lock)
81 if (lock.mutex_.enabled_)
85 // Wait for the event to become signalled.
86 void wait(conditionally_enabled_mutex::scoped_lock& lock)
88 if (lock.mutex_.enabled_)
91 null_event().wait(lock);
94 // Timed wait for the event to become signalled.
96 conditionally_enabled_mutex::scoped_lock& lock, long usec)
98 if (lock.mutex_.enabled_)
99 return event_.wait_for_usec(lock, usec);
101 return null_event().wait_for_usec(lock, usec);
105 boost::asio::detail::event event_;
108 } // namespace detail
112 #include <boost/asio/detail/pop_options.hpp>
114 #endif // BOOST_ASIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP