From a84887356487a4fcbd57a90f3ce17914ea8bdd0a Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 8 Jun 2021 20:22:21 +0800 Subject: import(life): ... --- .../computer-network-experiment/CMakeLists.txt | 2 +- .../computer-network-experiment/ReadWriteLock.cpp | 59 ++++++++++++++++++++++ .../computer-network-experiment/ReadWriteLock.h | 40 +++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 works/life/computer-network-experiment/ReadWriteLock.cpp create mode 100644 works/life/computer-network-experiment/ReadWriteLock.h diff --git a/works/life/computer-network-experiment/CMakeLists.txt b/works/life/computer-network-experiment/CMakeLists.txt index 09c1ea4..2ffd481 100644 --- a/works/life/computer-network-experiment/CMakeLists.txt +++ b/works/life/computer-network-experiment/CMakeLists.txt @@ -9,7 +9,7 @@ set(CMAKE_CXX_STANDARD 17) find_package(fmt CONFIG REQUIRED) find_package(Microsoft.GSL CONFIG REQUIRED) -add_library(base STATIC Common.cpp StringUtil.cpp IO.cpp) +add_library(base STATIC Common.cpp StringUtil.cpp IO.cpp ReadWriteLock.cpp) target_link_libraries(base PUBLIC Microsoft.GSL::GSL fmt::fmt Folly::folly) if(WIN32) target_link_libraries(base PUBLIC Ws2_32) 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 +#include + +namespace cru { +ReadWriteLock::ReadWriteLock() { +#ifdef WIN32 + lock_ = std::make_unique(); + 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 diff --git a/works/life/computer-network-experiment/ReadWriteLock.h b/works/life/computer-network-experiment/ReadWriteLock.h new file mode 100644 index 0000000..c9889da --- /dev/null +++ b/works/life/computer-network-experiment/ReadWriteLock.h @@ -0,0 +1,40 @@ +#pragma once +#include "Common.h" + +#include + +#ifdef WIN32 +#include +#else +#endif + +namespace cru { +class ReadWriteLock { +public: + ReadWriteLock(); + + ReadWriteLock(ReadWriteLock &&other); + ReadWriteLock &operator=(ReadWriteLock &&other); + + ~ReadWriteLock(); + +public: + void ReadLock(); + void WriteLock(); + + bool ReadTryLock(); + bool WriteTryLock(); + + void ReadUnlock(); + void WriteUnlock(); + +private: + void Destroy(); + +private: +#ifdef WIN32 + std::unique_ptr lock_; +#else +#endif +}; +} // namespace cru -- cgit v1.2.3