OLD | NEW |
1 // Copyright (c) 2010 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" | 6 #include "base/logging.h" |
7 | 7 |
8 // NOTE: Although windows critical sections support recursive locks, we do not | 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 | 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). | 10 // acquire the lock a second time (while already holding it). |
11 | 11 |
(...skipping 10 matching lines...) Expand all Loading... |
22 | 22 |
23 LockImpl::~LockImpl() { | 23 LockImpl::~LockImpl() { |
24 ::DeleteCriticalSection(&os_lock_); | 24 ::DeleteCriticalSection(&os_lock_); |
25 } | 25 } |
26 | 26 |
27 bool LockImpl::Try() { | 27 bool LockImpl::Try() { |
28 if (::TryEnterCriticalSection(&os_lock_) != FALSE) { | 28 if (::TryEnterCriticalSection(&os_lock_) != FALSE) { |
29 #ifndef NDEBUG | 29 #ifndef NDEBUG |
30 // ONLY access data after locking. | 30 // ONLY access data after locking. |
31 owning_thread_id_ = PlatformThread::CurrentId(); | 31 owning_thread_id_ = PlatformThread::CurrentId(); |
32 DCHECK_NE(owning_thread_id_, 0u); | 32 DCHECK_NE(owning_thread_id_, 0); |
33 recursion_count_shadow_++; | 33 recursion_count_shadow_++; |
34 if (2 == recursion_count_shadow_ && !recursion_used_) { | 34 if (2 == recursion_count_shadow_ && !recursion_used_) { |
35 recursion_used_ = true; | 35 recursion_used_ = true; |
36 DCHECK(false); // Catch accidental redundant lock acquisition. | 36 DCHECK(false); // Catch accidental redundant lock acquisition. |
37 } | 37 } |
38 #endif | 38 #endif |
39 return true; | 39 return true; |
40 } | 40 } |
41 return false; | 41 return false; |
42 } | 42 } |
43 | 43 |
44 void LockImpl::Lock() { | 44 void LockImpl::Lock() { |
45 ::EnterCriticalSection(&os_lock_); | 45 ::EnterCriticalSection(&os_lock_); |
46 #ifndef NDEBUG | 46 #ifndef NDEBUG |
47 // ONLY access data after locking. | 47 // ONLY access data after locking. |
48 owning_thread_id_ = PlatformThread::CurrentId(); | 48 owning_thread_id_ = PlatformThread::CurrentId(); |
49 DCHECK_NE(owning_thread_id_, 0u); | 49 DCHECK_NE(owning_thread_id_, 0); |
50 recursion_count_shadow_++; | 50 recursion_count_shadow_++; |
51 if (2 == recursion_count_shadow_ && !recursion_used_) { | 51 if (2 == recursion_count_shadow_ && !recursion_used_) { |
52 recursion_used_ = true; | 52 recursion_used_ = true; |
53 DCHECK(false); // Catch accidental redundant lock acquisition. | 53 DCHECK(false); // Catch accidental redundant lock acquisition. |
54 } | 54 } |
55 #endif // NDEBUG | 55 #endif // NDEBUG |
56 } | 56 } |
57 | 57 |
58 void LockImpl::Unlock() { | 58 void LockImpl::Unlock() { |
59 #ifndef NDEBUG | 59 #ifndef NDEBUG |
60 --recursion_count_shadow_; // ONLY access while lock is still held. | 60 --recursion_count_shadow_; // ONLY access while lock is still held. |
61 DCHECK(0 <= recursion_count_shadow_); | 61 DCHECK(0 <= recursion_count_shadow_); |
62 owning_thread_id_ = 0; | 62 owning_thread_id_ = 0; |
63 #endif // NDEBUG | 63 #endif // NDEBUG |
64 ::LeaveCriticalSection(&os_lock_); | 64 ::LeaveCriticalSection(&os_lock_); |
65 } | 65 } |
66 | 66 |
67 // In non-debug builds, this method is declared as an empty inline method. | 67 // In non-debug builds, this method is declared as an empty inline method. |
68 #ifndef NDEBUG | 68 #ifndef NDEBUG |
69 void LockImpl::AssertAcquired() const { | 69 void LockImpl::AssertAcquired() const { |
70 DCHECK(recursion_count_shadow_ > 0); | 70 DCHECK(recursion_count_shadow_ > 0); |
71 DCHECK_EQ(owning_thread_id_, PlatformThread::CurrentId()); | 71 DCHECK_EQ(owning_thread_id_, PlatformThread::CurrentId()); |
72 } | 72 } |
73 #endif | 73 #endif |
OLD | NEW |