Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cThreadLockRW.h
Go to the documentation of this file.
1 //
7 
8 #ifndef _INC_cThreadLockRW_H
9 #define _INC_cThreadLockRW_H
10 #ifndef NO_PRAGMA_ONCE
11 #pragma once
12 #endif
13 
14 #include "cThreadLock.h"
15 #include "cInterlockedVal.h"
16 #include "cNonCopyable.h"
17 #include "cThreadLockRef.h"
18 
19 namespace Gray
20 {
22  {
28 
29  private:
30  cInterlockedInt m_nReaders;
31  cInterlockedInt m_nWriters;
32 
33  public:
35  {
36  for (;;)
37  {
38  m_nReaders.IncV();
39  if (m_nWriters == 0)
40  break;
41  m_nReaders.DecV();
43  }
44  }
46  {
47  m_nReaders.DecV();
48  }
49 
50  void Lock()
51  {
52  // Write lock
53  for (;;)
54  {
55  if (m_nWriters.Exchange(1) == 1)
56  {
58  }
59  else
60  {
61  while (m_nReaders != 0)
63  break;
64  }
65  }
66  }
67 
68  void Unlock()
69  {
70  // Write unlock
71  m_nWriters.DecV();
72  }
73  };
74 
76  {
92 
93  typedef cThreadLockFast SUPER_t;
94 
95  public:
96  // cThreadLock::m_nLockThreadID; // First reader or writers .
97  // cThreadLock::m_nLockCount; // How many write locks (for orig m_nLockThreadID)
98  cInterlockedInt m_nReadLockCount;
99  cInterlockedInt m_nOtherReadLockCount;
101 
102  public:
104  : m_bLostOrder(false)
105  {
106  }
108  {
109  ASSERT(m_nReadLockCount == 0);
110  ASSERT(m_nOtherReadLockCount == 0);
111  }
112 
113  inline void IncReadLockCount()
114  {
115  // this is a slightly softer READ lock.
116  if (SUPER_t::LockTry())
117  {
118  // I'm first locker or at least the same thread as the first.
119  m_nReadLockCount.IncV();
120  return;
121  }
122  // some other thread has the write/read lock before me. can i just read it ?
123  m_nOtherReadLockCount.IncV();
124  if (m_nReadLockCount == get_LockCount()) // another reader is OK.
125  {
126  }
127  else
128  {
129  }
130  }
131  inline void DecReadLockCount()
132  {
133  }
135  };
136 
137  class cThreadGuardRead : public cLockerT < cThreadLockRW >
138  {
141 
142  public:
144  : cLockerT<cThreadLockRW>(rLock)
145  {
146  }
147  // TODO: call IncReadLockCount
148  };
149 
150  typedef cLockerT<cThreadLockRW> cThreadGuardWrite; // I only want to write to this.
151 
152  //******************************************************
153 
155  : public cRefBase
156  , public cThreadLockRW
157  {
161  };
162 
163  template<class TYPE>
164  class CSmartReadPtr : public cRefPtr<TYPE>, public cThreadGuardRead
165  {
173 
174  CSmartReadPtr(TYPE* pObj)
175  : cRefPtr<TYPE>(pObj)
176  , cThreadGuardRead(*pObj)
177  {
178  }
179 
180  const TYPE* get_Ptr() const
181  {
182  return(this->m_pObj);
183  }
184  };
185  template<class TYPE>
186  class CSmartWritePtr : public cRefPtr<TYPE>, public cThreadGuardWrite
187  {
191  CSmartWritePtr(TYPE* pObj)
192  : cRefPtr<TYPE>(pObj)
193  , cThreadGuardWrite(*pObj)
194  {
195  }
196  };
197 };
198 #endif // _INC_cThreadLockRW_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define TYPE
Definition: StrT.cpp:38
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
Definition: cThreadLockRW.h:165
Definition: cThreadLockRW.h:187
Definition: cLocker.h:72
Definition: cNonCopyable.h:17
Definition: cRefPtr.h:22
Definition: cRefPtr.h:225
Definition: cThreadLockRW.h:138
cThreadGuardRead(cThreadLockRW &rLock)
Definition: cThreadLockRW.h:143
static void SleepCurrent(TIMESYS_t uMs=cTimeSys::k_FREQ) noexcept
Definition: cThreadLock.h:122
Definition: cThreadLock.h:173
Definition: cThreadLock.h:205
Definition: cThreadLockRW.h:22
void DecReadLockCount()
Definition: cThreadLockRW.h:45
void Lock()
Definition: cThreadLockRW.h:50
void IncReadLockCount()
Definition: cThreadLockRW.h:34
void Unlock()
Definition: cThreadLockRW.h:68
Definition: cThreadLockRW.h:76
~cThreadLockRW()
Definition: cThreadLockRW.h:107
cThreadLockRW()
Definition: cThreadLockRW.h:103
void IncReadLockCount()
Definition: cThreadLockRW.h:113
void DecReadLockCount()
Definition: cThreadLockRW.h:131
bool m_bLostOrder
can't figure who is thread.
Definition: cThreadLockRW.h:100
cInterlockedInt m_nReadLockCount
How many readers (for orig m_nLockThreadID)
Definition: cThreadLockRW.h:98
cInterlockedInt m_nOtherReadLockCount
How many outside (not on orig thread) readers.
Definition: cThreadLockRW.h:99
Definition: cThreadLockRW.h:157
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
cLockerT< cThreadLockRW > cThreadGuardWrite
Definition: cThreadLockRW.h:150