| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 BASE_LOCK_H_ | 5 #ifndef BASE_LOCK_H_ |
| 6 #define BASE_LOCK_H_ | 6 #define BASE_LOCK_H_ |
| 7 | 7 |
| 8 #include "base/lock_impl.h" | 8 #include "base/lock_impl.h" |
| 9 | 9 |
| 10 // A convenient wrapper for an OS specific critical section. | 10 // A convenient wrapper for an OS specific critical section. |
| 11 // | |
| 12 // NOTE: Although windows critical sections support recursive locks, we do not | |
| 13 // allow this, and we will commonly fire a DCHECK() if a thread attempts to | |
| 14 // acquire the lock a second time (while already holding it). | |
| 15 // | |
| 16 // Complication: UnitTest for DeathTests catch DCHECK exceptions, so we need | |
| 17 // to write code assuming DCHECK will throw. This means we need to save any | |
| 18 // assertable value in a local until we can safely throw. | |
| 19 | 11 |
| 20 class Lock { | 12 class Lock { |
| 21 public: | 13 public: |
| 22 Lock(); | 14 Lock() : lock_() {} |
| 23 ~Lock(); | 15 ~Lock() {} |
| 24 void Acquire(); | 16 void Acquire() { lock_.Lock(); } |
| 25 void Release(); | 17 void Release() { lock_.Unlock(); } |
| 26 // If the lock is not held, take it and return true. If the lock is already | 18 // If the lock is not held, take it and return true. If the lock is already |
| 27 // held by another thread, immediately return false. | 19 // held by another thread, immediately return false. |
| 28 bool Try(); | 20 bool Try() { return lock_.Try(); } |
| 29 | 21 |
| 30 // Return the underlying lock implementation. | 22 // Return the underlying lock implementation. |
| 31 // TODO(awalker): refactor lock and condition variables so that this is | 23 // TODO(awalker): refactor lock and condition variables so that this is |
| 32 // unnecessary. | 24 // unnecessary. |
| 33 LockImpl* lock_impl() { return &lock_; } | 25 LockImpl* lock_impl() { return &lock_; } |
| 34 | 26 |
| 35 private: | 27 private: |
| 36 LockImpl lock_; // User-supplied underlying lock implementation. | 28 LockImpl lock_; // Platform specific underlying lock implementation. |
| 37 | |
| 38 #ifndef NDEBUG | |
| 39 // All private data is implicitly protected by lock_. | |
| 40 // Be VERY careful to only access members under that lock. | |
| 41 int32 recursion_count_shadow_; | |
| 42 bool recursion_used_; // Allow debugging to continued after a DCHECK(). | |
| 43 int32 acquisition_count_; // Number of times lock was acquired. | |
| 44 int32 contention_count_; // Number of times there was contention. | |
| 45 #endif // NDEBUG | |
| 46 | 29 |
| 47 DISALLOW_COPY_AND_ASSIGN(Lock); | 30 DISALLOW_COPY_AND_ASSIGN(Lock); |
| 48 }; | 31 }; |
| 49 | 32 |
| 50 // A helper class that acquires the given Lock while the AutoLock is in scope. | 33 // A helper class that acquires the given Lock while the AutoLock is in scope. |
| 51 class AutoLock { | 34 class AutoLock { |
| 52 public: | 35 public: |
| 53 explicit AutoLock(Lock& lock) : lock_(lock) { | 36 explicit AutoLock(Lock& lock) : lock_(lock) { |
| 54 lock_.Acquire(); | 37 lock_.Acquire(); |
| 55 } | 38 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 77 | 60 |
| 78 ~AutoUnlock() { | 61 ~AutoUnlock() { |
| 79 lock_->Acquire(); | 62 lock_->Acquire(); |
| 80 } | 63 } |
| 81 | 64 |
| 82 Lock* lock_; | 65 Lock* lock_; |
| 83 }; | 66 }; |
| 84 | 67 |
| 85 #endif // BASE_LOCK_H_ | 68 #endif // BASE_LOCK_H_ |
| 86 | 69 |
| OLD | NEW |