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" | 10 #include "sdk_util/macros.h" |
11 #include "sdk_util/simple_lock.h" | 11 #include "sdk_util/simple_lock.h" |
12 | 12 |
| 13 namespace sdk_util { |
13 | 14 |
14 // This macro is provided to allow us to quickly instrument locking for | 15 // This macro is provided to allow us to quickly instrument locking for |
15 // debugging purposes. | 16 // debugging purposes. |
16 #define AUTO_LOCK(lock) \ | 17 #define AUTO_LOCK(lock) \ |
17 AutoLock Lock##__LINE__(lock); | 18 ::sdk_util::AutoLock Lock##__LINE__(lock); |
18 | 19 |
19 class AutoLock { | 20 class AutoLock { |
20 public: | 21 public: |
21 AutoLock(const SimpleLock& lock) { | 22 AutoLock(const SimpleLock& lock) { |
22 lock_ = lock.mutex(); | 23 lock_ = lock.mutex(); |
23 pthread_mutex_lock(lock_); | 24 pthread_mutex_lock(lock_); |
24 } | 25 } |
25 | 26 |
26 ~AutoLock() { | 27 ~AutoLock() { |
27 if (lock_) pthread_mutex_unlock(lock_); | 28 if (lock_) pthread_mutex_unlock(lock_); |
28 } | 29 } |
29 | 30 |
30 void Unlock() { | 31 void Unlock() { |
31 if (lock_) pthread_mutex_unlock(lock_); | 32 if (lock_) pthread_mutex_unlock(lock_); |
32 lock_ = NULL; | 33 lock_ = NULL; |
33 } | 34 } |
34 | 35 |
35 private: | 36 private: |
36 pthread_mutex_t* lock_; | 37 pthread_mutex_t* lock_; |
37 | 38 |
38 DISALLOW_COPY_AND_ASSIGN(AutoLock); | 39 DISALLOW_COPY_AND_ASSIGN(AutoLock); |
39 }; | 40 }; |
40 | 41 |
| 42 } // namespace sdk_util |
| 43 |
41 #endif // LIBRARIES_SDK_UTIL_AUTO_LOCK_H_ | 44 #endif // LIBRARIES_SDK_UTIL_AUTO_LOCK_H_ |
42 | 45 |
OLD | NEW |