15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_SYNCHRONIZATION_LOCK_IMPL_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_SYNCHRONIZATION_LOCK_IMPL_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "build/build_config.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN) 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <windows.h> 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(OS_POSIX) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <pthread.h> 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_export.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base { 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace internal { 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class implements the underlying platform-specific spin-lock mechanism 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// used for the Lock class. Most users should not use LockImpl directly, but 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// should instead use Lock. 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT LockImpl { 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN) 28d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) typedef CRITICAL_SECTION NativeHandle; 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif defined(OS_POSIX) 30d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) typedef pthread_mutex_t NativeHandle; 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) LockImpl(); 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~LockImpl(); 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If the lock is not held, take it and return true. If the lock is already 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // held by something else, immediately return false. 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool Try(); 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Take the lock, blocking until it is available if necessary. 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Lock(); 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Release the lock. This must only be called by the lock's holder: after 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // a successful call to Try, or a call to Lock. 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Unlock(); 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Return the native underlying lock. 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(awalker): refactor lock and condition variables so that this is 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // unnecessary. 50d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) NativeHandle* native_handle() { return &native_handle_; } 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 53d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) NativeHandle native_handle_; 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(LockImpl); 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace internal 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace base 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // BASE_SYNCHRONIZATION_LOCK_IMPL_H_ 62