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_IMPL_H_ michael@0: #define BASE_SYNCHRONIZATION_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/base_export.h" michael@0: #include "base/basictypes.h" michael@0: michael@0: namespace base { michael@0: namespace internal { 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 BASE_EXPORT LockImpl { michael@0: public: michael@0: #if defined(OS_WIN) michael@0: typedef CRITICAL_SECTION NativeHandle; michael@0: #elif defined(OS_POSIX) michael@0: typedef pthread_mutex_t NativeHandle; 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: // Return the native underlying lock. michael@0: // TODO(awalker): refactor lock and condition variables so that this is michael@0: // unnecessary. michael@0: NativeHandle* native_handle() { return &native_handle_; } michael@0: michael@0: private: michael@0: NativeHandle native_handle_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(LockImpl); michael@0: }; michael@0: michael@0: } // namespace internal michael@0: } // namespace base michael@0: michael@0: #endif // BASE_SYNCHRONIZATION_LOCK_IMPL_H_