include/boost/corosio/detail/conditionally_enabled_mutex.hpp
100.0% Lines (32/0/32)
100.0% List of functions (9/0/9)
Functions (9)
Function
Calls
Lines
Blocks
boost::corosio::detail::conditionally_enabled_mutex::conditionally_enabled_mutex(bool)
:33
19742x
100.0%
100.0%
boost::corosio::detail::conditionally_enabled_mutex::set_enabled(bool)
:47
12740x
100.0%
100.0%
boost::corosio::detail::conditionally_enabled_mutex::lock()
:53
69473x
100.0%
100.0%
boost::corosio::detail::conditionally_enabled_mutex::unlock()
:58
69473x
100.0%
100.0%
boost::corosio::detail::conditionally_enabled_mutex::scoped_lock::scoped_lock(boost::corosio::detail::conditionally_enabled_mutex&)
:74
875745x
100.0%
83.0%
boost::corosio::detail::conditionally_enabled_mutex::scoped_lock::lock()
:85
819806x
100.0%
100.0%
boost::corosio::detail::conditionally_enabled_mutex::scoped_lock::unlock()
:91
832906x
100.0%
100.0%
boost::corosio::detail::conditionally_enabled_mutex::scoped_lock::owns_lock() const
:97
829136x
100.0%
100.0%
boost::corosio::detail::conditionally_enabled_mutex::scoped_lock::underlying()
:104
40x
100.0%
100.0%
| Line | TLA | Hits | Source Code |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2026 Michael Vandeberg | ||
| 3 | // | ||
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 6 | // | ||
| 7 | // Official repository: https://github.com/cppalliance/corosio | ||
| 8 | // | ||
| 9 | |||
| 10 | #ifndef BOOST_COROSIO_DETAIL_CONDITIONALLY_ENABLED_MUTEX_HPP | ||
| 11 | #define BOOST_COROSIO_DETAIL_CONDITIONALLY_ENABLED_MUTEX_HPP | ||
| 12 | |||
| 13 | #include <mutex> | ||
| 14 | |||
| 15 | namespace boost::corosio::detail { | ||
| 16 | |||
| 17 | /* Mutex wrapper that becomes a no-op when disabled. | ||
| 18 | |||
| 19 | When enabled (the default), lock/unlock delegate to an | ||
| 20 | underlying std::mutex. When disabled, all operations are | ||
| 21 | no-ops. The enabled flag is fixed after construction. | ||
| 22 | |||
| 23 | scoped_lock wraps std::unique_lock<std::mutex> internally | ||
| 24 | so that condvar wait paths (which require the real lock | ||
| 25 | type) compile and work in multi-threaded mode. | ||
| 26 | */ | ||
| 27 | class conditionally_enabled_mutex | ||
| 28 | { | ||
| 29 | std::mutex mutex_; | ||
| 30 | bool enabled_; | ||
| 31 | |||
| 32 | public: | ||
| 33 | 19742x | explicit conditionally_enabled_mutex(bool enabled = true) noexcept | |
| 34 | 19742x | : enabled_(enabled) | |
| 35 | { | ||
| 36 | 19742x | } | |
| 37 | |||
| 38 | conditionally_enabled_mutex(conditionally_enabled_mutex const&) = delete; | ||
| 39 | conditionally_enabled_mutex& | ||
| 40 | operator=(conditionally_enabled_mutex const&) = delete; | ||
| 41 | |||
| 42 | bool enabled() const noexcept | ||
| 43 | { | ||
| 44 | return enabled_; | ||
| 45 | } | ||
| 46 | |||
| 47 | 12740x | void set_enabled(bool v) noexcept | |
| 48 | { | ||
| 49 | 12740x | enabled_ = v; | |
| 50 | 12740x | } | |
| 51 | |||
| 52 | // Lockable interface — allows std::lock_guard<conditionally_enabled_mutex> | ||
| 53 | 69473x | void lock() | |
| 54 | { | ||
| 55 | 69473x | if (enabled_) | |
| 56 | 69473x | mutex_.lock(); | |
| 57 | 69473x | } | |
| 58 | 69473x | void unlock() | |
| 59 | { | ||
| 60 | 69473x | if (enabled_) | |
| 61 | 69473x | mutex_.unlock(); | |
| 62 | 69473x | } | |
| 63 | bool try_lock() | ||
| 64 | { | ||
| 65 | return !enabled_ || mutex_.try_lock(); | ||
| 66 | } | ||
| 67 | |||
| 68 | class scoped_lock | ||
| 69 | { | ||
| 70 | std::unique_lock<std::mutex> lock_; | ||
| 71 | bool enabled_; | ||
| 72 | |||
| 73 | public: | ||
| 74 | 875745x | explicit scoped_lock(conditionally_enabled_mutex& m) | |
| 75 | 875745x | : lock_(m.mutex_, std::defer_lock) | |
| 76 | 875745x | , enabled_(m.enabled_) | |
| 77 | { | ||
| 78 | 875745x | if (enabled_) | |
| 79 | 875725x | lock_.lock(); | |
| 80 | 875745x | } | |
| 81 | |||
| 82 | scoped_lock(scoped_lock const&) = delete; | ||
| 83 | scoped_lock& operator=(scoped_lock const&) = delete; | ||
| 84 | |||
| 85 | 819806x | void lock() | |
| 86 | { | ||
| 87 | 819806x | if (enabled_) | |
| 88 | 819798x | lock_.lock(); | |
| 89 | 819806x | } | |
| 90 | |||
| 91 | 832906x | void unlock() | |
| 92 | { | ||
| 93 | 832906x | if (enabled_) | |
| 94 | 832898x | lock_.unlock(); | |
| 95 | 832906x | } | |
| 96 | |||
| 97 | 829136x | bool owns_lock() const noexcept | |
| 98 | { | ||
| 99 | 829136x | return enabled_ && lock_.owns_lock(); | |
| 100 | } | ||
| 101 | |||
| 102 | // Access the underlying unique_lock for condvar wait(). | ||
| 103 | // Only called when locking is enabled. | ||
| 104 | 40x | std::unique_lock<std::mutex>& underlying() noexcept | |
| 105 | { | ||
| 106 | 40x | return lock_; | |
| 107 | } | ||
| 108 | }; | ||
| 109 | }; | ||
| 110 | |||
| 111 | } // namespace boost::corosio::detail | ||
| 112 | |||
| 113 | #endif // BOOST_COROSIO_DETAIL_CONDITIONALLY_ENABLED_MUTEX_HPP | ||
| 114 |