Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_LOCK_H_ |
michael@0 | 6 | #define BASE_LOCK_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include "base/lock_impl.h" |
michael@0 | 9 | |
michael@0 | 10 | // A convenient wrapper for an OS specific critical section. |
michael@0 | 11 | |
michael@0 | 12 | class Lock { |
michael@0 | 13 | public: |
michael@0 | 14 | Lock() : lock_() {} |
michael@0 | 15 | ~Lock() {} |
michael@0 | 16 | void Acquire() { lock_.Lock(); } |
michael@0 | 17 | void Release() { lock_.Unlock(); } |
michael@0 | 18 | // If the lock is not held, take it and return true. If the lock is already |
michael@0 | 19 | // held by another thread, immediately return false. |
michael@0 | 20 | bool Try() { return lock_.Try(); } |
michael@0 | 21 | |
michael@0 | 22 | // In debug builds this method checks that the lock has been acquired by the |
michael@0 | 23 | // calling thread. If the lock has not been acquired, then the method |
michael@0 | 24 | // will DCHECK(). In non-debug builds, the LockImpl's implementation of |
michael@0 | 25 | // AssertAcquired() is an empty inline method. |
michael@0 | 26 | void AssertAcquired() const { return lock_.AssertAcquired(); } |
michael@0 | 27 | |
michael@0 | 28 | // Return the underlying lock implementation. |
michael@0 | 29 | // TODO(awalker): refactor lock and condition variables so that this is |
michael@0 | 30 | // unnecessary. |
michael@0 | 31 | LockImpl* lock_impl() { return &lock_; } |
michael@0 | 32 | |
michael@0 | 33 | private: |
michael@0 | 34 | LockImpl lock_; // Platform specific underlying lock implementation. |
michael@0 | 35 | |
michael@0 | 36 | DISALLOW_COPY_AND_ASSIGN(Lock); |
michael@0 | 37 | }; |
michael@0 | 38 | |
michael@0 | 39 | // A helper class that acquires the given Lock while the AutoLock is in scope. |
michael@0 | 40 | class AutoLock { |
michael@0 | 41 | public: |
michael@0 | 42 | explicit AutoLock(Lock& lock) : lock_(lock) { |
michael@0 | 43 | lock_.Acquire(); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | ~AutoLock() { |
michael@0 | 47 | lock_.AssertAcquired(); |
michael@0 | 48 | lock_.Release(); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | private: |
michael@0 | 52 | Lock& lock_; |
michael@0 | 53 | DISALLOW_COPY_AND_ASSIGN(AutoLock); |
michael@0 | 54 | }; |
michael@0 | 55 | |
michael@0 | 56 | // AutoUnlock is a helper that will Release() the |lock| argument in the |
michael@0 | 57 | // constructor, and re-Acquire() it in the destructor. |
michael@0 | 58 | class AutoUnlock { |
michael@0 | 59 | public: |
michael@0 | 60 | explicit AutoUnlock(Lock& lock) : lock_(lock) { |
michael@0 | 61 | // We require our caller to have the lock. |
michael@0 | 62 | lock_.AssertAcquired(); |
michael@0 | 63 | lock_.Release(); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | ~AutoUnlock() { |
michael@0 | 67 | lock_.Acquire(); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | private: |
michael@0 | 71 | Lock& lock_; |
michael@0 | 72 | DISALLOW_COPY_AND_ASSIGN(AutoUnlock); |
michael@0 | 73 | }; |
michael@0 | 74 | |
michael@0 | 75 | #endif // BASE_LOCK_H_ |