Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 #ifndef LIBRARIES_SDK_UTIL_AUTO_LOCK_H_ | 6 #ifndef LIBRARIES_SDK_UTIL_AUTO_LOCK_H_ |
| 7 #define LIBRARIES_SDK_UTIL_AUTO_LOCK_H_ | 7 #define LIBRARIES_SDK_UTIL_AUTO_LOCK_H_ |
| 8 | 8 |
| 9 #include <pthread.h> | 9 #include <pthread.h> |
| 10 #include "sdk_util/macros.h" | |
| 11 #include "sdk_util/simple_lock.h" | |
| 12 | |
| 13 | |
| 14 // This macro is provided to allow us to quickly instrument locking for | |
| 15 // debugging perposes. | |
|
binji
2013/07/13 00:16:48
sp: purposes
| |
| 16 #define AUTO_LOCK(lock) \ | |
| 17 AutoLock Lock##__LINE__(lock); | |
| 10 | 18 |
| 11 class AutoLock { | 19 class AutoLock { |
| 12 public: | 20 public: |
| 13 explicit AutoLock(pthread_mutex_t* lock) { | 21 AutoLock(const SimpleLock& lock) { |
| 14 lock_ = lock; | 22 lock_ = lock.mutex(); |
| 15 pthread_mutex_lock(lock_); | 23 pthread_mutex_lock(lock_); |
| 16 } | 24 } |
| 25 | |
| 17 ~AutoLock() { | 26 ~AutoLock() { |
| 18 if (lock_) pthread_mutex_unlock(lock_); | 27 if (lock_) pthread_mutex_unlock(lock_); |
| 19 } | 28 } |
| 20 | 29 |
| 21 void Unlock() { | 30 void Unlock() { |
| 22 if (lock_) pthread_mutex_unlock(lock_); | 31 if (lock_) pthread_mutex_unlock(lock_); |
| 23 lock_ = NULL; | 32 lock_ = NULL; |
| 24 } | 33 } |
| 25 | 34 |
| 26 private: | 35 private: |
| 27 pthread_mutex_t* lock_; | 36 pthread_mutex_t* lock_; |
| 37 | |
| 38 DISALLOW_COPY_AND_ASSIGN(AutoLock); | |
| 28 }; | 39 }; |
| 29 | 40 |
| 30 #endif // LIBRARIES_SDK_UTIL_AUTO_LOCK_H_ | 41 #endif // LIBRARIES_SDK_UTIL_AUTO_LOCK_H_ |
| 31 | 42 |
| OLD | NEW |