include/boost/corosio/detail/conditionally_enabled_mutex.hpp
100.0% Lines (28/28)
100.0% List of functions (11/11)
57.1% Branches (8/14)
Functions (11)
Function
Calls
Lines
Branches
Blocks
boost::corosio::detail::conditionally_enabled_mutex::~conditionally_enabled_mutex()
:27
46306x
100.0%
–
100.0%
boost::corosio::detail::conditionally_enabled_mutex::conditionally_enabled_mutex(bool)
:33
46306x
100.0%
–
100.0%
boost::corosio::detail::conditionally_enabled_mutex::set_enabled(bool)
:46
15303x
100.0%
–
100.0%
boost::corosio::detail::conditionally_enabled_mutex::lock()
:52
210238x
100.0%
50.0%
100.0%
boost::corosio::detail::conditionally_enabled_mutex::unlock()
:53
210238x
100.0%
50.0%
100.0%
boost::corosio::detail::conditionally_enabled_mutex::scoped_lock::~scoped_lock()
:56
4027570x
100.0%
–
100.0%
boost::corosio::detail::conditionally_enabled_mutex::scoped_lock::scoped_lock(boost::corosio::detail::conditionally_enabled_mutex&)
:62
4027566x
100.0%
75.0%
100.0%
boost::corosio::detail::conditionally_enabled_mutex::scoped_lock::lock()
:73
1803518x
100.0%
50.0%
100.0%
boost::corosio::detail::conditionally_enabled_mutex::scoped_lock::unlock()
:79
1810553x
100.0%
50.0%
100.0%
boost::corosio::detail::conditionally_enabled_mutex::scoped_lock::owns_lock() const
:85
1809915x
100.0%
50.0%
100.0%
boost::corosio::detail::conditionally_enabled_mutex::scoped_lock::underlying()
:92
3x
100.0%
–
100.0%
| Line | Branch | 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 | 46306x | explicit conditionally_enabled_mutex(bool enabled = true) noexcept | ||
| 34 | 23153x | : enabled_(enabled) | ||
| 35 | 23153x | { | ||
| 36 | 46306x | } | ||
| 37 | ||||
| 38 | conditionally_enabled_mutex(conditionally_enabled_mutex const&) = delete; | |||
| 39 | conditionally_enabled_mutex& operator=(conditionally_enabled_mutex const&) = delete; | |||
| 40 | ||||
| 41 | bool enabled() const noexcept | |||
| 42 | { | |||
| 43 | return enabled_; | |||
| 44 | } | |||
| 45 | ||||
| 46 | 15303x | void set_enabled(bool v) noexcept | ||
| 47 | { | |||
| 48 | 15303x | enabled_ = v; | ||
| 49 | 15303x | } | ||
| 50 | ||||
| 51 | // Lockable interface — allows std::lock_guard<conditionally_enabled_mutex> | |||
| 52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 210238 times.
|
210238x | void lock() { if (enabled_) mutex_.lock(); } | |
| 53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 210238 times.
|
210238x | void unlock() { if (enabled_) mutex_.unlock(); } | |
| 54 | bool try_lock() { return !enabled_ || mutex_.try_lock(); } | |||
| 55 | ||||
| 56 | class scoped_lock | |||
| 57 | { | |||
| 58 | std::unique_lock<std::mutex> lock_; | |||
| 59 | bool enabled_; | |||
| 60 | ||||
| 61 | public: | |||
| 62 | 4027566x | explicit scoped_lock(conditionally_enabled_mutex& m) | ||
| 63 | 2013784x | : lock_(m.mutex_, std::defer_lock) | ||
| 64 | 2013784x | , enabled_(m.enabled_) | ||
| 65 | 2013782x | { | ||
| 66 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2013786 times.
|
2013784x | if (enabled_) | |
| 67 |
1/2✓ Branch 0 taken 2013786 times.
✗ Branch 1 not taken.
|
2013786x | lock_.lock(); | |
| 68 | 4027570x | } | ||
| 69 | ||||
| 70 | scoped_lock(scoped_lock const&) = delete; | |||
| 71 | scoped_lock& operator=(scoped_lock const&) = delete; | |||
| 72 | ||||
| 73 | 1803518x | void lock() | ||
| 74 | { | |||
| 75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1803518 times.
|
1803518x | if (enabled_) | |
| 76 | 1803518x | lock_.lock(); | ||
| 77 | 1803518x | } | ||
| 78 | ||||
| 79 | 1810553x | void unlock() | ||
| 80 | { | |||
| 81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1810553 times.
|
1810553x | if (enabled_) | |
| 82 | 1810553x | lock_.unlock(); | ||
| 83 | 1810553x | } | ||
| 84 | ||||
| 85 | 1809915x | bool owns_lock() const noexcept | ||
| 86 | { | |||
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1809915 times.
|
1809915x | return enabled_ && lock_.owns_lock(); | |
| 88 | } | |||
| 89 | ||||
| 90 | // Access the underlying unique_lock for condvar wait(). | |||
| 91 | // Only called when locking is enabled. | |||
| 92 | 3x | std::unique_lock<std::mutex>& underlying() noexcept | ||
| 93 | { | |||
| 94 | 3x | return lock_; | ||
| 95 | } | |||
| 96 | }; | |||
| 97 | }; | |||
| 98 | ||||
| 99 | } // namespace boost::corosio::detail | |||
| 100 | ||||
| 101 | #endif // BOOST_COROSIO_DETAIL_CONDITIONALLY_ENABLED_MUTEX_HPP | |||
| 102 |