100.00% Lines (32/32)
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 | 19468 | explicit conditionally_enabled_mutex(bool enabled = true) noexcept | 33 | 19543 | explicit conditionally_enabled_mutex(bool enabled = true) noexcept | ||
| HITCBC | 34 | 19468 | : enabled_(enabled) | 34 | 19543 | : enabled_(enabled) | ||
| 35 | { | 35 | { | |||||
| HITCBC | 36 | 19468 | } | 36 | 19543 | } | ||
| 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& | |||||
| 40 | operator=(conditionally_enabled_mutex const&) = delete; | 40 | operator=(conditionally_enabled_mutex const&) = delete; | |||||
| 41 | 41 | |||||||
| 42 | bool enabled() const noexcept | 42 | bool enabled() const noexcept | |||||
| 43 | { | 43 | { | |||||
| 44 | return enabled_; | 44 | return enabled_; | |||||
| 45 | } | 45 | } | |||||
| 46 | 46 | |||||||
| HITCBC | 47 | 12557 | void set_enabled(bool v) noexcept | 47 | 12607 | void set_enabled(bool v) noexcept | ||
| 48 | { | 48 | { | |||||
| HITCBC | 49 | 12557 | enabled_ = v; | 49 | 12607 | enabled_ = v; | ||
| HITCBC | 50 | 12557 | } | 50 | 12607 | } | ||
| 51 | 51 | |||||||
| 52 | // Lockable interface — allows std::lock_guard<conditionally_enabled_mutex> | 52 | // Lockable interface — allows std::lock_guard<conditionally_enabled_mutex> | |||||
| HITCBC | 53 | 68280 | void lock() | 53 | 68609 | void lock() | ||
| 54 | { | 54 | { | |||||
| HITCBC | 55 | 68280 | if (enabled_) | 55 | 68609 | if (enabled_) | ||
| HITCBC | 56 | 68280 | mutex_.lock(); | 56 | 68609 | mutex_.lock(); | ||
| HITCBC | 57 | 68280 | } | 57 | 68609 | } | ||
| HITCBC | 58 | 68280 | void unlock() | 58 | 68609 | void unlock() | ||
| 59 | { | 59 | { | |||||
| HITCBC | 60 | 68280 | if (enabled_) | 60 | 68609 | if (enabled_) | ||
| HITCBC | 61 | 68280 | mutex_.unlock(); | 61 | 68609 | mutex_.unlock(); | ||
| HITCBC | 62 | 68280 | } | 62 | 68609 | } | ||
| 63 | bool try_lock() | 63 | bool try_lock() | |||||
| 64 | { | 64 | { | |||||
| 65 | return !enabled_ || mutex_.try_lock(); | 65 | return !enabled_ || mutex_.try_lock(); | |||||
| 66 | } | 66 | } | |||||
| 67 | 67 | |||||||
| 68 | class scoped_lock | 68 | class scoped_lock | |||||
| 69 | { | 69 | { | |||||
| 70 | std::unique_lock<std::mutex> lock_; | 70 | std::unique_lock<std::mutex> lock_; | |||||
| 71 | bool enabled_; | 71 | bool enabled_; | |||||
| 72 | 72 | |||||||
| 73 | public: | 73 | public: | |||||
| HITCBC | 74 | 857095 | explicit scoped_lock(conditionally_enabled_mutex& m) | 74 | 880127 | explicit scoped_lock(conditionally_enabled_mutex& m) | ||
| HITCBC | 75 | 857095 | : lock_(m.mutex_, std::defer_lock) | 75 | 880127 | : lock_(m.mutex_, std::defer_lock) | ||
| HITCBC | 76 | 857095 | , enabled_(m.enabled_) | 76 | 880127 | , enabled_(m.enabled_) | ||
| 77 | { | 77 | { | |||||
| HITCBC | 78 | 857095 | if (enabled_) | 78 | 880127 | if (enabled_) | ||
| HITCBC | 79 | 857075 | lock_.lock(); | 79 | 880107 | lock_.lock(); | ||
| HITCBC | 80 | 857095 | } | 80 | 880127 | } | ||
| 81 | 81 | |||||||
| 82 | scoped_lock(scoped_lock const&) = delete; | 82 | scoped_lock(scoped_lock const&) = delete; | |||||
| 83 | scoped_lock& operator=(scoped_lock const&) = delete; | 83 | scoped_lock& operator=(scoped_lock const&) = delete; | |||||
| 84 | 84 | |||||||
| HITCBC | 85 | 806006 | void lock() | 85 | 833904 | void lock() | ||
| 86 | { | 86 | { | |||||
| HITCBC | 87 | 806006 | if (enabled_) | 87 | 833904 | if (enabled_) | ||
| HITCBC | 88 | 805998 | lock_.lock(); | 88 | 833896 | lock_.lock(); | ||
| HITCBC | 89 | 806006 | } | 89 | 833904 | } | ||
| 90 | 90 | |||||||
| HITCBC | 91 | 819394 | void unlock() | 91 | 847285 | void unlock() | ||
| 92 | { | 92 | { | |||||
| HITCBC | 93 | 819394 | if (enabled_) | 93 | 847285 | if (enabled_) | ||
| HITCBC | 94 | 819386 | lock_.unlock(); | 94 | 847277 | lock_.unlock(); | ||
| HITCBC | 95 | 819394 | } | 95 | 847285 | } | ||
| 96 | 96 | |||||||
| HITCBC | 97 | 814947 | bool owns_lock() const noexcept | 97 | 842954 | bool owns_lock() const noexcept | ||
| 98 | { | 98 | { | |||||
| HITCBC | 99 | 814947 | return enabled_ && lock_.owns_lock(); | 99 | 842954 | return enabled_ && lock_.owns_lock(); | ||
| 100 | } | 100 | } | |||||
| 101 | 101 | |||||||
| 102 | // Access the underlying unique_lock for condvar wait(). | 102 | // Access the underlying unique_lock for condvar wait(). | |||||
| 103 | // Only called when locking is enabled. | 103 | // Only called when locking is enabled. | |||||
| HITCBC | 104 | 58 | std::unique_lock<std::mutex>& underlying() noexcept | 104 | 57 | std::unique_lock<std::mutex>& underlying() noexcept | ||
| 105 | { | 105 | { | |||||
| HITCBC | 106 | 58 | return lock_; | 106 | 57 | return lock_; | ||
| 107 | } | 107 | } | |||||
| 108 | }; | 108 | }; | |||||
| 109 | }; | 109 | }; | |||||
| 110 | 110 | |||||||
| 111 | } // namespace boost::corosio::detail | 111 | } // namespace boost::corosio::detail | |||||
| 112 | 112 | |||||||
| 113 | #endif // BOOST_COROSIO_DETAIL_CONDITIONALLY_ENABLED_MUTEX_HPP | 113 | #endif // BOOST_COROSIO_DETAIL_CONDITIONALLY_ENABLED_MUTEX_HPP | |||||