MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
mutex.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_ASYNC_MUTEX_HPP__
2#define MSTL_CORE_ASYNC_MUTEX_HPP__
3
10
12#ifdef MSTL_PLATFORM_WINDOWS__
13#include <Windows.h>
14#ifdef max
15#undef max
16#endif
17#ifdef min
18#undef min
19#endif
20#endif
21#ifdef MSTL_PLATFORM_LINUX__
22#include <pthread.h>
23#endif
25
31
36class MSTL_API mutex {
37public:
42#ifdef MSTL_PLATFORM_WINDOWS__
43 ::SRWLOCK;
44#else
45 ::pthread_mutex_t;
46#endif
47
48private:
49 mutable native_handle_type mutex_;
50
51public:
56
61
62 mutex(const mutex&) = delete;
63 mutex& operator =(const mutex&) = delete;
64
69 native_handle_type* native_handle() noexcept { return &mutex_; }
70
75 const native_handle_type* native_handle() const noexcept { return &mutex_; }
76
82 void lock();
83
89 void unlock();
90
97 bool try_lock() noexcept;
98};
99
106class MSTL_API recursive_mutex {
107public:
112#ifdef MSTL_PLATFORM_WINDOWS__
113 ::CRITICAL_SECTION;
114#else
115 ::pthread_mutex_t;
116#endif
117
118private:
119 mutable native_handle_type recursive_mutex_;
120
121public:
126
131
132 recursive_mutex(const recursive_mutex&) = delete;
133 recursive_mutex& operator =(const recursive_mutex&) = delete;
134
139 native_handle_type* native_handle() noexcept { return &recursive_mutex_; }
140
145 const native_handle_type* native_handle() const noexcept { return &recursive_mutex_; }
146
153 void lock();
154
160 void unlock();
161
168 bool try_lock() noexcept;
169};
170
171
180template <typename Mutex, bool WithDefer = false>
181class lock {
182public:
183 using mutex_type = Mutex;
184
185private:
186 mutex_type& mutex_;
187
188public:
195 explicit lock(mutex_type& m) : mutex_(m) {
196 mutex_.lock();
197 }
198
205 mutex_.unlock();
206 }
207
208 lock(const lock&) = delete;
209 lock& operator =(const lock&) = delete;
210};
211
212
219struct defer_lock_tag {
220 constexpr defer_lock_tag() noexcept = default;
221};
222
225MSTL_INLINE17 constexpr defer_lock_tag defer_lock{};
226
233struct try_lock_tag {
234 constexpr try_lock_tag() noexcept = default;
235};
236
239MSTL_INLINE17 constexpr try_lock_tag try_lock{};
240
241
249template <typename Mutex>
250class lock<Mutex, true> {
251public:
252 using mutex_type = Mutex;
253
254private:
255 mutex_type* mutex_ = nullptr;
256 bool owns_lock_ = false;
257
258public:
264 lock() = default;
265
272 explicit lock(mutex_type& m)
273 : mutex_(&m), owns_lock_(true) {
274 mutex_->lock();
275 }
276
285 : mutex_(&m) {}
286
294 lock(mutex_type& m, try_lock_tag tag) noexcept
295 : mutex_(&m), owns_lock_(m.try_lock()) {}
296
297 lock(const lock&) = delete;
298 lock& operator =(const lock&) = delete;
299
306 lock(lock&& other) noexcept
307 : mutex_(other.mutex_), owns_lock_(other.owns_lock_) {
308 other.mutex_ = nullptr;
309 other.owns_lock_ = false;
310 }
311
319 lock& operator =(lock&& other) noexcept {
320 if (_MSTL addressof(other) == this) return *this;
321 if (owns_lock_) mutex_->unlock();
322 mutex_ = other.mutex_;
323 owns_lock_ = other.owns_lock_;
324 other.mutex_ = nullptr;
325 other.owns_lock_ = false;
326 return *this;
327 }
328
335 if (owns_lock_) mutex_->unlock();
336 }
337
342 MSTL_NODISCARD explicit operator bool() const noexcept {
343 return owns_lock_;
344 }
345
350 MSTL_NODISCARD bool owns_lock() const noexcept {
351 return owns_lock_;
352 }
353
358 MSTL_NODISCARD mutex_type* mutex() const noexcept {
359 return mutex_;
360 }
361
367 void lock_quiet() {
368 if (!mutex_) return;
369 if (owns_lock_) return;
370 mutex_->lock();
371 owns_lock_ = true;
372 }
373
380 if (!mutex_) return;
381 if (!owns_lock_) return;
382 mutex_->unlock();
383 owns_lock_ = false;
384 }
385
392 bool try_lock() noexcept {
393 if (!mutex_) return false;
394 if (owns_lock_) return true;
395 owns_lock_ = mutex_->try_lock();
396 return owns_lock_;
397 }
398
405 mutex_type* release() noexcept {
406 mutex_type* ret = mutex_;
407 mutex_ = nullptr;
408 owns_lock_ = false;
409 return ret;
410 }
411};
412
417template <typename Mutex>
419 // Mutex
421
423#endif // MSTL_CORE_ASYNC_MUTEX_HPP__
lock(mutex_type &m, try_lock_tag tag) noexcept
尝试锁定构造函数
bool try_lock() noexcept
尝试锁定互斥锁
lock()=default
默认构造函数
lock(lock &&other) noexcept
移动构造函数
void lock_quiet()
锁定互斥锁
lock(mutex_type &m)
从互斥锁构造
lock(mutex_type &m, defer_lock_tag tag) noexcept
延迟锁定构造函数
mutex_type * release() noexcept
释放所有权
MSTL_NODISCARD mutex_type * mutex() const noexcept
获取管理的互斥锁指针
~lock()
析构函数
MSTL_NODISCARD bool owns_lock() const noexcept
检查是否拥有锁
void unlock_quiet()
解锁互斥锁
Mutex mutex_type
互斥锁类型
锁管理器模板
Mutex mutex_type
互斥锁类型
~lock()
析构函数
lock(mutex_type &m)
构造函数
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()
锁定递归互斥锁
MSTL_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
MSTL_INLINE17 constexpr try_lock_tag try_lock
尝试锁定标签实例
lock< Mutex, true > smart_lock
智能锁管理器的便捷类型别名
MSTL_INLINE17 constexpr defer_lock_tag defer_lock
延迟锁定标签实例
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
延迟锁定标签
尝试锁定标签
MSTL类型萃取