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

Side by Side Diff: base/lock.h

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 | « no previous file | base/lock_impl.h » ('j') | 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-2009 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_LOCK_H_ 5 #ifndef BASE_LOCK_H_
6 #define BASE_LOCK_H_ 6 #define BASE_LOCK_H_
7 7
8 #include "base/lock_impl.h" 8 #include "base/lock_impl.h"
9 9
10 // A convenient wrapper for an OS specific critical section. 10 // A convenient wrapper for an OS specific critical section.
11 11
12 class Lock { 12 class Lock {
13 public: 13 public:
14 Lock() : lock_() {} 14 Lock() : lock_() {}
15 ~Lock() {} 15 ~Lock() {}
16 void Acquire() { lock_.Lock(); } 16 void Acquire() { lock_.Lock(); }
17 void Release() { lock_.Unlock(); } 17 void Release() { lock_.Unlock(); }
18 // If the lock is not held, take it and return true. If the lock is already 18 // If the lock is not held, take it and return true. If the lock is already
19 // held by another thread, immediately return false. 19 // held by another thread, immediately return false.
20 bool Try() { return lock_.Try(); } 20 bool Try() { return lock_.Try(); }
21 21
22 // In debug builds this method checks that the lock has been acquired by the
23 // calling thread. If the lock has not been acquired, then the method
24 // will DCHECK(). In non-debug builds, the LockImpl's implementation of
25 // AssertAcquired() is an empty inline method.
26 void AssertAcquired() { return lock_.AssertAcquired(); }
27
22 // Return the underlying lock implementation. 28 // Return the underlying lock implementation.
23 // TODO(awalker): refactor lock and condition variables so that this is 29 // TODO(awalker): refactor lock and condition variables so that this is
24 // unnecessary. 30 // unnecessary.
25 LockImpl* lock_impl() { return &lock_; } 31 LockImpl* lock_impl() { return &lock_; }
26 32
27 private: 33 private:
28 LockImpl lock_; // Platform specific underlying lock implementation. 34 LockImpl lock_; // Platform specific underlying lock implementation.
29 35
30 DISALLOW_COPY_AND_ASSIGN(Lock); 36 DISALLOW_COPY_AND_ASSIGN(Lock);
31 }; 37 };
32 38
33 // A helper class that acquires the given Lock while the AutoLock is in scope. 39 // A helper class that acquires the given Lock while the AutoLock is in scope.
34 class AutoLock { 40 class AutoLock {
35 public: 41 public:
36 explicit AutoLock(Lock& lock) : lock_(lock) { 42 explicit AutoLock(Lock& lock) : lock_(lock) {
37 lock_.Acquire(); 43 lock_.Acquire();
38 } 44 }
39 45
40 ~AutoLock() { 46 ~AutoLock() {
47 lock_.AssertAcquired();
41 lock_.Release(); 48 lock_.Release();
42 } 49 }
43 50
44 private: 51 private:
45 Lock& lock_; 52 Lock& lock_;
46 DISALLOW_COPY_AND_ASSIGN(AutoLock); 53 DISALLOW_COPY_AND_ASSIGN(AutoLock);
47 }; 54 };
48 55
49 // AutoUnlock is a helper class for ConditionVariable that will Release() the 56 // AutoUnlock is a helper that will Release() the |lock| argument in the
50 // lock argument in the constructor, and re-Acquire() it in the destructor. 57 // constructor, and re-Acquire() it in the destructor.
51 class ConditionVariable;
52 class AutoUnlock { 58 class AutoUnlock {
53 private: // Everything is private, so only our friend can use us. 59 public:
54 friend class ConditionVariable; // The only user of this class. 60 explicit AutoUnlock(Lock& lock) : lock_(lock) {
55
56 explicit AutoUnlock(Lock& lock) : lock_(&lock) {
57 // We require our caller to have the lock. 61 // We require our caller to have the lock.
58 lock_->Release(); 62 lock_.AssertAcquired();
63 lock_.Release();
59 } 64 }
60 65
61 ~AutoUnlock() { 66 ~AutoUnlock() {
62 lock_->Acquire(); 67 lock_.Acquire();
63 } 68 }
64 69
65 Lock* lock_; 70 private:
71 Lock& lock_;
72 DISALLOW_COPY_AND_ASSIGN(AutoUnlock);
66 }; 73 };
67 74
68 #endif // BASE_LOCK_H_ 75 #endif // BASE_LOCK_H_
OLDNEW
« no previous file with comments | « no previous file | base/lock_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698