lock.h 4.8 KB

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