michael@0: // Copyright (c) 2006-2008 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_LOCK_IMPL_H_ michael@0: #define BASE_LOCK_IMPL_H_ michael@0: michael@0: #include "build/build_config.h" michael@0: michael@0: #if defined(OS_WIN) michael@0: #include michael@0: #elif defined(OS_POSIX) michael@0: #include michael@0: #endif michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "base/platform_thread.h" michael@0: michael@0: // This class implements the underlying platform-specific spin-lock mechanism michael@0: // used for the Lock class. Most users should not use LockImpl directly, but michael@0: // should instead use Lock. michael@0: class LockImpl { michael@0: public: michael@0: #if defined(OS_WIN) michael@0: typedef CRITICAL_SECTION OSLockType; michael@0: #elif defined(OS_POSIX) michael@0: typedef pthread_mutex_t OSLockType; michael@0: #endif michael@0: michael@0: LockImpl(); michael@0: ~LockImpl(); michael@0: michael@0: // If the lock is not held, take it and return true. If the lock is already michael@0: // held by something else, immediately return false. michael@0: bool Try(); michael@0: michael@0: // Take the lock, blocking until it is available if necessary. michael@0: void Lock(); michael@0: michael@0: // Release the lock. This must only be called by the lock's holder: after michael@0: // a successful call to Try, or a call to Lock. michael@0: void Unlock(); michael@0: michael@0: // Debug-only method that will DCHECK() if the lock is not acquired by the michael@0: // current thread. In non-debug builds, no check is performed. michael@0: // Because linux and mac condition variables modify the underlyning lock michael@0: // through the os_lock() method, runtime assertions can not be done on those michael@0: // builds. michael@0: #if defined(NDEBUG) || !defined(OS_WIN) michael@0: void AssertAcquired() const {} michael@0: #else michael@0: void AssertAcquired() const; michael@0: #endif michael@0: michael@0: // Return the native underlying lock. Not supported for Windows builds. michael@0: // TODO(awalker): refactor lock and condition variables so that this is michael@0: // unnecessary. michael@0: #if !defined(OS_WIN) michael@0: OSLockType* os_lock() { return &os_lock_; } michael@0: #endif michael@0: michael@0: private: michael@0: OSLockType os_lock_; michael@0: michael@0: #if !defined(NDEBUG) && defined(OS_WIN) michael@0: // All private data is implicitly protected by lock_. michael@0: // Be VERY careful to only access members under that lock. michael@0: PlatformThreadId owning_thread_id_; michael@0: int32_t recursion_count_shadow_; michael@0: bool recursion_used_; // Allow debugging to continued after a DCHECK(). michael@0: #endif // NDEBUG michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(LockImpl); michael@0: }; michael@0: michael@0: michael@0: #endif // BASE_LOCK_IMPL_H_