Samchon Framework for CPP  1.0.0
RWMutex.hpp
1 #pragma once
2 
3 #include <atomic>
4 #include <condition_variable>
5 #include <mutex>
6 
7 namespace samchon
8 {
9 namespace library
10 {
29  class RWMutex
30  {
31  private:
32  // Status variables
33  size_t readers_count_;
34  std::atomic<bool> writing_;
35 
36  // Conditional waiters
37  std::condition_variable read_cv_;
38  std::condition_variable write_cv_;
39 
40  // Lockers
41  std::mutex readers_count_mtx_;
42  std::mutex read_mtx_;
43  std::mutex write_mtx_;
44 
45  public:
50  {
51  readers_count_ = 0;
52  writing_ = false;
53  };
54 
67  void readLock() const
68  {
69  // WAIT WRITE_UNLOCK
70  std::unique_lock<std::mutex> uk((std::mutex&)read_mtx_);
71 
72  while (writing_ == true)
73  ((std::condition_variable&)read_cv_).wait(uk);
74 
75  // PLUS NUMBER OF READERS
76  ((std::mutex&)readers_count_mtx_).lock();
77  ((size_t&)readers_count_)++;
78  ((std::mutex&)readers_count_mtx_).unlock();
79  };
80 
90  void readUnlock() const
91  {
92  std::unique_lock<std::mutex> uk((std::mutex&)readers_count_mtx_);
93 
94  //차감 전 이미 0 (과도한 readUnlock 수행) 은 안 됨 if (readers_count_ != 0 && --((size_t&)readers_count_) == 0) { uk.unlock(); ((std::condition_variable&)read_cv_).notify_all(); ((std::condition_variable&)write_cv_).notify_all(); } }; /** * @brief Lock on writing * * @details * <p> Changes writing flag to true. </p> * * <p> If another write_lock or read_lock is on a progress, wait until them to be unlocked. </p> * * \li Writing can be done by only a section at once. * \li Writing can't be done when reading. * * @note You've to call write_unlock when writing work was terminated. */ void writeLock() { std::unique_lock<std::mutex> uk(write_mtx_); while (writing_ == true || readers_count_ > 0) write_cv_.wait(uk); writing_ = true; }; /** * @brief Unlock on writing */ void writeUnlock() { writing_ = false; read_cv_.notify_all(); write_cv_.notify_all(); }; }; }; }; #include <samchon/library/UniqueReadLock.hpp> #include <samchon/library/UniqueWriteLock.hpp> #include <samchon/library/SharedReadLock.hpp> #include <samchon/library/SharedWriteLock.hpp>
95  if (readers_count_ != 0 && --((size_t&)readers_count_) == 0)
96  {
97  uk.unlock();
98 
99  ((std::condition_variable&)read_cv_).notify_all();
100  ((std::condition_variable&)write_cv_).notify_all();
101  }
102  };
103 
117  void writeLock()
118  {
119  std::unique_lock<std::mutex> uk(write_mtx_);
120 
121  while (writing_ == true || readers_count_ > 0)
122  write_cv_.wait(uk);
123 
124  writing_ = true;
125  };
126 
130  void writeUnlock()
131  {
132  writing_ = false;
133 
134  read_cv_.notify_all();
135  write_cv_.notify_all();
136  };
137  };
138 };
139 };
140 
141 #include <samchon/library/UniqueReadLock.hpp>
142 #include <samchon/library/UniqueWriteLock.hpp>
143 
144 #include <samchon/library/SharedReadLock.hpp>
145 #include <samchon/library/SharedWriteLock.hpp>
void readUnlock() const
Unlock of read.
Definition: RWMutex.hpp:90
void readLock() const
Lock on read.
Definition: RWMutex.hpp:67
void writeLock()
Lock on writing.
Definition: RWMutex.hpp:117
RWMutex()
Default Constructor.
Definition: RWMutex.hpp:49
void writeUnlock()
Unlock on writing.
Definition: RWMutex.hpp:130