EPICS Base  7.0.8.1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
epicsGuard.h
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * SPDX-License-Identifier: EPICS
7 * EPICS Base is distributed subject to a Software License Agreement found
8 * in file LICENSE that is included with this distribution.
9 \*************************************************************************/
10 
19 #ifndef epicsGuardh
20 #define epicsGuardh
21 
22 #ifndef assert // allow use of epicsAssert.h
23 # include <cassert>
24 #endif
25 
26 /*
27  * Author: Jeffrey O. Hill
28  */
29 
30 template < class T > class epicsGuardRelease;
31 
52 template < class T >
53 class epicsGuard {
54 public:
56 
64  epicsGuard ( T & mutexIn);
65  void assertIdenticalMutex ( const T & ) const;
66  ~epicsGuard ();
67 private:
68  T * _pTargetMutex;
69  epicsGuard ( const epicsGuard & );
70  epicsGuard & operator = ( const epicsGuard & );
71  friend class epicsGuardRelease < T >;
72 };
73 
74 
101 template < class T >
102 class epicsGuardRelease {
103 public:
104  typedef epicsGuard<T> guard_t;
112  epicsGuardRelease ( epicsGuard < T > & guardIn);
113  ~epicsGuardRelease ();
114 private:
115  epicsGuard < T > & _guard;
116  T * _pTargetMutex;
118  epicsGuardRelease & operator = ( const epicsGuardRelease & );
119 };
120 
121 // same interface as epicsMutex
128 public:
130  void lock ();
132  bool tryLock ();
134  void unlock ();
136  void show ( unsigned level ) const;
137 };
138 
139 template < class T >
140 inline epicsGuard < T > :: epicsGuard ( T & mutexIn ) :
141  _pTargetMutex ( & mutexIn )
142 {
143  _pTargetMutex->lock ();
144 }
145 
146 template < class T >
147 inline epicsGuard < T > :: ~epicsGuard ()
148 {
149  _pTargetMutex->unlock ();
150 }
151 
152 template < class T >
153 inline void epicsGuard < T > :: assertIdenticalMutex (
154  const T & mutexToVerify ) const
155 {
156  assert ( _pTargetMutex == & mutexToVerify );
157 }
158 
159 template < class T >
162  _guard ( guardIn ),
163  _pTargetMutex ( guardIn._pTargetMutex )
164 {
165  // Setting the guard's _pTargetMutex to nill will
166  // allow assertIdenticalMutex to catch situations
167  // where a guard is being used and it has been
168  // released, and also situations where ~epicsGuard ()
169  // runs and an epicsGuardRelease is still referencing
170  // the guard will be detected.
171  _guard._pTargetMutex = 0;
172  _pTargetMutex->unlock ();
173 }
174 
175 template < class T >
176 inline epicsGuardRelease < T > :: ~epicsGuardRelease ()
177 {
178  _pTargetMutex->lock ();
179  _guard._pTargetMutex = _pTargetMutex;
180 }
181 
182 inline void epicsMutexNOOP::lock () {}
183 inline bool epicsMutexNOOP::tryLock () { return true; }
184 inline void epicsMutexNOOP::unlock () {}
185 inline void epicsMutexNOOP::show ( unsigned ) const {}
186 
187 #endif // epicsGuardh
#define assert(exp)
Declare that a condition should be true.
Definition: epicsAssert.h:71
RAII style unlocking of an epicsGuard object.
Definition: epicsGuard.h:30
bool tryLock()
Does nothing, always returns true.
Definition: epicsGuard.h:183
void unlock()
Does nothing.
Definition: epicsGuard.h:184
epicsGuard(T &mutexIn)
Guard a mutex based on scope.
Definition: epicsGuard.h:140
epicsGuardRelease(epicsGuard< T > &guardIn)
Constructs an epicsGuardRelease, unlocking the given epicsGuard.
Definition: epicsGuard.h:161
void show(unsigned level) const
Does nothing.
Definition: epicsGuard.h:185
void lock()
Does nothing.
Definition: epicsGuard.h:182
Provides an RAII style lock/unlock of a mutex.
Definition: epicsGuard.h:53
Mutex-like object that does nothing.
Definition: epicsGuard.h:127