EPICS Base 7.0.8.0
Loading...
Searching...
No Matches
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
30template < class T > class epicsGuardRelease;
31
52template < class T >
54public:
56
64 epicsGuard ( T & mutexIn);
65 void assertIdenticalMutex ( const T & ) const;
66 ~epicsGuard ();
67private:
68 T * _pTargetMutex;
69 epicsGuard ( const epicsGuard & );
70 epicsGuard & operator = ( const epicsGuard & );
71 friend class epicsGuardRelease < T >;
72};
73
74
101template < class T >
103public:
104 typedef epicsGuard<T> guard_t;
112 epicsGuardRelease ( epicsGuard < T > & guardIn);
114private:
115 epicsGuard < T > & _guard;
116 T * _pTargetMutex;
119};
120
121// same interface as epicsMutex
128public:
130 void lock ();
132 bool tryLock ();
134 void unlock ();
136 void show ( unsigned level ) const;
137};
138
139template < class T >
141 _pTargetMutex ( & mutexIn )
142{
143 _pTargetMutex->lock ();
144}
145
146template < class T >
148{
149 _pTargetMutex->unlock ();
150}
151
152template < class T >
154 const T & mutexToVerify ) const
155{
156 assert ( _pTargetMutex == & mutexToVerify );
157}
158
159template < 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
175template < class T >
177{
178 _pTargetMutex->lock ();
179 _guard._pTargetMutex = _pTargetMutex;
180}
181
182inline void epicsMutexNOOP::lock () {}
183inline bool epicsMutexNOOP::tryLock () { return true; }
184inline void epicsMutexNOOP::unlock () {}
185inline void epicsMutexNOOP::show ( unsigned ) const {}
186
187#endif // epicsGuardh
Provides an RAII style lock/unlock of a mutex.
Definition epicsGuard.h:53
epicsGuard(T &mutexIn)
Guard a mutex based on scope.
Definition epicsGuard.h:140
RAII style unlocking of an epicsGuard object.
Definition epicsGuard.h:102
Mutex-like object that does nothing.
Definition epicsGuard.h:127
void show(unsigned level) const
Does nothing.
Definition epicsGuard.h:185
void unlock()
Does nothing.
Definition epicsGuard.h:184
bool tryLock()
Does nothing, always returns true.
Definition epicsGuard.h:183
void lock()
Does nothing.
Definition epicsGuard.h:182
#define assert(exp)
Declare that a condition should be true.
Definition epicsAssert.h:71