| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_MUTEX_MUTEX_HPP |
| 4 |
#define MPT_MUTEX_MUTEX_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
|
| 11 |
|
| 12 |
#if !MPT_PLATFORM_MULTITHREADED |
| 13 |
#define MPT_MUTEX_NONE 1 |
| 14 |
#elif defined(MPT_LIBCXX_QUIRK_NO_STD_THREAD) |
| 15 |
#define MPT_MUTEX_WIN32 1 |
| 16 |
#else |
| 17 |
#define MPT_MUTEX_STD 1 |
| 18 |
#endif |
| 19 |
|
| 20 |
#ifndef MPT_MUTEX_STD |
| 21 |
#define MPT_MUTEX_STD 0 |
| 22 |
#endif |
| 23 |
#ifndef MPT_MUTEX_WIN32 |
| 24 |
#define MPT_MUTEX_WIN32 0 |
| 25 |
#endif |
| 26 |
#ifndef MPT_MUTEX_NONE |
| 27 |
#define MPT_MUTEX_NONE 0 |
| 28 |
#endif |
| 29 |
|
| 30 |
#if MPT_MUTEX_STD |
| 31 |
#include <mutex> |
| 32 |
#ifdef MPT_LIBCXX_QUIRK_COMPLEX_STD_MUTEX |
| 33 |
#include <shared_mutex> |
| 34 |
#include <type_traits> |
| 35 |
#endif |
| 36 |
#elif MPT_MUTEX_WIN32 |
| 37 |
#include <windows.h> |
| 38 |
#endif // MPT_MUTEX |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
namespace mpt { |
| 43 |
inline namespace MPT_INLINE_NS { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#if MPT_MUTEX_STD |
| 48 |
|
| 49 |
#ifdef MPT_LIBCXX_QUIRK_COMPLEX_STD_MUTEX |
| 50 |
using mutex = std::conditional<sizeof(std::shared_mutex) < sizeof(std::mutex), std::shared_mutex, std::mutex>::type; |
| 51 |
#else |
| 52 |
using mutex = std::mutex; |
| 53 |
#endif |
| 54 |
using recursive_mutex = std::recursive_mutex; |
| 55 |
|
| 56 |
#elif MPT_MUTEX_WIN32 |
| 57 |
|
| 58 |
#if MPT_WINNT_AT_LEAST(MPT_WIN_VISTA) |
| 59 |
|
| 60 |
// compatible with c++11 std::mutex, can eventually be replaced without touching any usage site |
| 61 |
class mutex { |
| 62 |
private: |
| 63 |
SRWLOCK impl = SRWLOCK_INIT; |
| 64 |
|
| 65 |
public: |
| 66 |
mutex() = default; |
| 67 |
~mutex() = default; |
| 68 |
void lock() { |
| 69 |
AcquireSRWLockExclusive(&impl); |
| 70 |
} |
| 71 |
bool try_lock() { |
| 72 |
return TryAcquireSRWLockExclusive(&impl) ? true : false; |
| 73 |
} |
| 74 |
void unlock() { |
| 75 |
ReleaseSRWLockExclusive(&impl); |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
#else // !_WIN32_WINNT_VISTA |
| 80 |
|
| 81 |
// compatible with c++11 std::mutex, can eventually be replaced without touching any usage site |
| 82 |
class mutex { |
| 83 |
private: |
| 84 |
CRITICAL_SECTION impl; |
| 85 |
|
| 86 |
public: |
| 87 |
mutex() { |
| 88 |
InitializeCriticalSection(&impl); |
| 89 |
} |
| 90 |
~mutex() { |
| 91 |
DeleteCriticalSection(&impl); |
| 92 |
} |
| 93 |
void lock() { |
| 94 |
EnterCriticalSection(&impl); |
| 95 |
} |
| 96 |
bool try_lock() { |
| 97 |
return TryEnterCriticalSection(&impl) ? true : false; |
| 98 |
} |
| 99 |
void unlock() { |
| 100 |
LeaveCriticalSection(&impl); |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
#endif // _WIN32_WINNT_VISTA |
| 105 |
|
| 106 |
// compatible with c++11 std::recursive_mutex, can eventually be replaced without touching any usage site |
| 107 |
class recursive_mutex { |
| 108 |
private: |
| 109 |
CRITICAL_SECTION impl; |
| 110 |
|
| 111 |
public: |
| 112 |
recursive_mutex() { |
| 113 |
InitializeCriticalSection(&impl); |
| 114 |
} |
| 115 |
~recursive_mutex() { |
| 116 |
DeleteCriticalSection(&impl); |
| 117 |
} |
| 118 |
void lock() { |
| 119 |
EnterCriticalSection(&impl); |
| 120 |
} |
| 121 |
bool try_lock() { |
| 122 |
return TryEnterCriticalSection(&impl) ? true : false; |
| 123 |
} |
| 124 |
void unlock() { |
| 125 |
LeaveCriticalSection(&impl); |
| 126 |
} |
| 127 |
}; |
| 128 |
|
| 129 |
#else // MPT_MUTEX_NONE |
| 130 |
|
| 131 |
class mutex { |
| 132 |
public: |
| 133 |
mutex() { |
| 134 |
return; |
| 135 |
} |
| 136 |
~mutex() { |
| 137 |
return; |
| 138 |
} |
| 139 |
void lock() { |
| 140 |
return; |
| 141 |
} |
| 142 |
bool try_lock() { |
| 143 |
return true; |
| 144 |
} |
| 145 |
void unlock() { |
| 146 |
return; |
| 147 |
} |
| 148 |
}; |
| 149 |
|
| 150 |
class recursive_mutex { |
| 151 |
public: |
| 152 |
recursive_mutex() { |
| 153 |
return; |
| 154 |
} |
| 155 |
~recursive_mutex() { |
| 156 |
return; |
| 157 |
} |
| 158 |
void lock() { |
| 159 |
return; |
| 160 |
} |
| 161 |
bool try_lock() { |
| 162 |
return true; |
| 163 |
} |
| 164 |
void unlock() { |
| 165 |
return; |
| 166 |
} |
| 167 |
}; |
| 168 |
|
| 169 |
#endif // MPT_MUTEX |
| 170 |
|
| 171 |
#if MPT_MUTEX_STD |
| 172 |
|
| 173 |
template <typename T> |
| 174 |
using lock_guard = std::lock_guard<T>; |
| 175 |
|
| 176 |
#else // !MPT_MUTEX_STD |
| 177 |
|
| 178 |
// compatible with c++11 std::lock_guard, can eventually be replaced without touching any usage site |
| 179 |
template <typename mutex_type> |
| 180 |
class lock_guard { |
| 181 |
private: |
| 182 |
mutex_type & mutex; |
| 183 |
|
| 184 |
public: |
| 185 |
lock_guard(mutex_type & m) |
| 186 |
: mutex(m) { |
| 187 |
mutex.lock(); |
| 188 |
} |
| 189 |
~lock_guard() { |
| 190 |
mutex.unlock(); |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
#endif // MPT_MUTEX_STD |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
} // namespace MPT_INLINE_NS |
| 199 |
} // namespace mpt |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
#endif // MPT_MUTEX_MUTEX_HPP |