OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_SYNCHRONIZATION_LOCK_H_ | 5 #ifndef BASE_SYNCHRONIZATION_LOCK_H_ |
6 #define BASE_SYNCHRONIZATION_LOCK_H_ | 6 #define BASE_SYNCHRONIZATION_LOCK_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/synchronization/lock_impl.h" | 9 #include "base/synchronization/lock_impl.h" |
10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 // A convenient wrapper for an OS specific critical section. The only real | 14 // A convenient wrapper for an OS specific critical section. The only real |
15 // intelligence in this class is in debug mode for the support for the | 15 // intelligence in this class is in debug mode for the support for the |
16 // AssertAcquired() method. | 16 // AssertAcquired() method. |
17 class BASE_EXPORT Lock { | 17 class BASE_EXPORT Lock { |
18 public: | 18 public: |
19 #if defined(NDEBUG) // Optimized wrapper implementation | 19 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
20 // Optimized wrapper implementation | |
20 Lock() : lock_() {} | 21 Lock() : lock_() {} |
21 ~Lock() {} | 22 ~Lock() {} |
22 void Acquire() { lock_.Lock(); } | 23 void Acquire() { lock_.Lock(); } |
23 void Release() { lock_.Unlock(); } | 24 void Release() { lock_.Unlock(); } |
24 | 25 |
25 // If the lock is not held, take it and return true. If the lock is already | 26 // If the lock is not held, take it and return true. If the lock is already |
26 // held by another thread, immediately return false. This must not be called | 27 // held by another thread, immediately return false. This must not be called |
27 // by a thread already holding the lock (what happens is undefined and an | 28 // by a thread already holding the lock (what happens is undefined and an |
28 // assertion may fail). | 29 // assertion may fail). |
29 bool Try() { return lock_.Try(); } | 30 bool Try() { return lock_.Try(); } |
(...skipping 18 matching lines...) Expand all Loading... | |
48 | 49 |
49 bool Try() { | 50 bool Try() { |
50 bool rv = lock_.Try(); | 51 bool rv = lock_.Try(); |
51 if (rv) { | 52 if (rv) { |
52 CheckUnheldAndMark(); | 53 CheckUnheldAndMark(); |
53 } | 54 } |
54 return rv; | 55 return rv; |
55 } | 56 } |
56 | 57 |
57 void AssertAcquired() const; | 58 void AssertAcquired() const; |
58 #endif // NDEBUG | 59 #endif // NDEBUG && !DCHECK_ALWAYS_ON |
59 | 60 |
60 #if defined(OS_POSIX) | 61 #if defined(OS_POSIX) |
61 // The posix implementation of ConditionVariable needs to be able | 62 // The posix implementation of ConditionVariable needs to be able |
62 // to see our lock and tweak our debugging counters, as it releases | 63 // to see our lock and tweak our debugging counters, as it releases |
63 // and acquires locks inside of pthread_cond_{timed,}wait. | 64 // and acquires locks inside of pthread_cond_{timed,}wait. |
64 friend class ConditionVariable; | 65 friend class ConditionVariable; |
boliu
2014/07/21 18:23:11
See comments on these friend statements about the
| |
65 #elif defined(OS_WIN) | 66 #elif defined(OS_WIN) |
66 // The Windows Vista implementation of ConditionVariable needs the | 67 // The Windows Vista implementation of ConditionVariable needs the |
67 // native handle of the critical section. | 68 // native handle of the critical section. |
68 friend class WinVistaCondVar; | 69 friend class WinVistaCondVar; |
69 #endif | 70 #endif |
70 | 71 |
71 private: | 72 private: |
72 #if !defined(NDEBUG) | 73 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
73 // Members and routines taking care of locks assertions. | 74 // Members and routines taking care of locks assertions. |
74 // Note that this checks for recursive locks and allows them | 75 // Note that this checks for recursive locks and allows them |
75 // if the variable is set. This is allowed by the underlying implementation | 76 // if the variable is set. This is allowed by the underlying implementation |
76 // on windows but not on Posix, so we're doing unneeded checks on Posix. | 77 // on windows but not on Posix, so we're doing unneeded checks on Posix. |
77 // It's worth it to share the code. | 78 // It's worth it to share the code. |
78 void CheckHeldAndUnmark(); | 79 void CheckHeldAndUnmark(); |
79 void CheckUnheldAndMark(); | 80 void CheckUnheldAndMark(); |
80 | 81 |
81 // All private data is implicitly protected by lock_. | 82 // All private data is implicitly protected by lock_. |
82 // Be VERY careful to only access members under that lock. | 83 // Be VERY careful to only access members under that lock. |
83 base::PlatformThreadRef owning_thread_ref_; | 84 base::PlatformThreadRef owning_thread_ref_; |
84 #endif // NDEBUG | 85 #endif // !NDEBUG || DCHECK_ALWAYS_ON |
85 | 86 |
86 // Platform specific underlying lock implementation. | 87 // Platform specific underlying lock implementation. |
87 internal::LockImpl lock_; | 88 internal::LockImpl lock_; |
88 | 89 |
89 DISALLOW_COPY_AND_ASSIGN(Lock); | 90 DISALLOW_COPY_AND_ASSIGN(Lock); |
90 }; | 91 }; |
91 | 92 |
92 // A helper class that acquires the given Lock while the AutoLock is in scope. | 93 // A helper class that acquires the given Lock while the AutoLock is in scope. |
93 class AutoLock { | 94 class AutoLock { |
94 public: | 95 public: |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 } | 128 } |
128 | 129 |
129 private: | 130 private: |
130 Lock& lock_; | 131 Lock& lock_; |
131 DISALLOW_COPY_AND_ASSIGN(AutoUnlock); | 132 DISALLOW_COPY_AND_ASSIGN(AutoUnlock); |
132 }; | 133 }; |
133 | 134 |
134 } // namespace base | 135 } // namespace base |
135 | 136 |
136 #endif // BASE_SYNCHRONIZATION_LOCK_H_ | 137 #endif // BASE_SYNCHRONIZATION_LOCK_H_ |
OLD | NEW |