license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 5 | #ifndef BASE_LOCK_H_ |
| 6 | #define BASE_LOCK_H_ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 7 | |
| 8 | #include "base/lock_impl.h" |
| 9 | |
jar@google.com | 8372da2 | 2008-10-10 02:33:43 +0900 | [diff] [blame] | 10 | // A convenient wrapper for an OS specific critical section. |
jar@google.com | 8372da2 | 2008-10-10 02:33:43 +0900 | [diff] [blame] | 11 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 12 | class Lock { |
| 13 | public: |
jar@google.com | 87cf509 | 2008-10-22 08:37:02 +0900 | [diff] [blame] | 14 | Lock() : lock_() {} |
| 15 | ~Lock() {} |
| 16 | void Acquire() { lock_.Lock(); } |
| 17 | void Release() { lock_.Unlock(); } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 18 | // If the lock is not held, take it and return true. If the lock is already |
jar@google.com | 8372da2 | 2008-10-10 02:33:43 +0900 | [diff] [blame] | 19 | // held by another thread, immediately return false. |
jar@google.com | 87cf509 | 2008-10-22 08:37:02 +0900 | [diff] [blame] | 20 | bool Try() { return lock_.Try(); } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 21 | |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 22 | // Return the underlying lock implementation. |
| 23 | // TODO(awalker): refactor lock and condition variables so that this is |
| 24 | // unnecessary. |
| 25 | LockImpl* lock_impl() { return &lock_; } |
| 26 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 27 | private: |
jar@google.com | 87cf509 | 2008-10-22 08:37:02 +0900 | [diff] [blame] | 28 | LockImpl lock_; // Platform specific underlying lock implementation. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 29 | |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 30 | DISALLOW_COPY_AND_ASSIGN(Lock); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | // A helper class that acquires the given Lock while the AutoLock is in scope. |
| 34 | class AutoLock { |
| 35 | public: |
deanm@chromium.org | 91f3bd3 | 2008-10-01 22:47:39 +0900 | [diff] [blame] | 36 | explicit AutoLock(Lock& lock) : lock_(lock) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 37 | lock_.Acquire(); |
| 38 | } |
| 39 | |
| 40 | ~AutoLock() { |
| 41 | lock_.Release(); |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | Lock& lock_; |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 46 | DISALLOW_COPY_AND_ASSIGN(AutoLock); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 47 | }; |
| 48 | |
jar@google.com | 8372da2 | 2008-10-10 02:33:43 +0900 | [diff] [blame] | 49 | // AutoUnlock is a helper class for ConditionVariable that will Release() the |
| 50 | // lock argument in the constructor, and re-Acquire() it in the destructor. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 51 | class ConditionVariable; |
| 52 | class AutoUnlock { |
| 53 | private: // Everything is private, so only our friend can use us. |
| 54 | friend class ConditionVariable; // The only user of this class. |
jar@google.com | 8372da2 | 2008-10-10 02:33:43 +0900 | [diff] [blame] | 55 | |
| 56 | explicit AutoUnlock(Lock& lock) : lock_(&lock) { |
| 57 | // We require our caller to have the lock. |
| 58 | lock_->Release(); |
| 59 | } |
| 60 | |
| 61 | ~AutoUnlock() { |
| 62 | lock_->Acquire(); |
| 63 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 64 | |
| 65 | Lock* lock_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 66 | }; |
| 67 | |
mmentovai@google.com | f5a4000 | 2008-08-09 01:19:43 +0900 | [diff] [blame] | 68 | #endif // BASE_LOCK_H_ |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 69 | |