1// Copyright (c) 2011 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. 4 5#ifndef BASE_SYNCHRONIZATION_LOCK_IMPL_H_ 6#define BASE_SYNCHRONIZATION_LOCK_IMPL_H_ 7#pragma once 8 9#include "build/build_config.h" 10 11#if defined(OS_WIN) 12#include <windows.h> 13#elif defined(OS_POSIX) 14#include <pthread.h> 15#endif 16 17#include "base/base_api.h" 18#include "base/basictypes.h" 19 20namespace base { 21namespace internal { 22 23// This class implements the underlying platform-specific spin-lock mechanism 24// used for the Lock class. Most users should not use LockImpl directly, but 25// should instead use Lock. 26class BASE_API LockImpl { 27 public: 28#if defined(OS_WIN) 29 typedef CRITICAL_SECTION OSLockType; 30#elif defined(OS_POSIX) 31 typedef pthread_mutex_t OSLockType; 32#endif 33 34 LockImpl(); 35 ~LockImpl(); 36 37 // If the lock is not held, take it and return true. If the lock is already 38 // held by something else, immediately return false. 39 bool Try(); 40 41 // Take the lock, blocking until it is available if necessary. 42 void Lock(); 43 44 // Release the lock. This must only be called by the lock's holder: after 45 // a successful call to Try, or a call to Lock. 46 void Unlock(); 47 48 // Return the native underlying lock. Not supported for Windows builds. 49 // TODO(awalker): refactor lock and condition variables so that this is 50 // unnecessary. 51#if !defined(OS_WIN) 52 OSLockType* os_lock() { return &os_lock_; } 53#endif 54 55 private: 56 OSLockType os_lock_; 57 58 DISALLOW_COPY_AND_ASSIGN(LockImpl); 59}; 60 61} // namespace internal 62} // namespace base 63 64#endif // BASE_SYNCHRONIZATION_LOCK_IMPL_H_ 65