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_WIN) 68 // Windows mitigates priority inversion by randomly boosting the priority of 69 // ready threads. 70 // https://msdn.microsoft.com/library/windows/desktop/ms684831.aspx 71 return true; 72 #elif defined(OS_POSIX) || defined(OS_FUCHSIA) 73 // POSIX mitigates priority inversion by setting the priority of a thread 74 // holding a Lock to the maximum priority of any other thread waiting on it. 75 return internal::LockImpl::PriorityInheritanceAvailable(); 76 #else 77 #error Unsupported platform 78 #endif 79 } 80 81 // Both Windows and POSIX implementations of ConditionVariable need to be 82 // able to see our lock and tweak our debugging counters, as they release and 83 // acquire locks inside of their condition variable APIs. 84 friend class ConditionVariable; 85 86 private: 87 #if DCHECK_IS_ON() 88 // Members and routines taking care of locks assertions. 89 // Note that this checks for recursive locks and allows them 90 // if the variable is set. This is allowed by the underlying implementation 91 // on windows but not on Posix, so we're doing unneeded checks on Posix. 92 // It's worth it to share the code. 93 void CheckHeldAndUnmark(); 94 void CheckUnheldAndMark(); 95 96 // All private data is implicitly protected by lock_. 97 // Be VERY careful to only access members under that lock. 98 base::PlatformThreadRef owning_thread_ref_; 99 #endif // DCHECK_IS_ON() 100 101 // Platform specific underlying lock implementation. 102 internal::LockImpl lock_; 103 104 DISALLOW_COPY_AND_ASSIGN(Lock); 105 }; 106 107 // A helper class that acquires the given Lock while the AutoLock is in scope. 108 class AutoLock { 109 public: 110 struct AlreadyAcquired {}; 111 AutoLock(Lock & lock)112 explicit AutoLock(Lock& lock) : lock_(lock) { 113 lock_.Acquire(); 114 } 115 AutoLock(Lock & lock,const AlreadyAcquired &)116 AutoLock(Lock& lock, const AlreadyAcquired&) : lock_(lock) { 117 lock_.AssertAcquired(); 118 } 119 ~AutoLock()120 ~AutoLock() { 121 lock_.AssertAcquired(); 122 lock_.Release(); 123 } 124 125 private: 126 Lock& lock_; 127 DISALLOW_COPY_AND_ASSIGN(AutoLock); 128 }; 129 130 // AutoUnlock is a helper that will Release() the |lock| argument in the 131 // constructor, and re-Acquire() it in the destructor. 132 class AutoUnlock { 133 public: AutoUnlock(Lock & lock)134 explicit AutoUnlock(Lock& lock) : lock_(lock) { 135 // We require our caller to have the lock. 136 lock_.AssertAcquired(); 137 lock_.Release(); 138 } 139 ~AutoUnlock()140 ~AutoUnlock() { 141 lock_.Acquire(); 142 } 143 144 private: 145 Lock& lock_; 146 DISALLOW_COPY_AND_ASSIGN(AutoUnlock); 147 }; 148 149 } // namespace base 150 151 #endif // BASE_SYNCHRONIZATION_LOCK_H_ 152