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 #include "base/lock_impl.h" | 5 #include "base/lock_impl.h" |
| 6 #include "base/logging.h" |
| 7 |
| 8 // NOTE: Although windows critical sections support recursive locks, we do not |
| 9 // allow this, and we will commonly fire a DCHECK() if a thread attempts to |
| 10 // acquire the lock a second time (while already holding it). |
6 | 11 |
7 LockImpl::LockImpl() { | 12 LockImpl::LockImpl() { |
| 13 #ifndef NDEBUG |
| 14 recursion_count_shadow_ = 0; |
| 15 recursion_used_ = false; |
| 16 #endif // NDEBUG |
8 // The second parameter is the spin count, for short-held locks it avoid the | 17 // The second parameter is the spin count, for short-held locks it avoid the |
9 // contending thread from going to sleep which helps performance greatly. | 18 // contending thread from going to sleep which helps performance greatly. |
10 ::InitializeCriticalSectionAndSpinCount(&os_lock_, 2000); | 19 ::InitializeCriticalSectionAndSpinCount(&os_lock_, 2000); |
11 } | 20 } |
12 | 21 |
13 LockImpl::~LockImpl() { | 22 LockImpl::~LockImpl() { |
14 ::DeleteCriticalSection(&os_lock_); | 23 ::DeleteCriticalSection(&os_lock_); |
15 } | 24 } |
16 | 25 |
17 bool LockImpl::Try() { | 26 bool LockImpl::Try() { |
18 return ::TryEnterCriticalSection(&os_lock_) != FALSE; | 27 if (::TryEnterCriticalSection(&os_lock_) != FALSE) { |
| 28 #ifndef NDEBUG |
| 29 recursion_count_shadow_++; |
| 30 if (2 == recursion_count_shadow_ && !recursion_used_) { |
| 31 recursion_used_ = true; |
| 32 DCHECK(false); // Catch accidental redundant lock acquisition. |
| 33 } |
| 34 #endif |
| 35 return true; |
| 36 } |
| 37 return false; |
19 } | 38 } |
20 | 39 |
21 void LockImpl::Lock() { | 40 void LockImpl::Lock() { |
22 ::EnterCriticalSection(&os_lock_); | 41 ::EnterCriticalSection(&os_lock_); |
| 42 #ifndef NDEBUG |
| 43 // ONLY access data after locking. |
| 44 recursion_count_shadow_++; |
| 45 if (2 == recursion_count_shadow_ && !recursion_used_) { |
| 46 recursion_used_ = true; |
| 47 DCHECK(false); // Catch accidental redundant lock acquisition. |
| 48 } |
| 49 #endif // NDEBUG |
23 } | 50 } |
24 | 51 |
25 void LockImpl::Unlock() { | 52 void LockImpl::Unlock() { |
| 53 #ifndef NDEBUG |
| 54 --recursion_count_shadow_; // ONLY access while lock is still held. |
| 55 DCHECK(0 <= recursion_count_shadow_); |
| 56 #endif // NDEBUG |
26 ::LeaveCriticalSection(&os_lock_); | 57 ::LeaveCriticalSection(&os_lock_); |
27 } | 58 } |
28 | |
OLD | NEW |