NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
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
43class NEFORCE_API mutex {
44public:
49#ifdef NEFORCE_PLATFORM_WINDOWS
50 ::SRWLOCK;
51#else
52 ::pthread_mutex_t;
53#endif
54
55private:
56 mutable native_handle_type mutex_;
57
58public:
63
68
69 mutex(const mutex&) = delete;
70 mutex& operator=(const mutex&) = delete;
71
72 mutex(mutex&&) = default;
73 mutex& operator=(mutex&&) = default;
74
79 native_handle_type* native_handle() noexcept { return &mutex_; }
80
85 const native_handle_type* native_handle() const noexcept { return &mutex_; }
86
92 void lock();
93
99 void unlock();
100
107 bool try_lock() noexcept;
108};
109
116class NEFORCE_API recursive_mutex {
117public:
122#ifdef NEFORCE_PLATFORM_WINDOWS
123 ::CRITICAL_SECTION;
124#else
125 ::pthread_mutex_t;
126#endif
127
128private:
129 mutable native_handle_type recursive_mutex_;
130
131public:
136
141
142 recursive_mutex(const recursive_mutex&) = delete;
143 recursive_mutex& operator=(const recursive_mutex&) = delete;
144
149 native_handle_type* native_handle() noexcept { return &recursive_mutex_; }
150
155 const native_handle_type* native_handle() const noexcept { return &recursive_mutex_; }
156
163 void lock();
164
170 void unlock();
171
178 bool try_lock() noexcept;
179};
180
181
189template <typename Mutex>
190class lock {
191public:
192 using mutex_type = Mutex;
193
194private:
195 mutex_type& mutex_;
196
197public:
204 explicit lock(mutex_type& m) :
205 mutex_(m) {
206 mutex_.lock();
207 }
208
214 ~lock() { mutex_.unlock(); }
215
216 lock(const lock&) = delete;
217 lock& operator=(const lock&) = delete;
218};
219
220
227struct defer_lock_tag {
228 constexpr defer_lock_tag() noexcept = default;
229};
230
233NEFORCE_INLINE17 constexpr defer_lock_tag defer_lock{};
234
241struct try_lock_tag {
242 constexpr try_lock_tag() noexcept = default;
243};
244
247NEFORCE_INLINE17 constexpr try_lock_tag try_lock{};
248
249
256template <typename Mutex>
258public:
259 using mutex_type = Mutex;
260
261private:
262 mutex_type* mutex_ = nullptr;
263 bool owns_lock_ = false;
264
265public:
271 unique_lock() = default;
272
279 explicit unique_lock(mutex_type& m) :
280 mutex_(&m),
281 owns_lock_(true) {
282 mutex_->lock();
283 }
284
293 mutex_(&m) {}
294
303 mutex_(&m),
304 owns_lock_(m.try_lock()) {}
305
306 unique_lock(const unique_lock&) = delete;
307 unique_lock& operator=(const unique_lock&) = delete;
308
315 unique_lock(unique_lock&& other) noexcept :
316 mutex_(other.mutex_),
317 owns_lock_(other.owns_lock_) {
318 other.mutex_ = nullptr;
319 other.owns_lock_ = false;
320 }
321
329 unique_lock& operator=(unique_lock&& other) noexcept {
330 if (_NEFORCE addressof(other) == this) {
331 return *this;
332 }
333 if (owns_lock_) {
334 mutex_->unlock();
335 }
336 mutex_ = other.mutex_;
337 owns_lock_ = other.owns_lock_;
338 other.mutex_ = nullptr;
339 other.owns_lock_ = false;
340 return *this;
341 }
342
349 if (owns_lock_) {
350 mutex_->unlock();
351 }
352 }
353
358 NEFORCE_NODISCARD explicit operator bool() const noexcept { return owns_lock_; }
359
364 NEFORCE_NODISCARD bool owns_lock() const noexcept { return owns_lock_; }
365
370 NEFORCE_NODISCARD mutex_type* mutex() const noexcept { return mutex_; }
371
377 void lock_quiet() {
378 if (!mutex_) {
379 return;
380 }
381 if (owns_lock_) {
382 return;
383 }
384 mutex_->lock();
385 owns_lock_ = true;
386 }
387
394 if (!mutex_) {
395 return;
396 }
397 if (!owns_lock_) {
398 return;
399 }
400 mutex_->unlock();
401 owns_lock_ = false;
402 }
403
410 bool try_lock() noexcept {
411 if (!mutex_) {
412 return false;
413 }
414 if (owns_lock_) {
415 return true;
416 }
417 owns_lock_ = mutex_->try_lock();
418 return owns_lock_;
419 }
420
427 mutex_type* release() noexcept {
428 mutex_type* ret = mutex_;
429 mutex_ = nullptr;
430 owns_lock_ = false;
431 return ret;
432 }
433};
434 // Mutex
436 // AsyncComponents
438
439NEFORCE_END_NAMESPACE__
440#endif // NEFORCE_CORE_ASYNC_MUTEX_HPP__
锁管理器模板
lock(mutex_type &m)
构造函数
Mutex mutex_type
互斥锁类型
~lock()
析构函数
bool try_lock() noexcept
尝试锁定互斥锁
::pthread_mutex_t native_handle_type
互斥锁的系统句柄类型
void unlock()
解锁互斥锁
const native_handle_type * native_handle() const noexcept
获取常量原生句柄
mutex()
构造函数
void lock()
锁定互斥锁
native_handle_type * native_handle() noexcept
获取原生句柄
~mutex()
析构函数
~recursive_mutex()
析构函数
bool try_lock() noexcept
尝试锁定递归互斥锁
::pthread_mutex_t native_handle_type
递归互斥锁的系统句柄类型
recursive_mutex()
构造函数
const native_handle_type * native_handle() const noexcept
获取常量原生句柄
native_handle_type * native_handle() noexcept
获取原生句柄
void unlock()
解锁递归互斥锁
void lock()
锁定递归互斥锁
独占锁管理器模板
void unlock_quiet()
解锁互斥锁
bool try_lock() noexcept
尝试锁定互斥锁
~unique_lock()
析构函数
unique_lock(mutex_type &m, try_lock_tag tag) noexcept
尝试锁定构造函数
unique_lock(mutex_type &m)
从互斥锁构造
Mutex mutex_type
互斥锁类型
mutex_type * release() noexcept
释放所有权
NEFORCE_NODISCARD mutex_type * mutex() const noexcept
获取管理的互斥锁指针
unique_lock(mutex_type &m, defer_lock_tag tag) noexcept
延迟锁定构造函数
void lock_quiet()
锁定互斥锁
NEFORCE_NODISCARD bool owns_lock() const noexcept
检查是否拥有锁
unique_lock()=default
默认构造函数
unique_lock & operator=(unique_lock &&other) noexcept
移动赋值运算符
unique_lock(unique_lock &&other) noexcept
移动构造函数
NEFORCE_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
NEFORCE_INLINE17 constexpr try_lock_tag try_lock
尝试锁定标签实例
NEFORCE_INLINE17 constexpr defer_lock_tag defer_lock
延迟锁定标签实例
延迟锁定标签
尝试锁定标签
类型萃取
NeForce WindowsAPI 平台宏