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_H_ 6 #define BASE_SYNCHRONIZATION_LOCK_H_ 7 8 #include "base/base_export.h" 9 #include "base/logging.h" 10 #include "base/macros.h" 11 #include "base/synchronization/lock_impl.h" 12 #include "base/threading/platform_thread.h" 13 #include "build/build_config.h" 14 15 namespace base { 16 17 // A convenient wrapper for an OS specific critical section. The only real 18 // intelligence in this class is in debug mode for the support for the 19 // AssertAcquired() method. 20 class BASE_EXPORT Lock { 21 public: 22 #if !DCHECK_IS_ON() 23 // Optimized wrapper implementation Lock()24 Lock() : lock_() {} ~Lock()25 ~Lock() {} Acquire()26 void Acquire() { lock_.Lock(); } Release()27 void Release() { lock_.Unlock(); } 28 29 // If the lock is not held, take it and return true. If the lock is already 30 // held by another thread, immediately return false. This must not be called 31 // by a thread already holding the lock (what happens is undefined and an 32 // assertion may fail). Try()33 bool Try() { return lock_.Try(); } 34 35 // Null implementation if not debug. AssertAcquired()36 void AssertAcquired() const {} 37 #else 38 Lock(); 39 ~Lock(); 40 41 // NOTE: We do not permit recursive locks and will commonly fire a DCHECK() if 42 // a thread attempts to acquire the lock a second time (while already holding 43 // it). 44 void Acquire() { 45 lock_.Lock(); 46 CheckUnheldAndMark(); 47 } 48 void Release() { 49 CheckHeldAndUnmark(); 50 lock_.Unlock(); 51 } 52 53 bool Try() { 54 bool rv = lock_.Try(); 55 if (rv) { 56 CheckUnheldAndMark(); 57 } 58 return rv; 59 } 60 61 void AssertAcquired() const; 62 #endif // DCHECK_IS_ON() 63 64 // Whether Lock mitigates priority inversion when used from different thread 65 // priorities. HandlesMultipleThreadPriorities()66 static bool HandlesMultipleThreadPriorities() { 67 #if defined(OS_POSIX) 68 // POSIX mitigates priority inversion by setting the priority of a thread 69 // holding a Lock to the maximum priority of any other thread waiting on it. 70 return internal::LockImpl::PriorityInheritanceAvailable(); 71 #elif defined(OS_WIN) 72 // Windows mitigates priority inversion by randomly boosting the priority of 73 // ready threads. 74 // https://msdn.microsoft.com/library/windows/desktop/ms684831.aspx 75 return true; 76 #else 77 #error Unsupported platform 78 #endif 79 } 80 81 #if defined(OS_POSIX) || defined(OS_WIN) 82 // Both Windows and POSIX implementations of ConditionVariable need to be 83 // able to see our lock and tweak our debugging counters, as they release and 84 // acquire locks inside of their condition variable APIs. 85 friend class ConditionVariable; 86 #endif 87 88 private: 89 #if DCHECK_IS_ON() 90 // Members and routines taking care of locks assertions. 91 // Note that this checks for recursive locks and allows them 92 // if the variable is set. This is allowed by the underlying implementation 93 // on windows but not on Posix, so we're doing unneeded checks on Posix. 94 // It's worth it to share the code. 95 void CheckHeldAndUnmark(); 96 void CheckUnheldAndMark(); 97 98 // All private data is implicitly protected by lock_. 99 // Be VERY careful to only access members under that lock. 100 base::PlatformThreadRef owning_thread_ref_; 101 #endif // DCHECK_IS_ON() 102 103 // Platform specific underlying lock implementation. 104 internal::LockImpl lock_; 105 106 DISALLOW_COPY_AND_ASSIGN(Lock); 107 }; 108 109 // A helper class that acquires the given Lock while the AutoLock is in scope. 110 class AutoLock { 111 public: 112 struct AlreadyAcquired {}; 113 AutoLock(Lock & lock)114 explicit AutoLock(Lock& lock) : lock_(lock) { 115 lock_.Acquire(); 116 } 117 AutoLock(Lock & lock,const AlreadyAcquired &)118 AutoLock(Lock& lock, const AlreadyAcquired&) : lock_(lock) { 119 lock_.AssertAcquired(); 120 } 121 ~AutoLock()122 ~AutoLock() { 123 lock_.AssertAcquired(); 124 lock_.Release(); 125 } 126 127 private: 128 Lock& lock_; 129 DISALLOW_COPY_AND_ASSIGN(AutoLock); 130 }; 131 132 // AutoUnlock is a helper that will Release() the |lock| argument in the 133 // constructor, and re-Acquire() it in the destructor. 134 class AutoUnlock { 135 public: AutoUnlock(Lock & lock)136 explicit AutoUnlock(Lock& lock) : lock_(lock) { 137 // We require our caller to have the lock. 138 lock_.AssertAcquired(); 139 lock_.Release(); 140 } 141 ~AutoUnlock()142 ~AutoUnlock() { 143 lock_.Acquire(); 144 } 145 146 private: 147 Lock& lock_; 148 DISALLOW_COPY_AND_ASSIGN(AutoUnlock); 149 }; 150 151 } // namespace base 152 153 #endif // BASE_SYNCHRONIZATION_LOCK_H_ 154