2 // detail/conditionally_enabled_mutex.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2022 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_MUTEX_HPP
12 #define BOOST_ASIO_DETAIL_CONDITIONALLY_ENABLED_MUTEX_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/mutex.hpp>
20 #include <boost/asio/detail/noncopyable.hpp>
21 #include <boost/asio/detail/scoped_lock.hpp>
23 #include <boost/asio/detail/push_options.hpp>
29 // Mutex adapter used to conditionally enable or disable locking.
30 class conditionally_enabled_mutex
34 // Helper class to lock and unlock a mutex automatically.
39 // Tag type used to distinguish constructors.
40 enum adopt_lock_t { adopt_lock };
42 // Constructor adopts a lock that is already held.
43 scoped_lock(conditionally_enabled_mutex& m, adopt_lock_t)
49 // Constructor acquires the lock.
50 explicit scoped_lock(conditionally_enabled_mutex& m)
62 // Destructor releases the lock.
66 mutex_.mutex_.unlock();
69 // Explicitly acquire the lock.
72 if (mutex_.enabled_ && !locked_)
79 // Explicitly release the lock.
89 // Test whether the lock is held.
95 // Get the underlying mutex.
96 boost::asio::detail::mutex& mutex()
102 friend class conditionally_enabled_event;
103 conditionally_enabled_mutex& mutex_;
108 explicit conditionally_enabled_mutex(bool enabled)
114 ~conditionally_enabled_mutex()
118 // Determine whether locking is enabled.
139 friend class scoped_lock;
140 friend class conditionally_enabled_event;
141 boost::asio::detail::mutex mutex_;
145 } // namespace detail
149 #include <boost/asio/detail/pop_options.hpp>
151 #endif // BOOST_ASIO_DETAIL_CONDITIONALLY_ENABLED_MUTEX_HPP