Samchon Framework for CPP  1.0.0
UniqueReadLock.cpp
1 #include <samchon/library/RWMutex.hpp>
2 using namespace samchon::library;
3 
4 /* -----------------------------------------------------------
5  CONSTRUCTORS
6 ----------------------------------------------------------- */
7 UniqueReadLock::UniqueReadLock(const RWMutex &mtx, bool doLock)
8 {
9  if(doLock == true)
10  mtx.readLock();
11 
12  this->mtx = &mtx;
13  this->isLocked = doLock;
14 }
16 {
17  //MOVE
18  this->mtx = obj.mtx;
19  this->isLocked = obj.isLocked;
20 
21  //TRUNCATE
22  obj.mtx = nullptr;
23  obj.isLocked = nullptr;
24 }
26 {
27  if(isLocked == true)
28  mtx->readUnlock();
29 }
30 
31 /* -----------------------------------------------------------
32  LOCKERS
33 ----------------------------------------------------------- */
35 {
36  if(isLocked == true)
37  return;
38 
39  bool *flag = (bool*)&isLocked;
40 
41  mtx->readLock();
42  *flag = true;
43 }
45 {
46  if(isLocked == false)
47  return;
48 
49  bool *flag = (bool*)&isLocked;
50 
51  mtx->readUnlock();
52  *flag = false;
53 }
void lock() const
Lock on read.
UniqueReadLock(const RWMutex &, bool=true)
Construct from mutex.
~UniqueReadLock()
Default Destructor.
Package of libraries.
Definition: library.hpp:84
Unique lock for reading.
void readUnlock() const
Unlock of read.
Definition: RWMutex.cpp:39
bool isLocked
Whether the mutex was locked by UniqueLock.
const RWMutex * mtx
Managed mutex.
void unlock() const
Unlock of read.
void readLock() const
Lock on read.
Definition: RWMutex.cpp:31