aboutsummaryrefslogtreecommitdiff
path: root/store/works/life/computer-network-experiment/ReadWriteLock.h
blob: ee40ac0871f99101a62d68971b7e7aa6def94617 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#pragma once
#include "Common.h"

#include <memory>

#ifdef WIN32
#include <Windows.h>
#else
#include <pthread.h>
#endif

namespace cru {
class ReadWriteLock {
  friend void swap(ReadWriteLock &left, ReadWriteLock &right);
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
  std::unique_ptr<pthread_rwlock_t> lock_;
#endif
};

void swap(ReadWriteLock &left, ReadWriteLock &right);
} // namespace cru