lock.h 4.6 KB

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