NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
mutex.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_ASYNC_MUTEX_HPP__
2#define NEFORCE_CORE_ASYNC_MUTEX_HPP__
3
10
12#ifdef NEFORCE_PLATFORM_WINDOWS
14# include <synchapi.h>
15# ifdef max
16# undef max
17# endif
18# ifdef min
19# undef min
20# endif
21#endif
22#ifdef NEFORCE_PLATFORM_LINUX
23# include <pthread.h>
24#endif
25NEFORCE_BEGIN_NAMESPACE__
26
32
38
39class mutex_base {};
40
45class NEFORCE_API mutex : public mutex_base {
46public:
51#ifdef NEFORCE_PLATFORM_WINDOWS
52 ::SRWLOCK;
53#else
54 ::pthread_mutex_t;
55#endif
56
57private:
58 mutable native_handle_type mutex_;
59
60public:
65
70
71 mutex(const mutex&) = delete;
72 mutex& operator=(const mutex&) = delete;
73 mutex(mutex&&) = delete;
74 mutex& operator=(mutex&&) = delete;
75
80 native_handle_type* native_handle() noexcept { return &mutex_; }
81
86 const native_handle_type* native_handle() const noexcept { return &mutex_; }
87
93 void lock();
94
100 void unlock();
101
108 bool try_lock() noexcept;
109};
110
117class NEFORCE_API recursive_mutex : public mutex_base {
118public:
123#ifdef NEFORCE_PLATFORM_WINDOWS
124 ::CRITICAL_SECTION;
125#else
126 ::pthread_mutex_t;
127#endif
128
129private:
130 mutable native_handle_type recursive_mutex_;
131
132public:
137
142
143 recursive_mutex(const recursive_mutex&) = delete;
144 recursive_mutex& operator=(const recursive_mutex&) = delete;
145
150 native_handle_type* native_handle() noexcept { return &recursive_mutex_; }
151
156 const native_handle_type* native_handle() const noexcept { return &recursive_mutex_; }
157
164 void lock();
165
171 void unlock();
172
179 bool try_lock() noexcept;
180};
181
182
190template <typename Mutex>
191class lock {
192 static_assert(is_base_of_v<mutex_base, Mutex>, "Mutex type must be derived from mutex_base");
193
194public:
195 using mutex_type = Mutex;
196
197private:
198 mutex_type& mutex_;
199
200public:
207 explicit lock(mutex_type& m) :
208 mutex_(m) {
209 mutex_.lock();
210 }
211
217 ~lock() { mutex_.unlock(); }
218
219 lock(const lock&) = delete;
220 lock& operator=(const lock&) = delete;
221};
222
223
230struct lock_defer_tag {
231 constexpr lock_defer_tag() noexcept = default;
232};
233
237NEFORCE_INLINE17 constexpr lock_defer_tag lock_defer{};
238
239
246struct lock_quiet_tag {
247 constexpr lock_quiet_tag() noexcept = default;
248};
249
253NEFORCE_INLINE17 constexpr lock_quiet_tag lock_quiet{};
254
255
262template <typename Mutex>
264 static_assert(is_base_of_v<mutex_base, Mutex>, "Mutex type must be derived from mutex_base");
265
266public:
267 using mutex_type = Mutex;
268
269private:
270 mutex_type* mutex_ = nullptr;
271 bool owns_lock_ = false;
272
273public:
279 unique_lock() = default;
280
287 explicit unique_lock(mutex_type& m) :
288 mutex_(&m),
289 owns_lock_(true) {
290 mutex_->lock();
291 }
292
301 mutex_(&m) {}
302
311 mutex_(&m),
312 owns_lock_(m.try_lock()) {}
313
314 unique_lock(const unique_lock&) = delete;
315 unique_lock& operator=(const unique_lock&) = delete;
316
323 unique_lock(unique_lock&& other) noexcept :
324 mutex_(other.mutex_),
325 owns_lock_(other.owns_lock_) {
326 other.mutex_ = nullptr;
327 other.owns_lock_ = false;
328 }
329
337 unique_lock& operator=(unique_lock&& other) noexcept {
338 if (_NEFORCE addressof(other) == this) {
339 return *this;
340 }
341 if (owns_lock_) {
342 mutex_->unlock();
343 }
344 mutex_ = other.mutex_;
345 owns_lock_ = other.owns_lock_;
346 other.mutex_ = nullptr;
347 other.owns_lock_ = false;
348 return *this;
349 }
350
357 if (owns_lock_) {
358 mutex_->unlock();
359 }
360 }
361
366 NEFORCE_NODISCARD explicit operator bool() const noexcept { return owns_lock_; }
367
372 NEFORCE_NODISCARD bool owns_lock() const noexcept { return owns_lock_; }
373
378 NEFORCE_NODISCARD mutex_type* mutex() const noexcept { return mutex_; }
379
385 void lock_quiet() {
386 if (mutex_ == nullptr) {
387 return;
388 }
389 if (owns_lock_) {
390 return;
391 }
392 mutex_->lock();
393 owns_lock_ = true;
394 }
395
402 if (mutex_ == nullptr) {
403 return;
404 }
405 if (!owns_lock_) {
406 return;
407 }
408 mutex_->unlock();
409 owns_lock_ = false;
410 }
411
418 bool try_lock() noexcept {
419 if (mutex_ == nullptr) {
420 return false;
421 }
422 if (owns_lock_) {
423 return true;
424 }
425 owns_lock_ = mutex_->try_lock();
426 return owns_lock_;
427 }
428
435 mutex_type* release() noexcept {
436 mutex_type* ret = mutex_;
437 mutex_ = nullptr;
438 owns_lock_ = false;
439 return ret;
440 }
441};
442 // Mutex
444 // AsyncComponents
446
447NEFORCE_END_NAMESPACE__
448#endif // NEFORCE_CORE_ASYNC_MUTEX_HPP__
锁管理器模板
Mutex mutex_type
互斥锁类型
~lock()
析构函数
lock(mutex_type &m)
构造函数
~mutex()
析构函数
const native_handle_type * native_handle() const noexcept
获取常量原生句柄
native_handle_type * native_handle() noexcept
获取原生句柄
mutex()
构造函数
void unlock()
解锁互斥锁
void lock()
锁定互斥锁
::SRWLOCK native_handle_type
互斥锁的系统句柄类型
bool try_lock() noexcept
尝试锁定互斥锁
bool try_lock() noexcept
尝试锁定递归互斥锁
~recursive_mutex()
析构函数
void unlock()
解锁递归互斥锁
recursive_mutex()
构造函数
const native_handle_type * native_handle() const noexcept
获取常量原生句柄
::CRITICAL_SECTION native_handle_type
递归互斥锁的系统句柄类型
void lock()
锁定递归互斥锁
native_handle_type * native_handle() noexcept
获取原生句柄
独占锁管理器模板
unique_lock & operator=(unique_lock &&other) noexcept
移动赋值运算符
mutex_type * mutex() const noexcept
获取管理的互斥锁指针
Mutex mutex_type
互斥锁类型
mutex_type * release() noexcept
释放所有权
unique_lock(mutex_type &m)
从互斥锁构造
unique_lock()=default
默认构造函数
unique_lock(unique_lock &&other) noexcept
移动构造函数
unique_lock(mutex_type &m, lock_defer_tag tag) noexcept
延迟锁定构造函数
void unlock_quiet()
解锁互斥锁
~unique_lock()
析构函数
unique_lock(mutex_type &m, lock_quiet_tag tag) noexcept
尝试锁定构造函数
bool owns_lock() const noexcept
检查是否拥有锁
bool try_lock() noexcept
尝试锁定互斥锁
void lock_quiet()
锁定互斥锁
constexpr T * addressof(T &x) noexcept
获取对象的地址
constexpr bool is_base_of_v
is_base_of的便捷变量模板
constexpr lock_defer_tag lock_defer
延迟锁定标签实例
constexpr lock_quiet_tag lock_quiet
尝试锁定标签实例
延迟锁定标签
尝试锁定标签
类型萃取
WindowsAPI 平台宏