Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lockguards.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_LOCKGUARDS_H_
19 #define ION_BASE_LOCKGUARDS_H_
20 
21 #include "base/macros.h"
22 #include "ion/base/readwritelock.h"
23 #include "ion/base/spinmutex.h"
24 #include "ion/port/mutex.h"
25 
26 namespace ion {
27 namespace base {
28 
31 
34 template <class MutexT>
35 class ION_API GenericLockGuardBase {
36  public:
40  bool IsLocked() const { return is_locked_; }
41 
44  void Lock() {
45  if (!is_locked_) {
46  mutex_.Lock();
47  is_locked_ = true;
48  }
49  }
50 
55  bool TryLock() {
56  if (!is_locked_) {
57  is_locked_ = mutex_.TryLock();
58  }
59  return is_locked_;
60  }
61 
63  void Unlock() {
64  if (is_locked_) {
65  mutex_.Unlock();
66  is_locked_ = false;
67  }
68  }
69 
70  protected:
72  explicit GenericLockGuardBase(MutexT* m) : mutex_(*m), is_locked_(false) {}
73 
76  ~GenericLockGuardBase() { Unlock(); }
77 
79  MutexT& mutex_;
81  bool is_locked_;
82 
83  private:
84  DISALLOW_IMPLICIT_CONSTRUCTORS(GenericLockGuardBase);
85 };
86 
89 template <class MutexT>
90 class ION_API GenericLockGuard : public GenericLockGuardBase<MutexT> {
91  public:
93  explicit GenericLockGuard(MutexT* m) : GenericLockGuardBase<MutexT>(m) {
94  this->mutex_.Lock();
95  this->is_locked_ = true;
96  }
97 
98  private:
99  DISALLOW_IMPLICIT_CONSTRUCTORS(GenericLockGuard);
100 };
101 
105 template <class MutexT>
106 class ION_API GenericTryLockGuard : public GenericLockGuardBase<MutexT> {
107  public:
109  explicit GenericTryLockGuard(MutexT* m) : GenericLockGuardBase<MutexT>(m) {
110  this->TryLock();
111  }
112 
113  private:
114  DISALLOW_IMPLICIT_CONSTRUCTORS(GenericTryLockGuard);
115 };
116 
120 template <class MutexT>
121 class ION_API GenericUnlockGuard {
122  public:
124  explicit GenericUnlockGuard(MutexT* m) : mutex_(*m) {
125  mutex_.Unlock();
126  }
127 
129  mutex_.Lock();
130  }
131 
132  private:
133  MutexT& mutex_;
134 
135  DISALLOW_IMPLICIT_CONSTRUCTORS(GenericUnlockGuard);
136 };
137 
141 template <typename T>
142 class ION_API ManualLockGuard : public GenericLockGuardBase<port::Mutex> {
143  public:
146  explicit ManualLockGuard(const T& initial_value)
147  : GenericLockGuardBase<port::Mutex>(&local_mutex_),
148  initial_value_(initial_value),
149  current_value_(initial_value) {
150  is_locked_ = false;
151  }
152 
156  ~ManualLockGuard() { Unlock(); }
157 
160  void SetAndLock(const T& new_value) {
161  Lock();
162  current_value_ = new_value;
163  }
164 
167  const T ResetAndUnlock() {
168  const T ret = current_value_;
169  current_value_ = initial_value_;
170  Unlock();
171  return ret;
172  }
173 
176  const T GetCurrentValue() {
177  Lock();
178  T ret = current_value_;
179  Unlock();
180  return ret;
181  }
182 
183  private:
184  port::Mutex local_mutex_;
185  T initial_value_;
186  T current_value_;
187 
188  DISALLOW_IMPLICIT_CONSTRUCTORS(ManualLockGuard);
189 };
190 
195 
200 
204 
205 } // namespace base
206 } // namespace ion
207 
208 #endif // ION_BASE_LOCKGUARDS_H_
GenericUnlockGuard(MutexT *m)
The passed pointer must be non-NULL.
Definition: lockguards.h:124
ManualLockGuard(const T &initial_value)
The constructor is passed the initial value of the variable being protected.
Definition: lockguards.h:146
GenericLockGuard< port::Mutex > LockGuard
Convenient typedefs for ion::port::Mutex.
Definition: lockguards.h:192
A ManualLockGuard can be used to protect a variable with a mutex in situations where it is not possib...
Definition: lockguards.h:142
GenericLockGuard(MutexT *m)
The passed pointer must be non-NULL.
Definition: lockguards.h:93
GenericLockGuard< SpinMutex > SpinLockGuard
Convenient typedefs for SpinMutex.
Definition: lockguards.h:197
An UnlockGuard is the reverse of a LockGuard; it unlocks a mutex when created and locks it when destr...
Definition: lockguards.h:121
const T ResetAndUnlock()
Resets the variable's value to the initial value (passed to the constructor), unlocks the mutex...
Definition: lockguards.h:167
A LockGuard locks a mutex when created, and unlocks it when destroyed.
Definition: lockguards.h:90
void SetAndLock(const T &new_value)
Sets the variable's value and locks the mutex.
Definition: lockguards.h:160
GenericTryLockGuard< port::Mutex > TryLockGuard
Definition: lockguards.h:194
bool IsLocked() const
Returns whether this guard has locked the mutex; returns false even if another guard has it locked...
Definition: lockguards.h:40
A TryLockGuard attempts to lock a mutex when created, and if successful, will unlock it when destroye...
Definition: lockguards.h:106
~ManualLockGuard()
The destructor unlocks the mutex if necessary.
Definition: lockguards.h:156
GenericTryLockGuard< SpinMutex > SpinTryLockGuard
Definition: lockguards.h:199
~GenericLockGuardBase()
This destructor intentionally non-virtual for speed.
Definition: lockguards.h:76
GenericLockGuardBase(MutexT *m)
The constructor is protected since this is an abstract base class.
Definition: lockguards.h:72
GenericLockGuard< ReadLock > ReadGuard
Convenient typedefs for ReadWriteLock.
Definition: lockguards.h:202
void Unlock()
Releases a lock on the mutex if it was previously locked by this guard.
Definition: lockguards.h:63
const T GetCurrentValue()
Returns the current value of the variable.
Definition: lockguards.h:176
bool TryLock()
Attempts to lock the mutex if the mutex is not already locked by this guard.
Definition: lockguards.h:55
MutexT & mutex_
The mutex used for locking.
Definition: lockguards.h:79
port::Mutex mutex_
Protects shared access to the Allocator and FT_Library.
bool is_locked_
Whether the mutex is currently locked by this guard.
Definition: lockguards.h:81
GenericUnlockGuard< SpinMutex > SpinUnlockGuard
Definition: lockguards.h:198
This file contains utility classes for automatically locking and unlocking mutexes.
Definition: lockguards.h:35
void Lock()
Locks the mutex if it is not already locked by this guard.
Definition: lockguards.h:44
GenericLockGuard< WriteLock > WriteGuard
Definition: lockguards.h:203
GenericUnlockGuard< port::Mutex > UnlockGuard
Definition: lockguards.h:193
A Mutex is used to ensure that only one thread or process can access a block of code at one time...
Definition: mutex.h:34
GenericTryLockGuard(MutexT *m)
The passed pointer must be non-NULL.
Definition: lockguards.h:109