NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
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
26class shared_mutex_base : public mutex_base {};
27
28
36class NEFORCE_API shared_mutex : public shared_mutex_base {
37public:
42#ifdef NEFORCE_PLATFORM_WINDOWS
43 ::SRWLOCK;
44#else
45 ::pthread_rwlock_t;
46#endif
47
48private:
49 mutable native_handle_type shared_mutex_;
50
51public:
56
61
62 shared_mutex(const shared_mutex&) = delete;
63 shared_mutex& operator=(const shared_mutex&) = delete;
64
65 shared_mutex(shared_mutex&&) = default;
66 shared_mutex& operator=(shared_mutex&&) = default;
67
72 native_handle_type* native_handle() noexcept { return &shared_mutex_; }
73
78 const native_handle_type* native_handle() const noexcept { return &shared_mutex_; }
79
86 void lock();
87
93 void unlock();
94
101 bool try_lock() noexcept;
102
110
117
124 bool try_lock_shared() noexcept;
125};
126
134template <typename SharedMutex>
137 "SharedMutex type must be derived from shared_mutex_base");
138
139public:
140 using mutex_type = SharedMutex;
141
142private:
143 mutex_type* mutex_ = nullptr;
144 bool owns_lock_ = false;
145
146public:
152 shared_lock() = default;
153
160 explicit shared_lock(mutex_type& m) :
161 mutex_(&m),
162 owns_lock_(true) {
163 mutex_->lock_shared();
164 }
165
174 mutex_(&m) {}
175
184 mutex_(&m),
185 owns_lock_(m.try_lock_shared()) {}
186
187 shared_lock(const shared_lock&) = delete;
188 shared_lock& operator=(const shared_lock&) = delete;
189
196 shared_lock(shared_lock&& other) noexcept :
197 mutex_(other.mutex_),
198 owns_lock_(other.owns_lock_) {
199 other.mutex_ = nullptr;
200 other.owns_lock_ = false;
201 }
202
210 shared_lock& operator=(shared_lock&& other) noexcept {
211 if (_NEFORCE addressof(other) == this) {
212 return *this;
213 }
214 if (owns_lock_) {
215 mutex_->unlock_shared();
216 }
217 mutex_ = other.mutex_;
218 owns_lock_ = other.owns_lock_;
219 other.mutex_ = nullptr;
220 other.owns_lock_ = false;
221 return *this;
222 }
223
230 if (owns_lock_) {
231 mutex_->unlock_shared();
232 }
233 }
234
239 NEFORCE_NODISCARD explicit operator bool() const noexcept { return owns_lock_; }
240
245 NEFORCE_NODISCARD bool owns_lock() const noexcept { return owns_lock_; }
246
251 NEFORCE_NODISCARD mutex_type* mutex() const noexcept { return mutex_; }
252
258 void lock() {
259 if (mutex_ == nullptr) {
260 return;
261 }
262 if (owns_lock_) {
263 return;
264 }
265 mutex_->lock_shared();
266 owns_lock_ = true;
267 }
268
274 void unlock() {
275 if (mutex_ == nullptr) {
276 return;
277 }
278 if (!owns_lock_) {
279 return;
280 }
281 mutex_->unlock_shared();
282 owns_lock_ = false;
283 }
284
291 bool try_lock() noexcept {
292 if (mutex_ == nullptr) {
293 return false;
294 }
295 if (owns_lock_) {
296 return true;
297 }
298 owns_lock_ = mutex_->try_lock_shared();
299 return owns_lock_;
300 }
301
308 mutex_type* release() noexcept {
309 mutex_type* ret = mutex_;
310 mutex_ = nullptr;
311 owns_lock_ = false;
312 return ret;
313 }
314};
315 // Mutex
317 // AsyncComponents
319
320NEFORCE_END_NAMESPACE__
321#endif // NEFORCE_CORE_ASYNC_SHARED_MUTEX_HPP__
shared_lock & operator=(shared_lock &&other) noexcept
移动赋值运算符
shared_lock(mutex_type &m, lock_defer_tag tag) noexcept
延迟锁定构造函数
shared_lock(mutex_type &m)
从共享互斥锁构造
mutex_type * release() noexcept
释放所有权
bool try_lock() noexcept
尝试获取读锁
shared_lock()=default
默认构造函数
void unlock()
释放读锁
SharedMutex mutex_type
共享互斥锁类型
shared_lock(shared_lock &&other) noexcept
移动构造函数
mutex_type * mutex() const noexcept
获取管理的共享互斥锁指针
bool owns_lock() const noexcept
检查是否拥有共享锁
shared_lock(mutex_type &m, lock_quiet_tag tag) noexcept
尝试锁定构造函数
~shared_mutex()
析构函数
void unlock_shared()
释放读锁
void lock()
获取写锁
bool try_lock_shared() noexcept
尝试获取读锁
void unlock()
释放写锁
shared_mutex()
构造函数
bool try_lock() noexcept
尝试获取写锁
::SRWLOCK native_handle_type
共享互斥锁的系统句柄类型
const native_handle_type * native_handle() const noexcept
获取常量原生句柄
native_handle_type * native_handle() noexcept
获取原生句柄
void lock_shared()
获取读锁
constexpr T * addressof(T &x) noexcept
获取对象的地址
constexpr bool is_base_of_v
is_base_of的便捷变量模板
互斥锁
延迟锁定标签
尝试锁定标签