1#ifndef BOOST_SYSTEM_DETAIL_MUTEX_HPP_INCLUDED
2#define BOOST_SYSTEM_DETAIL_MUTEX_HPP_INCLUDED
3
4// Copyright 2023 Peter Dimov
5// Distributed under the Boost Software License, Version 1.0.
6// https://www.boost.org/LICENSE_1_0.txt)
7
8#include <boost/config.hpp>
9
10#if defined(BOOST_SYSTEM_DISABLE_THREADS)
11
12namespace boost
13{
14namespace system
15{
16namespace detail
17{
18
19struct mutex
20{
21 void lock()
22 {
23 }
24
25 void unlock()
26 {
27 }
28};
29
30} // namespace detail
31} // namespace system
32} // namespace boost
33
34#else // defined(BOOST_SYSTEM_DISABLE_THREADS)
35
36#if defined(BOOST_MSSTL_VERSION) && BOOST_MSSTL_VERSION >= 140
37
38// Under the MS STL, std::mutex::mutex() is not constexpr, as is
39// required by the standard, which leads to initialization order
40// issues. However, shared_mutex is based on SRWLock and its
41// default constructor is constexpr, so we use that instead.
42
43#include <boost/winapi/config.hpp>
44
45// SRWLOCK is not available when targeting Windows XP
46#if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
47
48#include <shared_mutex>
49
50#if BOOST_MSSTL_VERSION >= 142 || _HAS_SHARED_MUTEX
51# define BOOST_SYSTEM_HAS_MSSTL_SHARED_MUTEX
52#endif
53
54#endif // BOOST_MSSTL_VERSION >= 142 || _HAS_SHARED_MUTEX
55
56#endif // BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
57
58#if defined(BOOST_SYSTEM_HAS_MSSTL_SHARED_MUTEX)
59
60namespace boost
61{
62namespace system
63{
64namespace detail
65{
66
67typedef std::shared_mutex mutex;
68
69} // namespace detail
70} // namespace system
71} // namespace boost
72
73#else // defined(BOOST_SYSTEM_HAS_MSSTL_SHARED_MUTEX)
74
75#include <mutex>
76
77namespace boost
78{
79namespace system
80{
81namespace detail
82{
83
84using std::mutex;
85
86} // namespace detail
87} // namespace system
88} // namespace boost
89
90#endif // defined(BOOST_SYSTEM_HAS_MSSTL_SHARED_MUTEX)
91#endif // defined(BOOST_SYSTEM_DISABLE_THREADS)
92
93namespace boost
94{
95namespace system
96{
97namespace detail
98{
99
100template<class Mtx> class lock_guard
101{
102private:
103
104 Mtx& mtx_;
105
106private:
107
108 lock_guard( lock_guard const& );
109 lock_guard& operator=( lock_guard const& );
110
111public:
112
113 explicit lock_guard( Mtx& mtx ): mtx_( mtx )
114 {
115 mtx_.lock();
116 }
117
118 ~lock_guard()
119 {
120 mtx_.unlock();
121 }
122};
123
124} // namespace detail
125} // namespace system
126} // namespace boost
127
128#endif // #ifndef BOOST_SYSTEM_DETAIL_MUTEX_HPP_INCLUDED
129

source code of boost/libs/system/include/boost/system/detail/mutex.hpp