comparison osx/include/kj/mutex.h @ 62:0994c39f1e94

Cap'n Proto v0.6 + build for OSX
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 22 May 2017 10:01:37 +0100
parents 3ab5a40c4e3b
children
comparison
equal deleted inserted replaced
61:d101c4099725 62:0994c39f1e94
156 // ======================================================================================= 156 // =======================================================================================
157 // Public interface 157 // Public interface
158 158
159 template <typename T> 159 template <typename T>
160 class Locked { 160 class Locked {
161 // Return type for `MutexGuarded<T>::lock()`. `Locked<T>` provides access to the guarded object 161 // Return type for `MutexGuarded<T>::lock()`. `Locked<T>` provides access to the bounded object
162 // and unlocks the mutex when it goes out of scope. 162 // and unlocks the mutex when it goes out of scope.
163 163
164 public: 164 public:
165 KJ_DISALLOW_COPY(Locked); 165 KJ_DISALLOW_COPY(Locked);
166 inline Locked(): mutex(nullptr), ptr(nullptr) {} 166 inline Locked(): mutex(nullptr), ptr(nullptr) {}
206 friend class MutexGuarded; 206 friend class MutexGuarded;
207 }; 207 };
208 208
209 template <typename T> 209 template <typename T>
210 class MutexGuarded { 210 class MutexGuarded {
211 // An object of type T, guarded by a mutex. In order to access the object, you must lock it. 211 // An object of type T, bounded by a mutex. In order to access the object, you must lock it.
212 // 212 //
213 // Write locks are not "recursive" -- trying to lock again in a thread that already holds a lock 213 // Write locks are not "recursive" -- trying to lock again in a thread that already holds a lock
214 // will deadlock. Recursive write locks are usually a sign of bad design. 214 // will deadlock. Recursive write locks are usually a sign of bad design.
215 // 215 //
216 // Unfortunately, **READ LOCKS ARE NOT RECURSIVE** either. Common sense says they should be. 216 // Unfortunately, **READ LOCKS ARE NOT RECURSIVE** either. Common sense says they should be.
221 // another read lock recursively, the result is deadlock. 221 // another read lock recursively, the result is deadlock.
222 222
223 public: 223 public:
224 template <typename... Params> 224 template <typename... Params>
225 explicit MutexGuarded(Params&&... params); 225 explicit MutexGuarded(Params&&... params);
226 // Initialize the mutex-guarded object by passing the given parameters to its constructor. 226 // Initialize the mutex-bounded object by passing the given parameters to its constructor.
227 227
228 Locked<T> lockExclusive() const; 228 Locked<T> lockExclusive() const;
229 // Exclusively locks the object and returns it. The returned `Locked<T>` can be passed by 229 // Exclusively locks the object and returns it. The returned `Locked<T>` can be passed by
230 // move, similar to `Own<T>`. 230 // move, similar to `Own<T>`.
231 // 231 //