include/boost/corosio/native/detail/iocp/win_mutex.hpp
93.8% Lines (15/16)
80.0% List of functions (4/5)
50.0% Branches (2/4)
Functions (5)
Function
Calls
Lines
Branches
Blocks
boost::corosio::detail::win_mutex::win_mutex()
:40
3041x
100.0%
–
100.0%
boost::corosio::detail::win_mutex::~win_mutex()
:45
3041x
100.0%
–
100.0%
boost::corosio::detail::win_mutex::set_enabled(bool)
:53
0
0.0%
–
0.0%
boost::corosio::detail::win_mutex::lock()
:56
23519x
100.0%
50.0%
100.0%
boost::corosio::detail::win_mutex::unlock()
:62
23519x
100.0%
50.0%
100.0%
| Line | Branch | TLA | Hits | Source Code |
|---|---|---|---|---|
| 1 | // | |||
| 2 | // Copyright (c) 2025 Vinnie Falco ([email protected]) | |||
| 3 | // Copyright (c) 2026 Steve Gerbino | |||
| 4 | // | |||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||
| 7 | // | |||
| 8 | // Official repository: https://github.com/cppalliance/corosio | |||
| 9 | // | |||
| 10 | ||||
| 11 | #ifndef BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_MUTEX_HPP | |||
| 12 | #define BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_MUTEX_HPP | |||
| 13 | ||||
| 14 | #include <boost/corosio/detail/platform.hpp> | |||
| 15 | ||||
| 16 | #if BOOST_COROSIO_HAS_IOCP | |||
| 17 | ||||
| 18 | #include <boost/corosio/detail/config.hpp> | |||
| 19 | ||||
| 20 | #include <boost/corosio/native/detail/iocp/win_windows.hpp> | |||
| 21 | ||||
| 22 | namespace boost::corosio::detail { | |||
| 23 | ||||
| 24 | /** Recursive mutex using Windows CRITICAL_SECTION. | |||
| 25 | ||||
| 26 | This mutex can be locked multiple times by the same thread. | |||
| 27 | Each call to `lock()` or successful `try_lock()` must be | |||
| 28 | balanced by a corresponding call to `unlock()`. | |||
| 29 | ||||
| 30 | When disabled via `set_enabled(false)`, all locking operations | |||
| 31 | become no-ops. This supports single-threaded (lockless) mode | |||
| 32 | where cross-thread posting is undefined behavior. | |||
| 33 | ||||
| 34 | Satisfies the Lockable named requirement and is compatible | |||
| 35 | with `std::lock_guard`, `std::unique_lock`, and `std::scoped_lock`. | |||
| 36 | */ | |||
| 37 | class win_mutex | |||
| 38 | { | |||
| 39 | public: | |||
| 40 | 3041x | win_mutex() | ||
| 41 | 3041x | { | ||
| 42 | 3041x | ::InitializeCriticalSectionAndSpinCount(&cs_, 0x80000000); | ||
| 43 | 3041x | } | ||
| 44 | ||||
| 45 | 3041x | ~win_mutex() | ||
| 46 | { | |||
| 47 | 3041x | ::DeleteCriticalSection(&cs_); | ||
| 48 | 3041x | } | ||
| 49 | ||||
| 50 | win_mutex(win_mutex const&) = delete; | |||
| 51 | win_mutex& operator=(win_mutex const&) = delete; | |||
| 52 | ||||
| 53 | ✗ | void set_enabled(bool v) noexcept { enabled_ = v; } | ||
| 54 | bool enabled() const noexcept { return enabled_; } | |||
| 55 | ||||
| 56 | 23519x | void lock() noexcept | ||
| 57 | { | |||
| 58 |
1/2✓ Branch 2 → 3 taken 23519 times.
✗ Branch 2 → 4 not taken.
|
23519x | if (enabled_) | |
| 59 | 23519x | ::EnterCriticalSection(&cs_); | ||
| 60 | 23519x | } | ||
| 61 | ||||
| 62 | 23519x | void unlock() noexcept | ||
| 63 | { | |||
| 64 |
1/2✓ Branch 2 → 3 taken 23519 times.
✗ Branch 2 → 4 not taken.
|
23519x | if (enabled_) | |
| 65 | 23519x | ::LeaveCriticalSection(&cs_); | ||
| 66 | 23519x | } | ||
| 67 | ||||
| 68 | bool try_lock() noexcept | |||
| 69 | { | |||
| 70 | return !enabled_ || ::TryEnterCriticalSection(&cs_) != 0; | |||
| 71 | } | |||
| 72 | ||||
| 73 | private: | |||
| 74 | ::CRITICAL_SECTION cs_; | |||
| 75 | bool enabled_ = true; | |||
| 76 | }; | |||
| 77 | ||||
| 78 | } // namespace boost::corosio::detail | |||
| 79 | ||||
| 80 | #endif // BOOST_COROSIO_HAS_IOCP | |||
| 81 | ||||
| 82 | #endif // BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_MUTEX_HPP | |||
| 83 |