include/boost/corosio/native/detail/iocp/win_mutex.hpp
100.0% Lines (13/13)
100.0% Functions (4/4)
-% Branches (0/0)
| Line | 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 | Satisfies the Lockable named requirement and is compatible | ||
| 31 | with `std::lock_guard`, `std::unique_lock`, and `std::scoped_lock`. | ||
| 32 | */ | ||
| 33 | class win_mutex | ||
| 34 | { | ||
| 35 | public: | ||
| 36 | 1160 | win_mutex() | |
| 37 | 1160 | { | |
| 38 | 1160 | ::InitializeCriticalSectionAndSpinCount(&cs_, 0x80000000); | |
| 39 | 1160 | } | |
| 40 | |||
| 41 | 1160 | ~win_mutex() | |
| 42 | { | ||
| 43 | 1160 | ::DeleteCriticalSection(&cs_); | |
| 44 | 1160 | } | |
| 45 | |||
| 46 | win_mutex(win_mutex const&) = delete; | ||
| 47 | win_mutex& operator=(win_mutex const&) = delete; | ||
| 48 | |||
| 49 | 22253 | void lock() noexcept | |
| 50 | { | ||
| 51 | 22253 | ::EnterCriticalSection(&cs_); | |
| 52 | 22253 | } | |
| 53 | |||
| 54 | 22253 | void unlock() noexcept | |
| 55 | { | ||
| 56 | 22253 | ::LeaveCriticalSection(&cs_); | |
| 57 | 22253 | } | |
| 58 | |||
| 59 | bool try_lock() noexcept | ||
| 60 | { | ||
| 61 | return ::TryEnterCriticalSection(&cs_) != 0; | ||
| 62 | } | ||
| 63 | |||
| 64 | private: | ||
| 65 | ::CRITICAL_SECTION cs_; | ||
| 66 | }; | ||
| 67 | |||
| 68 | } // namespace boost::corosio::detail | ||
| 69 | |||
| 70 | #endif // BOOST_COROSIO_HAS_IOCP | ||
| 71 | |||
| 72 | #endif // BOOST_COROSIO_NATIVE_DETAIL_IOCP_WIN_MUTEX_HPP | ||
| 73 |