[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
| 5 | #ifndef BASE_SEQUENCE_CHECKER_H_ |
| 6 | #define BASE_SEQUENCE_CHECKER_H_ |
| 7 | |
Hans Wennborg | 7b53371 | 2020-06-22 20:52:27 | [diff] [blame] | 8 | #include "base/check.h" |
Lei Zhang | f28a4c6 | 2021-12-21 01:31:57 | [diff] [blame^] | 9 | #include "base/dcheck_is_on.h" |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 10 | #include "base/sequence_checker_impl.h" |
Etienne Pierre-doray | 841f3e8 | 2020-01-14 17:10:25 | [diff] [blame] | 11 | #include "base/strings/string_piece.h" |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 12 | |
danakj | 894364e | 2021-01-27 21:51:29 | [diff] [blame] | 13 | #if DCHECK_IS_ON() |
| 14 | #include "base/debug/stack_trace.h" |
| 15 | #endif |
| 16 | |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 17 | // SequenceChecker is a helper class used to help verify that some methods of a |
Etienne Pierre-doray | 841f3e8 | 2020-01-14 17:10:25 | [diff] [blame] | 18 | // class are called sequentially (for thread-safety). It supports thread safety |
| 19 | // annotations (see base/thread_annotations.h). |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 20 | // |
| 21 | // Use the macros below instead of the SequenceChecker directly so that the |
| 22 | // unused member doesn't result in an extra byte (four when padded) per |
| 23 | // instance in production. |
| 24 | // |
| 25 | // This class is much prefered to ThreadChecker for thread-safety checks. |
| 26 | // ThreadChecker should only be used for classes that are truly thread-affine |
| 27 | // (use thread-local-storage or a third-party API that does). |
| 28 | // |
danakj | 894364e | 2021-01-27 21:51:29 | [diff] [blame] | 29 | // Debugging: |
| 30 | // If SequenceChecker::EnableStackLogging() is called beforehand, then when |
| 31 | // SequenceChecker fails, in addition to crashing with a stack trace of where |
| 32 | // the violation occurred, it will also dump a stack trace of where the |
| 33 | // checker was bound to a sequence. |
| 34 | // |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 35 | // Usage: |
| 36 | // class MyClass { |
| 37 | // public: |
| 38 | // MyClass() { |
Victor Costan | e1b5971e | 2021-01-06 01:35:08 | [diff] [blame] | 39 | // // Detaching on construction is necessary for objects that are |
| 40 | // // constructed on one sequence and forever after used from another |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 41 | // // sequence. |
| 42 | // DETACH_FROM_SEQUENCE(my_sequence_checker_); |
| 43 | // } |
| 44 | // |
| 45 | // ~MyClass() { |
| 46 | // // SequenceChecker doesn't automatically check it's destroyed on origin |
| 47 | // // sequence for the same reason it's sometimes detached in the |
| 48 | // // constructor. It's okay to destroy off sequence if the owner |
| 49 | // // otherwise knows usage on the associated sequence is done. If you're |
| 50 | // // not detaching in the constructor, you probably want to explicitly |
| 51 | // // check in the destructor. |
Christian Fremerey | 177b9b93 | 2017-06-02 16:55:02 | [diff] [blame] | 52 | // DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_); |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 53 | // } |
| 54 | // void MyMethod() { |
| 55 | // DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_); |
| 56 | // ... (do stuff) ... |
Etienne Pierre-doray | 841f3e8 | 2020-01-14 17:10:25 | [diff] [blame] | 57 | // MyOtherMethod(); |
| 58 | // } |
| 59 | // |
| 60 | // void MyOtherMethod() |
| 61 | // VALID_CONTEXT_REQUIRED(my_sequence_checker_) { |
| 62 | // foo_ = 42; |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 63 | // } |
| 64 | // |
| 65 | // private: |
Etienne Pierre-doray | 841f3e8 | 2020-01-14 17:10:25 | [diff] [blame] | 66 | // // GUARDED_BY_CONTEXT() enforces that this member is only |
| 67 | // // accessed from a scope that invokes DCHECK_CALLED_ON_VALID_SEQUENCE() |
| 68 | // // or from a function annotated with VALID_CONTEXT_REQUIRED(). A |
| 69 | // // DCHECK build will not compile if the member is accessed and these |
| 70 | // // conditions are not met. |
| 71 | // int foo_ GUARDED_BY_CONTEXT(my_sequence_checker_); |
| 72 | // |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 73 | // SEQUENCE_CHECKER(my_sequence_checker_); |
| 74 | // } |
| 75 | |
Etienne Pierre-doray | 841f3e8 | 2020-01-14 17:10:25 | [diff] [blame] | 76 | #define SEQUENCE_CHECKER_INTERNAL_CONCAT2(a, b) a##b |
| 77 | #define SEQUENCE_CHECKER_INTERNAL_CONCAT(a, b) \ |
| 78 | SEQUENCE_CHECKER_INTERNAL_CONCAT2(a, b) |
| 79 | #define SEQUENCE_CHECKER_INTERNAL_UID(prefix) \ |
| 80 | SEQUENCE_CHECKER_INTERNAL_CONCAT(prefix, __LINE__) |
| 81 | |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 82 | #if DCHECK_IS_ON() |
| 83 | #define SEQUENCE_CHECKER(name) base::SequenceChecker name |
Etienne Pierre-doray | 841f3e8 | 2020-01-14 17:10:25 | [diff] [blame] | 84 | #define DCHECK_CALLED_ON_VALID_SEQUENCE(name, ...) \ |
| 85 | base::ScopedValidateSequenceChecker SEQUENCE_CHECKER_INTERNAL_UID( \ |
Zhenyao Mo | 8f19d6a | 2020-04-14 10:47:30 | [diff] [blame] | 86 | scoped_validate_sequence_checker_)(name, ##__VA_ARGS__) |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 87 | #define DETACH_FROM_SEQUENCE(name) (name).DetachFromSequence() |
| 88 | #else // DCHECK_IS_ON() |
Nico Weber | 140566b1 | 2020-06-20 08:29:18 | [diff] [blame] | 89 | // A no-op expansion that can be followed by a semicolon at class level. |
Nico Weber | dcacb33 | 2019-03-09 02:32:02 | [diff] [blame] | 90 | #define SEQUENCE_CHECKER(name) static_assert(true, "") |
Hans Wennborg | 7b53371 | 2020-06-22 20:52:27 | [diff] [blame] | 91 | #define DCHECK_CALLED_ON_VALID_SEQUENCE(name, ...) EAT_CHECK_STREAM_PARAMS() |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 92 | #define DETACH_FROM_SEQUENCE(name) |
| 93 | #endif // DCHECK_IS_ON() |
| 94 | |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 95 | namespace base { |
| 96 | |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 97 | // Do nothing implementation, for use in release mode. |
| 98 | // |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 99 | // Note: You should almost always use the SequenceChecker class (through the |
| 100 | // above macros) to get the right version for your build configuration. |
Etienne Pierre-doray | f19e574 | 2020-12-09 00:47:43 | [diff] [blame] | 101 | // Note: This is marked with "context" capability in order to support |
| 102 | // thread_annotations.h. |
| 103 | class THREAD_ANNOTATION_ATTRIBUTE__(capability("context")) |
| 104 | SequenceCheckerDoNothing { |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 105 | public: |
danakj | 894364e | 2021-01-27 21:51:29 | [diff] [blame] | 106 | static void EnableStackLogging() {} |
| 107 | |
tzik | c342ef57 | 2017-07-21 08:09:50 | [diff] [blame] | 108 | SequenceCheckerDoNothing() = default; |
Gabriel Charette | 9746ffce | 2019-07-30 20:27:17 | [diff] [blame] | 109 | |
| 110 | // Moving between matching sequences is allowed to help classes with |
| 111 | // SequenceCheckers that want a default move-construct/assign. |
| 112 | SequenceCheckerDoNothing(SequenceCheckerDoNothing&& other) = default; |
| 113 | SequenceCheckerDoNothing& operator=(SequenceCheckerDoNothing&& other) = |
| 114 | default; |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 115 | SequenceCheckerDoNothing(const SequenceCheckerDoNothing&) = delete; |
| 116 | SequenceCheckerDoNothing& operator=(const SequenceCheckerDoNothing&) = delete; |
Gabriel Charette | 9746ffce | 2019-07-30 20:27:17 | [diff] [blame] | 117 | |
danakj | 894364e | 2021-01-27 21:51:29 | [diff] [blame] | 118 | bool CalledOnValidSequence(void* = nullptr) const WARN_UNUSED_RESULT { |
| 119 | return true; |
| 120 | } |
[email protected] | d52426c | 2013-07-30 19:26:40 | [diff] [blame] | 121 | void DetachFromSequence() {} |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 122 | }; |
| 123 | |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 124 | #if DCHECK_IS_ON() |
Etienne Pierre-doray | f19e574 | 2020-12-09 00:47:43 | [diff] [blame] | 125 | using SequenceChecker = SequenceCheckerImpl; |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 126 | #else |
Etienne Pierre-doray | f19e574 | 2020-12-09 00:47:43 | [diff] [blame] | 127 | using SequenceChecker = SequenceCheckerDoNothing; |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 128 | #endif // DCHECK_IS_ON() |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 129 | |
danakj | 894364e | 2021-01-27 21:51:29 | [diff] [blame] | 130 | #if DCHECK_IS_ON() |
Etienne Pierre-doray | 841f3e8 | 2020-01-14 17:10:25 | [diff] [blame] | 131 | class SCOPED_LOCKABLE ScopedValidateSequenceChecker { |
| 132 | public: |
| 133 | explicit ScopedValidateSequenceChecker(const SequenceChecker& checker) |
| 134 | EXCLUSIVE_LOCK_FUNCTION(checker) { |
danakj | 894364e | 2021-01-27 21:51:29 | [diff] [blame] | 135 | std::unique_ptr<debug::StackTrace> bound_at; |
| 136 | DCHECK(checker.CalledOnValidSequence(&bound_at)) |
| 137 | << (bound_at ? "\nWas attached to sequence at:\n" + bound_at->ToString() |
| 138 | : ""); |
Etienne Pierre-doray | 841f3e8 | 2020-01-14 17:10:25 | [diff] [blame] | 139 | } |
| 140 | |
Etienne Pierre-doray | 841f3e8 | 2020-01-14 17:10:25 | [diff] [blame] | 141 | ~ScopedValidateSequenceChecker() UNLOCK_FUNCTION() {} |
| 142 | |
| 143 | private: |
| 144 | }; |
danakj | 894364e | 2021-01-27 21:51:29 | [diff] [blame] | 145 | #endif |
Etienne Pierre-doray | 841f3e8 | 2020-01-14 17:10:25 | [diff] [blame] | 146 | |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 147 | } // namespace base |
| 148 | |
| 149 | #endif // BASE_SEQUENCE_CHECKER_H_ |