diff options
Diffstat (limited to 'works')
3 files changed, 100 insertions, 1 deletions
| 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 <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 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 <memory>
 +
 +#ifdef WIN32
 +#include <Windows.h>
 +#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<SRWLOCK> lock_;
 +#else
 +#endif
 +};
 +} // namespace cru
 | 
