Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Side by Side Diff: base/lock_impl_win.cc

Issue 48109: Added a debug method AssertAcquired() that compiles to nothing in non-debug b... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/lock_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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" 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
12 LockImpl::LockImpl() { 12 LockImpl::LockImpl() {
13 #ifndef NDEBUG 13 #ifndef NDEBUG
14 recursion_count_shadow_ = 0; 14 recursion_count_shadow_ = 0;
15 recursion_used_ = false; 15 recursion_used_ = false;
16 owning_thread_id_ = 0;
16 #endif // NDEBUG 17 #endif // NDEBUG
17 // The second parameter is the spin count, for short-held locks it avoid the 18 // The second parameter is the spin count, for short-held locks it avoid the
18 // contending thread from going to sleep which helps performance greatly. 19 // contending thread from going to sleep which helps performance greatly.
19 ::InitializeCriticalSectionAndSpinCount(&os_lock_, 2000); 20 ::InitializeCriticalSectionAndSpinCount(&os_lock_, 2000);
20 } 21 }
21 22
22 LockImpl::~LockImpl() { 23 LockImpl::~LockImpl() {
23 ::DeleteCriticalSection(&os_lock_); 24 ::DeleteCriticalSection(&os_lock_);
24 } 25 }
25 26
26 bool LockImpl::Try() { 27 bool LockImpl::Try() {
27 if (::TryEnterCriticalSection(&os_lock_) != FALSE) { 28 if (::TryEnterCriticalSection(&os_lock_) != FALSE) {
28 #ifndef NDEBUG 29 #ifndef NDEBUG
30 // ONLY access data after locking.
31 owning_thread_id_ = PlatformThread::CurrentId();
32 DCHECK_NE(owning_thread_id_, 0);
29 recursion_count_shadow_++; 33 recursion_count_shadow_++;
30 if (2 == recursion_count_shadow_ && !recursion_used_) { 34 if (2 == recursion_count_shadow_ && !recursion_used_) {
31 recursion_used_ = true; 35 recursion_used_ = true;
32 DCHECK(false); // Catch accidental redundant lock acquisition. 36 DCHECK(false); // Catch accidental redundant lock acquisition.
33 } 37 }
34 #endif 38 #endif
35 return true; 39 return true;
36 } 40 }
37 return false; 41 return false;
38 } 42 }
39 43
40 void LockImpl::Lock() { 44 void LockImpl::Lock() {
41 ::EnterCriticalSection(&os_lock_); 45 ::EnterCriticalSection(&os_lock_);
42 #ifndef NDEBUG 46 #ifndef NDEBUG
43 // ONLY access data after locking. 47 // ONLY access data after locking.
48 owning_thread_id_ = PlatformThread::CurrentId();
49 DCHECK_NE(owning_thread_id_, 0);
44 recursion_count_shadow_++; 50 recursion_count_shadow_++;
45 if (2 == recursion_count_shadow_ && !recursion_used_) { 51 if (2 == recursion_count_shadow_ && !recursion_used_) {
46 recursion_used_ = true; 52 recursion_used_ = true;
47 DCHECK(false); // Catch accidental redundant lock acquisition. 53 DCHECK(false); // Catch accidental redundant lock acquisition.
48 } 54 }
49 #endif // NDEBUG 55 #endif // NDEBUG
50 } 56 }
51 57
52 void LockImpl::Unlock() { 58 void LockImpl::Unlock() {
53 #ifndef NDEBUG 59 #ifndef NDEBUG
54 --recursion_count_shadow_; // ONLY access while lock is still held. 60 --recursion_count_shadow_; // ONLY access while lock is still held.
55 DCHECK(0 <= recursion_count_shadow_); 61 DCHECK(0 <= recursion_count_shadow_);
62 owning_thread_id_ = 0;
56 #endif // NDEBUG 63 #endif // NDEBUG
57 ::LeaveCriticalSection(&os_lock_); 64 ::LeaveCriticalSection(&os_lock_);
58 } 65 }
66
67 // In non-debug builds, this method is declared as an empty inline method.
68 #ifndef NDEBUG
69 void LockImpl::AssertAcquired() {
70 DCHECK(recursion_count_shadow_ > 0);
71 DCHECK_EQ(owning_thread_id_, PlatformThread::CurrentId());
72 }
73 #endif
OLDNEW
« no previous file with comments | « base/lock_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698