checked_lock.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.
  30. // On Acquisition if the previous lock acquired on the thread is not
  31. // either |predecessor| or a universal predecessor. Okay if there
  32. // was no previous lock acquired.
  33. //
  34. // CheckedLock(UniversalPredecessor universal_predecessor)
  35. // Constructor for a lock that will allow the acquisition of any lock after
  36. // it, without needing to explicitly be named a predecessor. Can only be
  37. // acquired if no locks are currently held by this thread.
  38. // DCHECKs
  39. // On Acquisition if any CheckedLock is acquired on this thread.
  40. //
  41. // void Acquire()
  42. // Acquires the lock.
  43. //
  44. // void Release()
  45. // Releases the lock.
  46. //
  47. // void AssertAcquired().
  48. // DCHECKs if the lock is not acquired.
  49. //
  50. // std::unique_ptr<ConditionVariable> CreateConditionVariable()
  51. // Creates a condition variable using this as a lock.
  52. #if DCHECK_IS_ON()
  53. class LOCKABLE CheckedLock : public CheckedLockImpl {
  54. public:
  55. CheckedLock() = default;
  56. explicit CheckedLock(const CheckedLock* predecessor)
  57. : CheckedLockImpl(predecessor) {}
  58. explicit CheckedLock(UniversalPredecessor universal_predecessor)
  59. : CheckedLockImpl(universal_predecessor) {}
  60. };
  61. #else // DCHECK_IS_ON()
  62. class LOCKABLE CheckedLock : public Lock {
  63. public:
  64. CheckedLock() = default;
  65. explicit CheckedLock(const CheckedLock*) {}
  66. explicit CheckedLock(UniversalPredecessor) {}
  67. static void AssertNoLockHeldOnCurrentThread() {}
  68. std::unique_ptr<ConditionVariable> CreateConditionVariable() {
  69. return std::unique_ptr<ConditionVariable>(new ConditionVariable(this));
  70. }
  71. };
  72. #endif // DCHECK_IS_ON()
  73. // Provides the same functionality as base::AutoLock for CheckedLock.
  74. using CheckedAutoLock = internal::BasicAutoLock<CheckedLock>;
  75. // Provides the same functionality as base::AutoUnlock for CheckedLock.
  76. using CheckedAutoUnlock = internal::BasicAutoUnlock<CheckedLock>;
  77. // Provides the same functionality as base::AutoLockMaybe for CheckedLock.
  78. using CheckedAutoLockMaybe = internal::BasicAutoLockMaybe<CheckedLock>;
  79. // Informs the clang thread safety analysis that an aliased lock is acquired.
  80. // Because the clang thread safety analysis doesn't understand aliased locks
  81. // [1], this code wouldn't compile without AnnotateAcquiredLockAlias:
  82. //
  83. // class Example {
  84. // public:
  85. // CheckedLock lock_;
  86. // int value = 0 GUARDED_BY(lock_);
  87. // };
  88. //
  89. // Example example;
  90. // CheckedLock* acquired = &example.lock_;
  91. // CheckedAutoLock auto_lock(*acquired);
  92. // AnnotateAcquiredLockAlias annotate(*acquired, example.lock_);
  93. // example.value = 42; // Doesn't compile without |annotate|.
  94. //
  95. // [1] https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#no-alias-analysis
  96. class SCOPED_LOCKABLE AnnotateAcquiredLockAlias {
  97. public:
  98. // |acquired_lock| is an acquired lock. |lock_alias| is an alias of
  99. // |acquired_lock|.
  100. AnnotateAcquiredLockAlias(const CheckedLock& acquired_lock,
  101. const CheckedLock& lock_alias)
  102. EXCLUSIVE_LOCK_FUNCTION(lock_alias)
  103. : acquired_lock_(acquired_lock) {
  104. DCHECK_EQ(&acquired_lock, &lock_alias);
  105. acquired_lock_.AssertAcquired();
  106. }
  107. ~AnnotateAcquiredLockAlias() UNLOCK_FUNCTION() {
  108. acquired_lock_.AssertAcquired();
  109. }
  110. private:
  111. const CheckedLock& acquired_lock_;
  112. DISALLOW_COPY_AND_ASSIGN(AnnotateAcquiredLockAlias);
  113. };
  114. } // namespace internal
  115. } // namespace base
  116. #endif // BASE_TASK_COMMON_CHECKED_LOCK_H_