NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
shared_mutex.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_ASYNC_SHARED_MUTEX_HPP__
2#define NEFORCE_CORE_ASYNC_SHARED_MUTEX_HPP__
3
10
11#include "mutex.hpp"
12NEFORCE_BEGIN_NAMESPACE__
13
19
25
33class NEFORCE_API shared_mutex {
34public:
39#ifdef NEFORCE_PLATFORM_WINDOWS
40 ::SRWLOCK;
41#else
42 ::pthread_rwlock_t;
43#endif
44
45private:
46 mutable native_handle_type shared_mutex_;
47
48public:
53
58
59 shared_mutex(const shared_mutex&) = delete;
60 shared_mutex& operator=(const shared_mutex&) = delete;
61
62 shared_mutex(shared_mutex&&) = default;
63 shared_mutex& operator=(shared_mutex&&) = default;
64
69 native_handle_type* native_handle() noexcept { return &shared_mutex_; }
70
75 const native_handle_type* native_handle() const noexcept { return &shared_mutex_; }
76
83 void lock();
84
90 void unlock();
91
98 bool try_lock() noexcept;
99
107
114
121 bool try_lock_shared() noexcept;
122};
123
131template <typename SharedMutex>
133public:
134 using mutex_type = SharedMutex;
135
136private:
137 mutex_type* mutex_ = nullptr;
138 bool owns_lock_ = false;
139
140public:
146 shared_lock() = default;
147
154 explicit shared_lock(mutex_type& m) :
155 mutex_(&m),
156 owns_lock_(true) {
157 mutex_->lock_shared();
158 }
159
168 mutex_(&m) {}
169
178 mutex_(&m),
179 owns_lock_(m.try_lock_shared()) {}
180
181 shared_lock(const shared_lock&) = delete;
182 shared_lock& operator=(const shared_lock&) = delete;
183
190 shared_lock(shared_lock&& other) noexcept :
191 mutex_(other.mutex_),
192 owns_lock_(other.owns_lock_) {
193 other.mutex_ = nullptr;
194 other.owns_lock_ = false;
195 }
196
204 shared_lock& operator=(shared_lock&& other) noexcept {
205 if (_NEFORCE addressof(other) == this) {
206 return *this;
207 }
208 if (owns_lock_) {
209 mutex_->unlock_shared();
210 }
211 mutex_ = other.mutex_;
212 owns_lock_ = other.owns_lock_;
213 other.mutex_ = nullptr;
214 other.owns_lock_ = false;
215 return *this;
216 }
217
224 if (owns_lock_) {
225 mutex_->unlock_shared();
226 }
227 }
228
233 NEFORCE_NODISCARD explicit operator bool() const noexcept { return owns_lock_; }
234
239 NEFORCE_NODISCARD bool owns_lock() const noexcept { return owns_lock_; }
240
245 NEFORCE_NODISCARD mutex_type* mutex() const noexcept { return mutex_; }
246
252 void lock() {
253 if (!mutex_) {
254 return;
255 }
256 if (owns_lock_) {
257 return;
258 }
259 mutex_->lock_shared();
260 owns_lock_ = true;
261 }
262
268 void unlock() {
269 if (!mutex_) {
270 return;
271 }
272 if (!owns_lock_) {
273 return;
274 }
275 mutex_->unlock_shared();
276 owns_lock_ = false;
277 }
278
285 bool try_lock() noexcept {
286 if (!mutex_) {
287 return false;
288 }
289 if (owns_lock_) {
290 return true;
291 }
292 owns_lock_ = mutex_->try_lock_shared();
293 return owns_lock_;
294 }
295
302 mutex_type* release() noexcept {
303 mutex_type* ret = mutex_;
304 mutex_ = nullptr;
305 owns_lock_ = false;
306 return ret;
307 }
308};
309 // Mutex
311 // AsyncComponents
313
314NEFORCE_END_NAMESPACE__
315#endif // NEFORCE_CORE_ASYNC_SHARED_MUTEX_HPP__
共享锁类模板
~shared_lock()
析构函数
SharedMutex mutex_type
共享互斥锁类型
shared_lock(shared_lock &&other) noexcept
移动构造函数
shared_lock & operator=(shared_lock &&other) noexcept
移动赋值运算符
mutex_type * release() noexcept
释放所有权
shared_lock(mutex_type &m, defer_lock_tag tag) noexcept
延迟锁定构造函数
shared_lock()=default
默认构造函数
bool try_lock() noexcept
尝试获取读锁
void lock()
获取读锁
void unlock()
释放读锁
NEFORCE_NODISCARD bool owns_lock() const noexcept
检查是否拥有共享锁
NEFORCE_NODISCARD mutex_type * mutex() const noexcept
获取管理的共享互斥锁指针
shared_lock(mutex_type &m)
从共享互斥锁构造
shared_lock(mutex_type &m, try_lock_tag tag) noexcept
尝试锁定构造函数
void unlock_shared()
释放读锁
native_handle_type * native_handle() noexcept
获取原生句柄
const native_handle_type * native_handle() const noexcept
获取常量原生句柄
~shared_mutex()
析构函数
::pthread_rwlock_t native_handle_type
共享互斥锁的系统句柄类型
shared_mutex()
构造函数
void lock()
获取写锁
bool try_lock() noexcept
尝试获取写锁
void lock_shared()
获取读锁
bool try_lock_shared() noexcept
尝试获取读锁
void unlock()
释放写锁
NEFORCE_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
互斥锁
延迟锁定标签
尝试锁定标签