EPICS ARCHIVER V4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups
Guard.h
1 // -*- c++ -*-
2 
3 #ifndef _GUARD_H_
4 #define _GUARD_H_
5 
6 // tools
7 #include "tools/GenericException.h"
8 #include "tools/OrderedMutex.h"
9 #include "tools/NoCopy.h"
10 
20 {
21 public:
22 
24  epicsMutexGuard(epicsMutex &mutex) : mutex(mutex)
25  {
26  mutex.lock();
27  }
28 
31  {
32  mutex.unlock();
33  }
34 
36  void check(const char *file, size_t line,
37  const epicsMutex &the_one_it_should_be);
38 
39 private:
40  PROHIBIT_DEFAULT_COPY(epicsMutexGuard);
41  epicsMutex &mutex;
42 };
43 
49 class Guardable
50 {
51 public:
52  virtual ~Guardable();
53 
55  virtual OrderedMutex &getMutex() = 0;
56 };
57 
63 class Guard
64 {
65 public:
67  Guard(const char *file, size_t line, Guardable &guardable)
68  : mutex(guardable.getMutex()), is_locked(false)
69  {
70  lock(file, line);
71  is_locked = true;
72  }
73 
75  Guard(const char *file, size_t line, OrderedMutex &mutex)
76  : mutex(mutex), is_locked(false)
77  {
78  lock(file, line);
79  is_locked = true;
80  }
81 
84  {
85  if (!is_locked)
86  throw GenericException(__FILE__, __LINE__,
87  "Found a released lock in Guard::~Guard()");
88  mutex.unlock();
89  }
90 
95  void check(const char *file, size_t line,
96  const OrderedMutex &the_one_it_should_be);
97 
99  void unlock()
100  {
101  mutex.unlock();
102  is_locked = false;
103  }
104 
106  void lock(const char *file, size_t line);
107 
109  bool isLocked()
110  {
111  return is_locked;
112  }
113 
114 private:
115  PROHIBIT_DEFAULT_COPY(Guard);
116  OrderedMutex &mutex;
117  bool is_locked;
118 };
119 
120 
127 {
128 public:
130  GuardRelease(const char *file, size_t line, Guard &guard)
131  : file(file), line(line), guard(guard)
132  {
133  guard.unlock();
134  }
135 
138  {
139  guard.lock(file, line);
140  }
141 
142 private:
143  PROHIBIT_DEFAULT_COPY(GuardRelease);
144  const char *file;
145  size_t line;
146  Guard &guard;
147 };
148 
149 #endif
void check(const char *file, size_t line, const epicsMutex &the_one_it_should_be)
Check.
~GuardRelease()
Destructor re-locks the guard.
Definition: Guard.h:137
epicsMutexGuard(epicsMutex &mutex)
Constructor locks mutex.
Definition: Guard.h:24
A mutex with informational name and lock order.
Definition: OrderedMutex.h:34
Guard(const char *file, size_t line, OrderedMutex &mutex)
Constructor attaches to mutex and locks.
Definition: Guard.h:75
void unlock()
Unlock the mutex.
Automatically takes and releases an epicsMutex.
Definition: Guard.h:19
~epicsMutexGuard()
Destructor unlocks mutex.
Definition: Guard.h:30
~Guard()
Destructor unlocks.
Definition: Guard.h:83
virtual OrderedMutex & getMutex()=0
Automatically takes and releases an epicsMutex.
Definition: Guard.h:63
bool isLocked()
Definition: Guard.h:109
void lock(const char *file, size_t line)
Lock again after a temporary unlock.
Interface for something that can be protected by a Guard.
Definition: Guard.h:49
void unlock()
Unlock, meant for temporary, manual unlock().
Definition: Guard.h:99
GuardRelease(const char *file, size_t line, Guard &guard)
Constructor releases the guard.
Definition: Guard.h:130
Generic Exception: Base class for exceptions.
Definition: GenericException.h:45
Guard(const char *file, size_t line, Guardable &guardable)
Constructor attaches to mutex and locks.
Definition: Guard.h:67
Temporarily releases and then re-takes a Guard.
Definition: Guard.h:126
void check(const char *file, size_t line, const OrderedMutex &the_one_it_should_be)
Check if the guard is assigned to the correct mutex.