diff options
author | crupest <crupest@outlook.com> | 2021-06-08 20:22:21 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-06-08 20:22:21 +0800 |
commit | a84887356487a4fcbd57a90f3ce17914ea8bdd0a (patch) | |
tree | a896769a28b73aa10f41b6c0359ec78ce7f2563b /works/life/computer-network-experiment/ReadWriteLock.cpp | |
parent | 53717298fda9bffed5f8d9201db47f3fd09fa612 (diff) | |
download | crupest-a84887356487a4fcbd57a90f3ce17914ea8bdd0a.tar.gz crupest-a84887356487a4fcbd57a90f3ce17914ea8bdd0a.tar.bz2 crupest-a84887356487a4fcbd57a90f3ce17914ea8bdd0a.zip |
import(life): ...
Diffstat (limited to 'works/life/computer-network-experiment/ReadWriteLock.cpp')
-rw-r--r-- | works/life/computer-network-experiment/ReadWriteLock.cpp | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/works/life/computer-network-experiment/ReadWriteLock.cpp b/works/life/computer-network-experiment/ReadWriteLock.cpp new file mode 100644 index 0000000..d9b6e3e --- /dev/null +++ b/works/life/computer-network-experiment/ReadWriteLock.cpp @@ -0,0 +1,59 @@ +#include "ReadWriteLock.h"
+
+#include <cassert>
+#include <memory>
+
+namespace cru {
+ReadWriteLock::ReadWriteLock() {
+#ifdef WIN32
+ lock_ = std::make_unique<SRWLOCK>();
+ InitializeSRWLock(lock_.get());
+#else
+#endif
+}
+
+ReadWriteLock::ReadWriteLock(ReadWriteLock &&other)
+ : lock_(std::move(other.lock_)) {}
+
+ReadWriteLock &ReadWriteLock::operator=(ReadWriteLock &&other) {
+ if (this != &other) {
+ Destroy();
+ lock_ = std::move(other.lock_);
+ }
+ return *this;
+}
+
+ReadWriteLock::~ReadWriteLock() { Destroy(); }
+
+void ReadWriteLock::ReadLock() {
+ assert(lock_);
+ AcquireSRWLockShared(lock_.get());
+}
+
+void ReadWriteLock::WriteLock() {
+ assert(lock_);
+ AcquireSRWLockExclusive(lock_.get());
+}
+
+bool ReadWriteLock::ReadTryLock() {
+ assert(lock_);
+ return TryAcquireSRWLockShared(lock_.get()) != 0;
+}
+
+bool ReadWriteLock::WriteTryLock() {
+ assert(lock_);
+ return TryAcquireSRWLockExclusive(lock_.get()) != 0;
+}
+
+void ReadWriteLock::ReadUnlock() {
+ assert(lock_);
+ ReleaseSRWLockShared(lock_.get());
+}
+
+void ReadWriteLock::WriteUnlock() {
+ assert(lock_);
+ ReleaseSRWLockExclusive(lock_.get());
+}
+
+void ReadWriteLock::Destroy() {}
+} // namespace cru
\ No newline at end of file |