Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cThreadLockRef.h
Go to the documentation of this file.
1 //
5 
6 #ifndef _INC_cThreadLockRef_H
7 #define _INC_cThreadLockRef_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cThreadLock.h"
13 #include "cRefPtr.h"
14 
15 namespace Gray
16 {
18  : public cRefBase
19  , public cThreadLockCount
20  {
25  public:
26  cThreadLockableRef(int iStaticRefCount = 0) noexcept
27  : cRefBase(iStaticRefCount)
28  {
29  }
31  {
32  }
33  virtual void onThreadLockFail(TIMESYSD_t dwWaitMS)
34  {
36  UNREFERENCED_PARAMETER(dwWaitMS);
37  }
38  };
39 
40  //****************************************************************************
41 
42  template<class TYPE = cThreadLockableRef >
43  class cThreadLockRef : public cRefPtr < TYPE >
44  {
53 
55 
56 #if defined(_MT) || defined(__linux__)
58 
59  private:
60  void SetFirstLockObj(TYPE* p2)
61  {
63  if (p2 != nullptr)
64  {
65 #ifdef _DEBUG
66  ASSERT(DYNPTR_CAST(cThreadLockableRef, p2) != nullptr);
67  ASSERT(DYNPTR_CAST(TYPE, p2) != nullptr);
68 #endif
69  p2->IncRefCount();
70  p2->Lock();
71  }
72  this->m_p = p2;
73  }
74  bool SetFirstLockObjTry(TYPE* p2, TIMESYSD_t dwWaitMS)
75  {
78  if (p2 != nullptr)
79  {
80 #ifdef _DEBUG
81  ASSERT(DYNPTR_CAST(cThreadLockableRef, p2) != nullptr);
82  ASSERT(DYNPTR_CAST(TYPE, p2) != nullptr);
83 #endif
84  p2->IncRefCount();
85  if (!p2->LockTry(dwWaitMS))
86  {
87  if (dwWaitMS)
88  {
89  p2->onThreadLockFail(dwWaitMS);
90  }
91  p2->DecRefCount();
92  this->m_p = nullptr;
93  return false;
94  }
95  }
96  this->m_p = p2;
97  return true;
98  }
99 
100  public:
101  // Construct and destruct
103  {
104  }
105  cThreadLockRef(TYPE* p2)
106  {
108  SetFirstLockObj(p2);
109  }
110  cThreadLockRef(TYPE* p2, TIMESYSD_t dwWaitMS)
111  {
114  SetFirstLockObjTry(p2, dwWaitMS);
115  }
116  cThreadLockRef(const THIS_t& ref)
117  {
119  SetFirstLockObj(ref.get_Ptr());
120  }
121  ~cThreadLockRef()
122  {
123  ReleasePtr();
124  }
125 
126  void ReleasePtr()
127  {
128  TYPE* p2 = this->m_p; // make local copy
129  if (p2 != nullptr)
130  {
131  this->m_p = nullptr;
132 #ifdef _DEBUG
133  ASSERT(DYNPTR_CAST(TYPE, p2) != nullptr);
134 #endif
135  p2->Unlock();
136  p2->DecRefCount();
137  }
138  }
139 
140  operator TYPE* () const
141  {
142  return(this->m_p);
143  }
144  TYPE& operator * () const
145  {
146  ASSERT(this->m_p != nullptr); return *(this->m_p);
147  }
148  TYPE* operator -> () const
149  {
150  ASSERT(this->m_p != nullptr); return(this->m_p);
151  }
152 
153  void put_Ptr(TYPE* p2)
154  {
156  if (p2 == this->m_p)
157  return;
158  ReleasePtr();
159  SetFirstLockObj(p2);
160  }
161  bool SetLockObjTry(TYPE* p2, TIMESYSD_t dwWaitMS)
162  {
163  if (p2 == this->m_p)
164  return true;
165  ReleasePtr();
166  return SetFirstLockObjTry(p2, dwWaitMS);
167  }
168 
169  // Assignment
170  const THIS_t& operator = (TYPE* p2)
171  {
172  put_Ptr(p2);
173  return *this;
174  }
175  const THIS_t& operator = (const THIS_t& ref)
176  {
177  put_Ptr(ref.m_p);
178  return *this;
179  }
180 #endif // defined(_MT) || __linux__
181  };
182 
184 }
185 
186 #endif // _INC_cThreadLockRef_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define DYNPTR_CAST(t, p)
Definition: PtrCast.h:22
#define TYPE
Definition: StrT.cpp:38
#define UNREFERENCED_PARAMETER(P)
< _WIN32 type thing. get rid of stupid warning.
Definition: SysTypes.h:299
#define ASSERT(exp)
Definition: cDebugAssert.h:87
Definition: cPtrFacade.h:19
TYPE * m_p
Pointer to some object of TYPE.
Definition: cPtrFacade.h:28
TYPE & operator*() const
Definition: cPtrFacade.h:111
TYPE * get_Ptr() const noexcept
Definition: cPtrFacade.h:53
TYPE * operator->() const
Definition: cPtrFacade.h:116
Definition: cRefPtr.h:22
Definition: cRefPtr.h:225
THIS_t & operator=(const cThreadLockableRef *p2)
Definition: cRefPtr.h:342
void ReleasePtr()
Definition: cRefPtr.h:320
void put_Ptr(cThreadLockableRef *p)
Definition: cRefPtr.h:310
Definition: cThreadLockRef.h:44
Definition: cThreadLock.h:498
Definition: cThreadLockRef.h:20
virtual ~cThreadLockableRef()
Definition: cThreadLockRef.h:30
cThreadLockableRef(int iStaticRefCount=0) noexcept
Definition: cThreadLockRef.h:26
virtual void onThreadLockFail(TIMESYSD_t dwWaitMS)
Definition: cThreadLockRef.h:33
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
INT32 TIMESYSD_t
Time delta. signed milli-Seconds Span. cTimeSys::k_DMAX, cTimeSys::k_INF = MAILSLOT_WAIT_FOREVER.
Definition: cTimeSys.h:28
cThreadLockRef< cThreadLockableRef > cThreadLockRefX
Definition: cThreadLockRef.h:183