summaryrefslogtreecommitdiffstats
path: root/base/lock.h
diff options
context:
space:
mode:
authorralphl@chromium.org <ralphl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-18 23:46:27 +0000
committerralphl@chromium.org <ralphl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-18 23:46:27 +0000
commitc9d869c9d91ccb7ad8f061a93e7265bcddd85b47 (patch)
treef3731e31f3af7b8226fe76361c62d68d1c38478b /base/lock.h
parent5b65ba7172c2c29c0cfb2b66bb08178ce9652d4f (diff)
downloadchromium_src-c9d869c9d91ccb7ad8f061a93e7265bcddd85b47.zip
chromium_src-c9d869c9d91ccb7ad8f061a93e7265bcddd85b47.tar.gz
chromium_src-c9d869c9d91ccb7ad8f061a93e7265bcddd85b47.tar.bz2
Added a debug method AssertAcquired() that compiles to nothing in non-debug builds. In debug builds, the method will DCHECK if the
current thread does not hold the lock. I also removed a class that was not being used (AutoLockImpl) which seems like someone just copied the Lock.h file and blindly added an Impl for the AutoLock class. Thre are places in my code where I want to use the AutoUnlock class. I can't see why it was restricted to use by the condition variable class only, so I just made it into a regular class with no restrictions. I noticed that JamesR posted a CL about a month ago that made AutoUnlock a public class, but it was never committed, so I added him to this CL for review too. Review URL: http://codereview.chromium.org/48109 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@12037 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/lock.h')
-rw-r--r--base/lock.h29
1 files changed, 18 insertions, 11 deletions
diff --git a/base/lock.h b/base/lock.h
index 8a4441a..f3653cf 100644
--- a/base/lock.h
+++ b/base/lock.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -19,6 +19,12 @@ class Lock {
// held by another thread, immediately return false.
bool Try() { return lock_.Try(); }
+ // In debug builds this method checks that the lock has been acquired by the
+ // calling thread. If the lock has not been acquired, then the method
+ // will DCHECK(). In non-debug builds, the LockImpl's implementation of
+ // AssertAcquired() is an empty inline method.
+ void AssertAcquired() { return lock_.AssertAcquired(); }
+
// Return the underlying lock implementation.
// TODO(awalker): refactor lock and condition variables so that this is
// unnecessary.
@@ -38,6 +44,7 @@ class AutoLock {
}
~AutoLock() {
+ lock_.AssertAcquired();
lock_.Release();
}
@@ -46,23 +53,23 @@ class AutoLock {
DISALLOW_COPY_AND_ASSIGN(AutoLock);
};
-// AutoUnlock is a helper class for ConditionVariable that will Release() the
-// lock argument in the constructor, and re-Acquire() it in the destructor.
-class ConditionVariable;
+// AutoUnlock is a helper that will Release() the |lock| argument in the
+// constructor, and re-Acquire() it in the destructor.
class AutoUnlock {
- private: // Everything is private, so only our friend can use us.
- friend class ConditionVariable; // The only user of this class.
-
- explicit AutoUnlock(Lock& lock) : lock_(&lock) {
+ public:
+ explicit AutoUnlock(Lock& lock) : lock_(lock) {
// We require our caller to have the lock.
- lock_->Release();
+ lock_.AssertAcquired();
+ lock_.Release();
}
~AutoUnlock() {
- lock_->Acquire();
+ lock_.Acquire();
}
- Lock* lock_;
+ private:
+ Lock& lock_;
+ DISALLOW_COPY_AND_ASSIGN(AutoUnlock);
};
#endif // BASE_LOCK_H_