Evo C++ Library v0.5.1
lock.h
Go to the documentation of this file.
1 // Evo C++ Library
2 /* Copyright 2019 Justin Crowell
3 Distributed under the BSD 2-Clause License -- see included file LICENSE.txt for details.
4 */
6 
7 #pragma once
8 #ifndef INCL_evo_lock_h
9 #define INCL_evo_lock_h
10 
11 namespace evo {
14 
16 
27 template<class T>
28 struct SmartLock {
33  SmartLock(T& object, bool lock=true) : object_(object) {
34  locked_ = false;
35  if (lock) {
36  object_.lock();
37  locked_ = true;
38  }
39  }
40 
43  if (locked_)
44  object_.unlock();
45  }
46 
51  if (!locked_) {
52  object_.lock();
53  locked_ = true;
54  }
55  return *this;
56  }
57 
62  if (locked_) {
63  object_.unlock();
64  locked_ = false;
65  }
66  return *this;
67  }
68 
69  T& object_;
70 
71 protected:
72  bool locked_;
73 };
74 
76 
86 template<class T>
87 struct SmartLockRead {
92  SmartLockRead(T& object, bool lock=true) : object_(object) {
93  locked_ = false;
94  if (lock) {
95  object_.lock_read();
96  locked_ = true;
97  }
98  }
99 
102  if (locked_)
103  object_.unlock_read();
104  }
105 
110  if (!locked_) {
111  object_.lock_read();
112  locked_ = true;
113  }
114  return *this;
115  }
116 
121  if (locked_) {
122  object_.unlock_read();
123  locked_ = false;
124  }
125  return *this;
126  }
127 
128  T& object_;
129 
130 protected:
131  bool locked_;
132 };
133 
135 
145 template<class T>
151  SmartSleepLock(T& object, ulong sleep_ms=1) : object_(object) {
152  locked_ = false;
153  if (sleep_ms > 0) {
154  object_.sleeplock(sleep_ms);
155  locked_ = true;
156  }
157  }
158 
161  if (locked_)
162  object_.unlock();
163  }
164 
169  SmartSleepLock& lock(ulong sleep_ms=1) {
170  if (!locked_ && sleep_ms > 0) {
171  object_.sleeplock(sleep_ms);
172  locked_ = true;
173  }
174  return *this;
175  }
176 
181  if (locked_) {
182  object_.unlock();
183  locked_ = false;
184  }
185  return *this;
186  }
187 
188  T& object_;
189 
190 protected:
191  bool locked_;
192 };
193 
195 }
197 #endif
SmartLock & lock()
Lock object, if not already locked by this.
Definition: lock.h:50
SmartLock & unlock()
Unlock object, if locked by this.
Definition: lock.h:61
SmartLock(T &object, bool lock=true)
Constructor.
Definition: lock.h:33
~SmartLock()
Destructor, unlocks if locked here.
Definition: lock.h:42
SmartLockRead(T &object, bool lock=true)
Constructor.
Definition: lock.h:92
Smart sleep-locking for synchronization.
Definition: lock.h:146
SmartLockRead & lock()
Read-Lock object, if not already locked by this.
Definition: lock.h:109
SmartLockRead & unlock()
Read-Unlock object, if locked by this.
Definition: lock.h:120
~SmartSleepLock()
Destructor, unlocks if locked here.
Definition: lock.h:160
bool locked_
Whether object is locked by this.
Definition: lock.h:72
T & object_
Synchronization object to lock.
Definition: lock.h:69
Smart locking for synchronization.
Definition: lock.h:28
SmartSleepLock(T &object, ulong sleep_ms=1)
Constructor.
Definition: lock.h:151
SmartSleepLock & lock(ulong sleep_ms=1)
Lock object, if not already locked by this.
Definition: lock.h:169
Evo C++ Library namespace.
Definition: alg.h:11
Smart read-locking for synchronization.
Definition: lock.h:87
SmartSleepLock & unlock()
Unlock object, if locked by this.
Definition: lock.h:180
~SmartLockRead()
Destructor, unlocks if locked here.
Definition: lock.h:101