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