1 // Copyright 2011 The Chromium Authors 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/dcheck_is_on.h" 10 #include "base/synchronization/lock_impl.h" 11 #include "base/thread_annotations.h" 12 #include "build/build_config.h" 13 14 #if DCHECK_IS_ON() 15 #include "base/threading/platform_thread_ref.h" 16 #endif 17 18 namespace base { 19 20 // A convenient wrapper for an OS specific critical section. The only real 21 // intelligence in this class is in debug mode for the support for the 22 // AssertAcquired() method. 23 class LOCKABLE BASE_EXPORT Lock { 24 public: 25 #if !DCHECK_IS_ON() 26 // Optimized wrapper implementation Lock()27 Lock() : lock_() {} 28 29 Lock(const Lock&) = delete; 30 Lock& operator=(const Lock&) = delete; 31 ~Lock()32 ~Lock() {} 33 Acquire()34 void Acquire() EXCLUSIVE_LOCK_FUNCTION() { lock_.Lock(); } Release()35 void Release() UNLOCK_FUNCTION() { lock_.Unlock(); } 36 37 // If the lock is not held, take it and return true. If the lock is already 38 // held by another thread, immediately return false. This must not be called 39 // by a thread already holding the lock (what happens is undefined and an 40 // assertion may fail). Try()41 bool Try() EXCLUSIVE_TRYLOCK_FUNCTION(true) { return lock_.Try(); } 42 43 // Null implementation if not debug. AssertAcquired()44 void AssertAcquired() const ASSERT_EXCLUSIVE_LOCK() {} AssertNotHeld()45 void AssertNotHeld() const {} 46 #else 47 Lock(); 48 ~Lock(); 49 50 // NOTE: We do not permit recursive locks and will commonly fire a DCHECK() if 51 // a thread attempts to acquire the lock a second time (while already holding 52 // it). 53 void Acquire() EXCLUSIVE_LOCK_FUNCTION() { 54 lock_.Lock(); 55 CheckUnheldAndMark(); 56 } 57 void Release() UNLOCK_FUNCTION() { 58 CheckHeldAndUnmark(); 59 lock_.Unlock(); 60 } 61 62 bool Try() EXCLUSIVE_TRYLOCK_FUNCTION(true) { 63 bool rv = lock_.Try(); 64 if (rv) { 65 CheckUnheldAndMark(); 66 } 67 return rv; 68 } 69 70 void AssertAcquired() const ASSERT_EXCLUSIVE_LOCK(); 71 void AssertNotHeld() const; 72 #endif // DCHECK_IS_ON() 73 74 // Whether Lock mitigates priority inversion when used from different thread 75 // priorities. HandlesMultipleThreadPriorities()76 static bool HandlesMultipleThreadPriorities() { 77 #if BUILDFLAG(IS_WIN) 78 // Windows mitigates priority inversion by randomly boosting the priority of 79 // ready threads. 80 // https://msdn.microsoft.com/library/windows/desktop/ms684831.aspx 81 return true; 82 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) 83 // POSIX mitigates priority inversion by setting the priority of a thread 84 // holding a Lock to the maximum priority of any other thread waiting on it. 85 return internal::LockImpl::PriorityInheritanceAvailable(); 86 #else 87 #error Unsupported platform 88 #endif 89 } 90 91 // Both Windows and POSIX implementations of ConditionVariable need to be 92 // able to see our lock and tweak our debugging counters, as they release and 93 // acquire locks inside of their condition variable APIs. 94 friend class ConditionVariable; 95 96 private: 97 #if DCHECK_IS_ON() 98 // Members and routines taking care of locks assertions. 99 // Note that this checks for recursive locks and allows them 100 // if the variable is set. This is allowed by the underlying implementation 101 // on windows but not on Posix, so we're doing unneeded checks on Posix. 102 // It's worth it to share the code. 103 void CheckHeldAndUnmark(); 104 void CheckUnheldAndMark(); 105 106 // All private data is implicitly protected by lock_. 107 // Be VERY careful to only access members under that lock. 108 base::PlatformThreadRef owning_thread_ref_; 109 #endif // DCHECK_IS_ON() 110 111 // Platform specific underlying lock implementation. 112 internal::LockImpl lock_; 113 }; 114 115 // A helper class that acquires the given Lock while the AutoLock is in scope. 116 using AutoLock = internal::BasicAutoLock<Lock>; 117 118 // A helper class that tries to acquire the given Lock while the AutoTryLock is 119 // in scope. 120 using AutoTryLock = internal::BasicAutoTryLock<Lock>; 121 122 // AutoUnlock is a helper that will Release() the |lock| argument in the 123 // constructor, and re-Acquire() it in the destructor. 124 using AutoUnlock = internal::BasicAutoUnlock<Lock>; 125 126 // Like AutoLock but is a no-op when the provided Lock* is null. Inspired from 127 // absl::MutexLockMaybe. Use this instead of std::optional<base::AutoLock> to 128 // get around -Wthread-safety-analysis warnings for conditional locking. 129 using AutoLockMaybe = internal::BasicAutoLockMaybe<Lock>; 130 131 // Like AutoLock but permits Release() of its mutex before destruction. 132 // Release() may be called at most once. Inspired from 133 // absl::ReleasableMutexLock. Use this instead of std::optional<base::AutoLock> 134 // to get around -Wthread-safety-analysis warnings for AutoLocks that are 135 // explicitly released early (prefer proper scoping to this). 136 using ReleasableAutoLock = internal::BasicReleasableAutoLock<Lock>; 137 138 } // namespace base 139 140 #endif // BASE_SYNCHRONIZATION_LOCK_H_ 141