| 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 #ifndef LIBRARIES_SDK_UTIL_AUTO_LOCK_H_ | 5 #ifndef LIBRARIES_SDK_UTIL_AUTO_LOCK_H_ |
| 6 #define LIBRARIES_SDK_UTIL_AUTO_LOCK_H_ | 6 #define LIBRARIES_SDK_UTIL_AUTO_LOCK_H_ |
| 7 | 7 |
| 8 #include <pthread.h> | 8 #include <pthread.h> |
| 9 #include "sdk_util/macros.h" | 9 #include "sdk_util/macros.h" |
| 10 #include "sdk_util/simple_lock.h" | 10 #include "sdk_util/simple_lock.h" |
| 11 | 11 |
| 12 namespace sdk_util { | 12 namespace sdk_util { |
| 13 | 13 |
| 14 // This macro is provided to allow us to quickly instrument locking for | 14 // This macro is provided to allow us to quickly instrument locking for |
| 15 // debugging purposes. | 15 // debugging purposes. |
| 16 #define AUTO_LOCK(lock) \ | 16 #define AUTO_LOCK(lock) \ |
| 17 ::sdk_util::AutoLock Lock##__LINE__(lock); | 17 ::sdk_util::AutoLock Lock##__LINE__(lock); |
| 18 | 18 |
| 19 class AutoUnlock; |
| 20 |
| 19 class AutoLock { | 21 class AutoLock { |
| 20 public: | 22 public: |
| 21 AutoLock(const SimpleLock& lock) { | 23 AutoLock(const SimpleLock& lock) { |
| 22 lock_ = lock.mutex(); | 24 lock_ = lock.mutex(); |
| 23 pthread_mutex_lock(lock_); | 25 pthread_mutex_lock(lock_); |
| 24 } | 26 } |
| 25 | 27 |
| 26 ~AutoLock() { | 28 ~AutoLock() { |
| 27 Unlock(); | 29 Unlock(); |
| 28 } | 30 } |
| 29 | 31 |
| 30 void Unlock() { | 32 void Unlock() { |
| 31 if (lock_) pthread_mutex_unlock(lock_); | 33 if (lock_) pthread_mutex_unlock(lock_); |
| 32 lock_ = NULL; | 34 lock_ = NULL; |
| 33 } | 35 } |
| 34 | 36 |
| 35 private: | 37 private: |
| 36 pthread_mutex_t* lock_; | 38 pthread_mutex_t* lock_; |
| 37 | 39 |
| 40 friend class AutoUnlock; |
| 38 DISALLOW_COPY_AND_ASSIGN(AutoLock); | 41 DISALLOW_COPY_AND_ASSIGN(AutoLock); |
| 39 }; | 42 }; |
| 40 | 43 |
| 44 |
| 45 class AutoUnlock { |
| 46 public: |
| 47 AutoUnlock() { |
| 48 simple_lock_ = NULL; |
| 49 } |
| 50 |
| 51 ~AutoUnlock() { |
| 52 Unlock(); |
| 53 } |
| 54 |
| 55 void Unlock() { |
| 56 if (simple_lock_) |
| 57 simple_lock_->Unlock(); |
| 58 |
| 59 simple_lock_ = NULL; |
| 60 } |
| 61 |
| 62 void Take(const SimpleLock& lock) { |
| 63 if (simple_lock_ != &lock) |
| 64 Unlock(); |
| 65 |
| 66 simple_lock_ = &lock; |
| 67 } |
| 68 |
| 69 const SimpleLock* Give() { |
| 70 const SimpleLock* out = simple_lock_; |
| 71 simple_lock_ = NULL; |
| 72 return out; |
| 73 } |
| 74 |
| 75 private: |
| 76 const SimpleLock* simple_lock_; |
| 77 DISALLOW_COPY_AND_ASSIGN(AutoUnlock); |
| 78 }; |
| 79 |
| 41 } // namespace sdk_util | 80 } // namespace sdk_util |
| 42 | 81 |
| 43 #endif // LIBRARIES_SDK_UTIL_AUTO_LOCK_H_ | 82 #endif // LIBRARIES_SDK_UTIL_AUTO_LOCK_H_ |
| OLD | NEW |