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 mutex(mutex&&) = delete;
72 mutex& operator=(mutex&&) = delete;
73
78 native_handle_type* native_handle() noexcept { return &mutex_; }
79
84 const native_handle_type* native_handle() const noexcept { return &mutex_; }
85
91 void lock();
92
98 void unlock();
99
106 bool try_lock() noexcept;
107};
108
115class NEFORCE_API recursive_mutex {
116public:
121#ifdef NEFORCE_PLATFORM_WINDOWS
122 ::CRITICAL_SECTION;
123#else
124 ::pthread_mutex_t;
125#endif
126
127private:
128 mutable native_handle_type recursive_mutex_;
129
130public:
135
140
141 recursive_mutex(const recursive_mutex&) = delete;
142 recursive_mutex& operator=(const recursive_mutex&) = delete;
143
148 native_handle_type* native_handle() noexcept { return &recursive_mutex_; }
149
154 const native_handle_type* native_handle() const noexcept { return &recursive_mutex_; }
155
162 void lock();
163
169 void unlock();
170
177 bool try_lock() noexcept;
178};
179
180
188template <typename Mutex>
189class lock {
190public:
191 using mutex_type = Mutex;
192
193private:
194 mutex_type& mutex_;
195
196public:
203 explicit lock(mutex_type& m) :
204 mutex_(m) {
205 mutex_.lock();
206 }
207
213 ~lock() { mutex_.unlock(); }
214
215 lock(const lock&) = delete;
216 lock& operator=(const lock&) = delete;
217};
218
219
226struct defer_lock_tag {
227 constexpr defer_lock_tag() noexcept = default;
228};
229
232NEFORCE_INLINE17 constexpr defer_lock_tag defer_lock{};
233
240struct try_lock_tag {
241 constexpr try_lock_tag() noexcept = default;
242};
243
246NEFORCE_INLINE17 constexpr try_lock_tag try_lock{};
247
248
255template <typename Mutex>
257public:
258 using mutex_type = Mutex;
259
260private:
261 mutex_type* mutex_ = nullptr;
262 bool owns_lock_ = false;
263
264public:
270 unique_lock() = default;
271
278 explicit unique_lock(mutex_type& m) :
279 mutex_(&m),
280 owns_lock_(true) {
281 mutex_->lock();
282 }
283
292 mutex_(&m) {}
293
302 mutex_(&m),
303 owns_lock_(m.try_lock()) {}
304
305 unique_lock(const unique_lock&) = delete;
306 unique_lock& operator=(const unique_lock&) = delete;
307
314 unique_lock(unique_lock&& other) noexcept :
315 mutex_(other.mutex_),
316 owns_lock_(other.owns_lock_) {
317 other.mutex_ = nullptr;
318 other.owns_lock_ = false;
319 }
320
328 unique_lock& operator=(unique_lock&& other) noexcept {
329 if (_NEFORCE addressof(other) == this) {
330 return *this;
331 }
332 if (owns_lock_) {
333 mutex_->unlock();
334 }
335 mutex_ = other.mutex_;
336 owns_lock_ = other.owns_lock_;
337 other.mutex_ = nullptr;
338 other.owns_lock_ = false;
339 return *this;
340 }
341
348 if (owns_lock_) {
349 mutex_->unlock();
350 }
351 }
352
357 NEFORCE_NODISCARD explicit operator bool() const noexcept { return owns_lock_; }
358
363 NEFORCE_NODISCARD bool owns_lock() const noexcept { return owns_lock_; }
364
369 NEFORCE_NODISCARD mutex_type* mutex() const noexcept { return mutex_; }
370
376 void lock_quiet() {
377 if (mutex_ == nullptr) {
378 return;
379 }
380 if (owns_lock_) {
381 return;
382 }
383 mutex_->lock();
384 owns_lock_ = true;
385 }
386
393 if (mutex_ == nullptr) {
394 return;
395 }
396 if (!owns_lock_) {
397 return;
398 }
399 mutex_->unlock();
400 owns_lock_ = false;
401 }
402
409 bool try_lock() noexcept {
410 if (mutex_ == nullptr) {
411 return false;
412 }
413 if (owns_lock_) {
414 return true;
415 }
416 owns_lock_ = mutex_->try_lock();
417 return owns_lock_;
418 }
419
426 mutex_type* release() noexcept {
427 mutex_type* ret = mutex_;
428 mutex_ = nullptr;
429 owns_lock_ = false;
430 return ret;
431 }
432};
433 // Mutex
435 // AsyncComponents
437
438NEFORCE_END_NAMESPACE__
439#endif // NEFORCE_CORE_ASYNC_MUTEX_HPP__
锁管理器模板
lock(mutex_type &m)
构造函数
Mutex mutex_type
互斥锁类型
~lock()
析构函数
bool try_lock() noexcept
尝试锁定互斥锁
void unlock()
解锁互斥锁
const native_handle_type * native_handle() const noexcept
获取常量原生句柄
mutex()
构造函数
void lock()
锁定互斥锁
::SRWLOCK native_handle_type
互斥锁的系统句柄类型
native_handle_type * native_handle() noexcept
获取原生句柄
~mutex()
析构函数
~recursive_mutex()
析构函数
bool try_lock() noexcept
尝试锁定递归互斥锁
recursive_mutex()
构造函数
::CRITICAL_SECTION native_handle_type
递归互斥锁的系统句柄类型
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
释放所有权
bool owns_lock() const noexcept
检查是否拥有锁
unique_lock(mutex_type &m, defer_lock_tag tag) noexcept
延迟锁定构造函数
void lock_quiet()
锁定互斥锁
unique_lock()=default
默认构造函数
unique_lock & operator=(unique_lock &&other) noexcept
移动赋值运算符
unique_lock(unique_lock &&other) noexcept
移动构造函数
mutex_type * mutex() const noexcept
获取管理的互斥锁指针
constexpr T * addressof(T &x) noexcept
获取对象的地址
constexpr defer_lock_tag defer_lock
延迟锁定标签实例
constexpr try_lock_tag try_lock
尝试锁定标签实例
延迟锁定标签
尝试锁定标签
类型萃取
WindowsAPI 平台宏