1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_SYNCHRONIZATION_LOCK_IMPL_H_ 6*635a8641SAndroid Build Coastguard Worker #define BASE_SYNCHRONIZATION_LOCK_IMPL_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h" 9*635a8641SAndroid Build Coastguard Worker #include "base/logging.h" 10*635a8641SAndroid Build Coastguard Worker #include "base/macros.h" 11*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h" 12*635a8641SAndroid Build Coastguard Worker 13*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN) 14*635a8641SAndroid Build Coastguard Worker #include "base/win/windows_types.h" 15*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA) 16*635a8641SAndroid Build Coastguard Worker #include <errno.h> 17*635a8641SAndroid Build Coastguard Worker #include <pthread.h> 18*635a8641SAndroid Build Coastguard Worker #endif 19*635a8641SAndroid Build Coastguard Worker 20*635a8641SAndroid Build Coastguard Worker namespace base { 21*635a8641SAndroid Build Coastguard Worker namespace internal { 22*635a8641SAndroid Build Coastguard Worker 23*635a8641SAndroid Build Coastguard Worker // This class implements the underlying platform-specific spin-lock mechanism 24*635a8641SAndroid Build Coastguard Worker // used for the Lock class. Most users should not use LockImpl directly, but 25*635a8641SAndroid Build Coastguard Worker // should instead use Lock. 26*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT LockImpl { 27*635a8641SAndroid Build Coastguard Worker public: 28*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN) 29*635a8641SAndroid Build Coastguard Worker using NativeHandle = CHROME_SRWLOCK; 30*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA) 31*635a8641SAndroid Build Coastguard Worker using NativeHandle = pthread_mutex_t; 32*635a8641SAndroid Build Coastguard Worker #endif 33*635a8641SAndroid Build Coastguard Worker 34*635a8641SAndroid Build Coastguard Worker LockImpl(); 35*635a8641SAndroid Build Coastguard Worker ~LockImpl(); 36*635a8641SAndroid Build Coastguard Worker 37*635a8641SAndroid Build Coastguard Worker // If the lock is not held, take it and return true. If the lock is already 38*635a8641SAndroid Build Coastguard Worker // held by something else, immediately return false. 39*635a8641SAndroid Build Coastguard Worker bool Try(); 40*635a8641SAndroid Build Coastguard Worker 41*635a8641SAndroid Build Coastguard Worker // Take the lock, blocking until it is available if necessary. 42*635a8641SAndroid Build Coastguard Worker void Lock(); 43*635a8641SAndroid Build Coastguard Worker 44*635a8641SAndroid Build Coastguard Worker // Release the lock. This must only be called by the lock's holder: after 45*635a8641SAndroid Build Coastguard Worker // a successful call to Try, or a call to Lock. 46*635a8641SAndroid Build Coastguard Worker inline void Unlock(); 47*635a8641SAndroid Build Coastguard Worker 48*635a8641SAndroid Build Coastguard Worker // Return the native underlying lock. 49*635a8641SAndroid Build Coastguard Worker // TODO(awalker): refactor lock and condition variables so that this is 50*635a8641SAndroid Build Coastguard Worker // unnecessary. native_handle()51*635a8641SAndroid Build Coastguard Worker NativeHandle* native_handle() { return &native_handle_; } 52*635a8641SAndroid Build Coastguard Worker 53*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX) || defined(OS_FUCHSIA) 54*635a8641SAndroid Build Coastguard Worker // Whether this lock will attempt to use priority inheritance. 55*635a8641SAndroid Build Coastguard Worker static bool PriorityInheritanceAvailable(); 56*635a8641SAndroid Build Coastguard Worker #endif 57*635a8641SAndroid Build Coastguard Worker 58*635a8641SAndroid Build Coastguard Worker private: 59*635a8641SAndroid Build Coastguard Worker NativeHandle native_handle_; 60*635a8641SAndroid Build Coastguard Worker 61*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(LockImpl); 62*635a8641SAndroid Build Coastguard Worker }; 63*635a8641SAndroid Build Coastguard Worker 64*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN) Unlock()65*635a8641SAndroid Build Coastguard Workervoid LockImpl::Unlock() { 66*635a8641SAndroid Build Coastguard Worker ::ReleaseSRWLockExclusive(reinterpret_cast<PSRWLOCK>(&native_handle_)); 67*635a8641SAndroid Build Coastguard Worker } 68*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA) Unlock()69*635a8641SAndroid Build Coastguard Workervoid LockImpl::Unlock() { 70*635a8641SAndroid Build Coastguard Worker int rv = pthread_mutex_unlock(&native_handle_); 71*635a8641SAndroid Build Coastguard Worker DCHECK_EQ(rv, 0) << ". " << strerror(rv); 72*635a8641SAndroid Build Coastguard Worker } 73*635a8641SAndroid Build Coastguard Worker #endif 74*635a8641SAndroid Build Coastguard Worker 75*635a8641SAndroid Build Coastguard Worker } // namespace internal 76*635a8641SAndroid Build Coastguard Worker } // namespace base 77*635a8641SAndroid Build Coastguard Worker 78*635a8641SAndroid Build Coastguard Worker #endif // BASE_SYNCHRONIZATION_LOCK_IMPL_H_ 79