checked_lock.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2016 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_TASK_COMMON_CHECKED_LOCK_H_
  5. #define BASE_TASK_COMMON_CHECKED_LOCK_H_
  6. #include <memory>
  7. #include "base/base_export.h"
  8. #include "base/macros.h"
  9. #include "base/synchronization/condition_variable.h"
  10. #include "base/synchronization/lock.h"
  11. #include "base/task/common/checked_lock_impl.h"
  12. #include "base/thread_annotations.h"
  13. namespace base {
  14. namespace internal {
  15. // CheckedLock should be used anywhere a Lock would be used in the base/task
  16. // impl. When DCHECK_IS_ON(), lock checking occurs. Otherwise, CheckedLock is
  17. // equivalent to base::Lock.
  18. //
  19. // The shape of CheckedLock is as follows:
  20. // CheckedLock()
  21. // Default constructor, no predecessor lock.
  22. // DCHECKs
  23. // On Acquisition if any CheckedLock is acquired on this thread.
  24. // Okay if a universal predecessor is acquired.
  25. //
  26. // CheckedLock(const CheckedLock* predecessor)
  27. // Constructor that specifies an allowed predecessor for that lock.
  28. // DCHECKs
  29. // On Construction if |predecessor| forms a predecessor lock cycle or
  30. // is a universal successor.
  31. // On Acquisition if the previous lock acquired on the thread is not
  32. // either |predecessor| or a universal predecessor. Okay if there
  33. // was no previous lock acquired.
  34. //
  35. // CheckedLock(UniversalPredecessor universal_predecessor)
  36. // Constructor for a lock that will allow the acquisition of any lock after
  37. // it, without needing to explicitly be named a predecessor (e.g. a root in
  38. // a lock chain). Can only be acquired if no locks are currently held by
  39. // this thread. DCHECKs
  40. // On Acquisition if any CheckedLock is acquired on this thread.
  41. //
  42. // CheckedLock(UniversalSuccessor universal_successor)
  43. // Constructor for a lock that will allow its acquisition after any other
  44. // lock, without needing to explicitly name its predecessor (e.g. a leaf in
  45. // a lock chain). Can not be acquired after another UniversalSuccessor lock.
  46. // DCHECKs
  47. // On Acquisition if there was a previously acquired lock on the thread
  48. // and it was also a universal successor.
  49. //
  50. // void Acquire()
  51. // Acquires the lock.
  52. //
  53. // void Release()
  54. // Releases the lock.
  55. //
  56. // void AssertAcquired().
  57. // DCHECKs if the lock is not acquired.
  58. //
  59. // std::unique_ptr<ConditionVariable> CreateConditionVariable()
  60. // Creates a condition variable using this as a lock.
  61. #if DCHECK_IS_ON()
  62. class LOCKABLE CheckedLock : public CheckedLockImpl {
  63. public:
  64. CheckedLock() = default;
  65. explicit CheckedLock(const CheckedLock* predecessor)
  66. : CheckedLockImpl(predecessor) {}
  67. explicit CheckedLock(UniversalPredecessor universal_predecessor)
  68. : CheckedLockImpl(universal_predecessor) {}
  69. explicit CheckedLock(UniversalSuccessor universal_successor)
  70. : CheckedLockImpl(universal_successor) {}
  71. };
  72. #else // DCHECK_IS_ON()
  73. class LOCKABLE CheckedLock : public Lock {
  74. public:
  75. CheckedLock() = default;
  76. explicit CheckedLock(const CheckedLock*) {}
  77. explicit CheckedLock(UniversalPredecessor) {}
  78. explicit CheckedLock(UniversalSuccessor) {}
  79. static void AssertNoLockHeldOnCurrentThread() {}
  80. std::unique_ptr<ConditionVariable> CreateConditionVariable() {
  81. return std::unique_ptr<ConditionVariable>(new ConditionVariable(this));
  82. }
  83. };
  84. #endif // DCHECK_IS_ON()
  85. // Provides the same functionality as base::AutoLock for CheckedLock.
  86. using CheckedAutoLock = internal::BasicAutoLock<CheckedLock>;
  87. // Provides the same functionality as base::AutoUnlock for CheckedLock.
  88. using CheckedAutoUnlock = internal::BasicAutoUnlock<CheckedLock>;
  89. // Provides the same functionality as base::AutoLockMaybe for CheckedLock.
  90. using CheckedAutoLockMaybe = internal::BasicAutoLockMaybe<CheckedLock>;
  91. // Informs the clang thread safety analysis that an aliased lock is acquired.
  92. // Because the clang thread safety analysis doesn't understand aliased locks
  93. // [1], this code wouldn't compile without AnnotateAcquiredLockAlias:
  94. //
  95. // class Example {
  96. // public:
  97. // CheckedLock lock_;
  98. // int value = 0 GUARDED_BY(lock_);
  99. // };
  100. //
  101. // Example example;
  102. // CheckedLock* acquired = &example.lock_;
  103. // CheckedAutoLock auto_lock(*acquired);
  104. // AnnotateAcquiredLockAlias annotate(*acquired, example.lock_);
  105. // example.value = 42; // Doesn't compile without |annotate|.
  106. //
  107. // [1] https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#no-alias-analysis
  108. class SCOPED_LOCKABLE AnnotateAcquiredLockAlias {
  109. public:
  110. // |acquired_lock| is an acquired lock. |lock_alias| is an alias of
  111. // |acquired_lock|.
  112. AnnotateAcquiredLockAlias(const CheckedLock& acquired_lock,
  113. const CheckedLock& lock_alias)
  114. EXCLUSIVE_LOCK_FUNCTION(lock_alias)
  115. : acquired_lock_(acquired_lock) {
  116. DCHECK_EQ(&acquired_lock, &lock_alias);
  117. acquired_lock_.AssertAcquired();
  118. }
  119. ~AnnotateAcquiredLockAlias() UNLOCK_FUNCTION() {
  120. acquired_lock_.AssertAcquired();
  121. }
  122. private:
  123. const CheckedLock& acquired_lock_;
  124. DISALLOW_COPY_AND_ASSIGN(AnnotateAcquiredLockAlias);
  125. };
  126. } // namespace internal
  127. } // namespace base
  128. #endif // BASE_TASK_COMMON_CHECKED_LOCK_H_