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" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 | 89 |
90 // Platform specific underlying lock implementation. | 90 // Platform specific underlying lock implementation. |
91 internal::LockImpl lock_; | 91 internal::LockImpl lock_; |
92 | 92 |
93 DISALLOW_COPY_AND_ASSIGN(Lock); | 93 DISALLOW_COPY_AND_ASSIGN(Lock); |
94 }; | 94 }; |
95 | 95 |
96 // A helper class that acquires the given Lock while the AutoLock is in scope. | 96 // A helper class that acquires the given Lock while the AutoLock is in scope. |
97 class AutoLock { | 97 class AutoLock { |
98 public: | 98 public: |
| 99 struct AlreadyAcquired {}; |
| 100 |
99 explicit AutoLock(Lock& lock) : lock_(lock) { | 101 explicit AutoLock(Lock& lock) : lock_(lock) { |
100 lock_.Acquire(); | 102 lock_.Acquire(); |
101 } | 103 } |
102 | 104 |
| 105 AutoLock(Lock& lock, const AlreadyAcquired&) : lock_(lock) { |
| 106 lock_.AssertAcquired(); |
| 107 } |
| 108 |
103 ~AutoLock() { | 109 ~AutoLock() { |
104 lock_.AssertAcquired(); | 110 lock_.AssertAcquired(); |
105 lock_.Release(); | 111 lock_.Release(); |
106 } | 112 } |
107 | 113 |
108 private: | 114 private: |
109 Lock& lock_; | 115 Lock& lock_; |
110 DISALLOW_COPY_AND_ASSIGN(AutoLock); | 116 DISALLOW_COPY_AND_ASSIGN(AutoLock); |
111 }; | 117 }; |
112 | 118 |
(...skipping 12 matching lines...) Expand all Loading... |
125 } | 131 } |
126 | 132 |
127 private: | 133 private: |
128 Lock& lock_; | 134 Lock& lock_; |
129 DISALLOW_COPY_AND_ASSIGN(AutoUnlock); | 135 DISALLOW_COPY_AND_ASSIGN(AutoUnlock); |
130 }; | 136 }; |
131 | 137 |
132 } // namespace base | 138 } // namespace base |
133 | 139 |
134 #endif // BASE_SYNCHRONIZATION_LOCK_H_ | 140 #endif // BASE_SYNCHRONIZATION_LOCK_H_ |
OLD | NEW |