michael@0: // Copyright (c) 2011 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_SYNCHRONIZATION_LOCK_H_ michael@0: #define BASE_SYNCHRONIZATION_LOCK_H_ michael@0: michael@0: #include "base/base_export.h" michael@0: #include "base/synchronization/lock_impl.h" michael@0: #include "base/threading/platform_thread.h" michael@0: michael@0: namespace base { michael@0: michael@0: // A convenient wrapper for an OS specific critical section. The only real michael@0: // intelligence in this class is in debug mode for the support for the michael@0: // AssertAcquired() method. michael@0: class BASE_EXPORT Lock { michael@0: public: michael@0: #if defined(NDEBUG) // Optimized wrapper implementation michael@0: Lock() : lock_() {} michael@0: ~Lock() {} michael@0: void Acquire() { lock_.Lock(); } michael@0: void Release() { lock_.Unlock(); } michael@0: michael@0: // If the lock is not held, take it and return true. If the lock is already michael@0: // held by another thread, immediately return false. This must not be called michael@0: // by a thread already holding the lock (what happens is undefined and an michael@0: // assertion may fail). michael@0: bool Try() { return lock_.Try(); } michael@0: michael@0: // Null implementation if not debug. michael@0: void AssertAcquired() const {} michael@0: #else michael@0: Lock(); michael@0: ~Lock(); michael@0: michael@0: // NOTE: Although windows critical sections support recursive locks, we do not michael@0: // allow this, and we will commonly fire a DCHECK() if a thread attempts to michael@0: // acquire the lock a second time (while already holding it). michael@0: void Acquire() { michael@0: lock_.Lock(); michael@0: CheckUnheldAndMark(); michael@0: } michael@0: void Release() { michael@0: CheckHeldAndUnmark(); michael@0: lock_.Unlock(); michael@0: } michael@0: michael@0: bool Try() { michael@0: bool rv = lock_.Try(); michael@0: if (rv) { michael@0: CheckUnheldAndMark(); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: void AssertAcquired() const; michael@0: #endif // NDEBUG michael@0: michael@0: #if defined(OS_POSIX) michael@0: // The posix implementation of ConditionVariable needs to be able michael@0: // to see our lock and tweak our debugging counters, as it releases michael@0: // and acquires locks inside of pthread_cond_{timed,}wait. michael@0: friend class ConditionVariable; michael@0: #elif defined(OS_WIN) michael@0: // The Windows Vista implementation of ConditionVariable needs the michael@0: // native handle of the critical section. michael@0: friend class WinVistaCondVar; michael@0: #endif michael@0: michael@0: private: michael@0: #if !defined(NDEBUG) michael@0: // Members and routines taking care of locks assertions. michael@0: // Note that this checks for recursive locks and allows them michael@0: // if the variable is set. This is allowed by the underlying implementation michael@0: // on windows but not on Posix, so we're doing unneeded checks on Posix. michael@0: // It's worth it to share the code. michael@0: void CheckHeldAndUnmark(); michael@0: void CheckUnheldAndMark(); michael@0: michael@0: // All private data is implicitly protected by lock_. michael@0: // Be VERY careful to only access members under that lock. michael@0: michael@0: // Determines validity of owning_thread_id_. Needed as we don't have michael@0: // a null owning_thread_id_ value. michael@0: bool owned_by_thread_; michael@0: base::PlatformThreadId owning_thread_id_; michael@0: #endif // NDEBUG michael@0: michael@0: // Platform specific underlying lock implementation. michael@0: internal::LockImpl lock_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(Lock); michael@0: }; michael@0: michael@0: // A helper class that acquires the given Lock while the AutoLock is in scope. michael@0: class AutoLock { michael@0: public: michael@0: struct AlreadyAcquired {}; michael@0: michael@0: explicit AutoLock(Lock& lock) : lock_(lock) { michael@0: lock_.Acquire(); michael@0: } michael@0: michael@0: AutoLock(Lock& lock, const AlreadyAcquired&) : lock_(lock) { michael@0: lock_.AssertAcquired(); michael@0: } michael@0: michael@0: ~AutoLock() { michael@0: lock_.AssertAcquired(); michael@0: lock_.Release(); michael@0: } michael@0: michael@0: private: michael@0: Lock& lock_; michael@0: DISALLOW_COPY_AND_ASSIGN(AutoLock); michael@0: }; michael@0: michael@0: // AutoUnlock is a helper that will Release() the |lock| argument in the michael@0: // constructor, and re-Acquire() it in the destructor. michael@0: class AutoUnlock { michael@0: public: michael@0: explicit AutoUnlock(Lock& lock) : lock_(lock) { michael@0: // We require our caller to have the lock. michael@0: lock_.AssertAcquired(); michael@0: lock_.Release(); michael@0: } michael@0: michael@0: ~AutoUnlock() { michael@0: lock_.Acquire(); michael@0: } michael@0: michael@0: private: michael@0: Lock& lock_; michael@0: DISALLOW_COPY_AND_ASSIGN(AutoUnlock); michael@0: }; michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_SYNCHRONIZATION_LOCK_H_