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