[email protected] | c9d869c9 | 2009-03-18 23:46:27 | [diff] [blame] | 1 | // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 08048c7 | 2008-08-08 16:19:43 | [diff] [blame] | 5 | #ifndef BASE_LOCK_H_ |
6 | #define BASE_LOCK_H_ | ||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 7 | |
8 | #include "base/lock_impl.h" | ||||
9 | |||||
[email protected] | 81898f7b2 | 2008-10-09 17:33:43 | [diff] [blame] | 10 | // A convenient wrapper for an OS specific critical section. |
[email protected] | 81898f7b2 | 2008-10-09 17:33:43 | [diff] [blame] | 11 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | class Lock { |
13 | public: | ||||
[email protected] | 3cdb6a7 | 2008-10-21 23:37:02 | [diff] [blame] | 14 | Lock() : lock_() {} |
15 | ~Lock() {} | ||||
16 | void Acquire() { lock_.Lock(); } | ||||
17 | void Release() { lock_.Unlock(); } | ||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 18 | // If the lock is not held, take it and return true. If the lock is already |
[email protected] | 81898f7b2 | 2008-10-09 17:33:43 | [diff] [blame] | 19 | // held by another thread, immediately return false. |
[email protected] | 3cdb6a7 | 2008-10-21 23:37:02 | [diff] [blame] | 20 | bool Try() { return lock_.Try(); } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | |
[email protected] | c9d869c9 | 2009-03-18 23:46:27 | [diff] [blame] | 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. | ||||
[email protected] | 1fff4a0 | 2009-03-19 21:03:13 | [diff] [blame] | 26 | void AssertAcquired() const { return lock_.AssertAcquired(); } |
[email protected] | c9d869c9 | 2009-03-18 23:46:27 | [diff] [blame] | 27 | |
[email protected] | 08048c7 | 2008-08-08 16:19:43 | [diff] [blame] | 28 | // Return the underlying lock implementation. |
29 | // TODO(awalker): refactor lock and condition variables so that this is | ||||
30 | // unnecessary. | ||||
31 | LockImpl* lock_impl() { return &lock_; } | ||||
32 | |||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 33 | private: |
[email protected] | 3cdb6a7 | 2008-10-21 23:37:02 | [diff] [blame] | 34 | LockImpl lock_; // Platform specific underlying lock implementation. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 35 | |
[email protected] | 08048c7 | 2008-08-08 16:19:43 | [diff] [blame] | 36 | DISALLOW_COPY_AND_ASSIGN(Lock); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 37 | }; |
38 | |||||
39 | // A helper class that acquires the given Lock while the AutoLock is in scope. | ||||
40 | class AutoLock { | ||||
41 | public: | ||||
[email protected] | b4fef872 | 2008-10-01 13:47:39 | [diff] [blame] | 42 | explicit AutoLock(Lock& lock) : lock_(lock) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 43 | lock_.Acquire(); |
44 | } | ||||
45 | |||||
46 | ~AutoLock() { | ||||
[email protected] | c9d869c9 | 2009-03-18 23:46:27 | [diff] [blame] | 47 | lock_.AssertAcquired(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 48 | lock_.Release(); |
49 | } | ||||
50 | |||||
51 | private: | ||||
52 | Lock& lock_; | ||||
[email protected] | 08048c7 | 2008-08-08 16:19:43 | [diff] [blame] | 53 | DISALLOW_COPY_AND_ASSIGN(AutoLock); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 54 | }; |
55 | |||||
[email protected] | c9d869c9 | 2009-03-18 23:46:27 | [diff] [blame] | 56 | // AutoUnlock is a helper that will Release() the |lock| argument in the |
57 | // constructor, and re-Acquire() it in the destructor. | ||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 58 | class AutoUnlock { |
[email protected] | c9d869c9 | 2009-03-18 23:46:27 | [diff] [blame] | 59 | public: |
60 | explicit AutoUnlock(Lock& lock) : lock_(lock) { | ||||
[email protected] | 81898f7b2 | 2008-10-09 17:33:43 | [diff] [blame] | 61 | // We require our caller to have the lock. |
[email protected] | c9d869c9 | 2009-03-18 23:46:27 | [diff] [blame] | 62 | lock_.AssertAcquired(); |
63 | lock_.Release(); | ||||
[email protected] | 81898f7b2 | 2008-10-09 17:33:43 | [diff] [blame] | 64 | } |
65 | |||||
66 | ~AutoUnlock() { | ||||
[email protected] | c9d869c9 | 2009-03-18 23:46:27 | [diff] [blame] | 67 | lock_.Acquire(); |
[email protected] | 81898f7b2 | 2008-10-09 17:33:43 | [diff] [blame] | 68 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 69 | |
[email protected] | c9d869c9 | 2009-03-18 23:46:27 | [diff] [blame] | 70 | private: |
71 | Lock& lock_; | ||||
72 | DISALLOW_COPY_AND_ASSIGN(AutoUnlock); | ||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 73 | }; |
74 | |||||
[email protected] | 08048c7 | 2008-08-08 16:19:43 | [diff] [blame] | 75 | #endif // BASE_LOCK_H_ |