Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 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 | |
Lei Zhang | b9e7c4b8 | 2021-12-21 17:56:05 | [diff] [blame] | 8 | #include "base/base_export.h" |
Lei Zhang | f28a4c6 | 2021-12-21 01:31:57 | [diff] [blame] | 9 | #include "base/dcheck_is_on.h" |
Peter Kasting | 71f64d7 | 2023-08-03 20:41:30 | [diff] [blame] | 10 | #include "base/macros/uniquify.h" |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 11 | #include "base/sequence_checker_impl.h" |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 12 | |
François Doray | 62cb8e9 | 2024-06-05 14:49:02 | [diff] [blame] | 13 | // SequenceChecker verifies mutual exclusion between calls to its |
| 14 | // `CalledOnValidSequence()` method. Mutual exclusion is guaranteed if all calls |
| 15 | // are made from the same thread, from the same sequence (see |
François Doray | 6671c40 | 2024-06-21 17:47:39 | [diff] [blame] | 16 | // `SequencedTaskRunner`) or under the same lock acquired with |
| 17 | // `base::subtle::LockTracking::kEnabled`. SequenceChecker supports thread |
| 18 | // safety annotations (see base/thread_annotations.h). |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 19 | // |
| 20 | // Use the macros below instead of the SequenceChecker directly so that the |
François Doray | 62cb8e9 | 2024-06-05 14:49:02 | [diff] [blame] | 21 | // unused member doesn't result in an extra byte (four when padded) per instance |
| 22 | // in production. |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 23 | // |
| 24 | // This class is much prefered to ThreadChecker for thread-safety checks. |
| 25 | // ThreadChecker should only be used for classes that are truly thread-affine |
| 26 | // (use thread-local-storage or a third-party API that does). |
| 27 | // |
danakj | 894364e | 2021-01-27 21:51:29 | [diff] [blame] | 28 | // Debugging: |
| 29 | // If SequenceChecker::EnableStackLogging() is called beforehand, then when |
| 30 | // SequenceChecker fails, in addition to crashing with a stack trace of where |
| 31 | // the violation occurred, it will also dump a stack trace of where the |
| 32 | // checker was bound to a sequence. |
| 33 | // |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 34 | // Usage: |
| 35 | // class MyClass { |
| 36 | // public: |
| 37 | // MyClass() { |
Victor Costan | e1b5971e | 2021-01-06 01:35:08 | [diff] [blame] | 38 | // // Detaching on construction is necessary for objects that are |
| 39 | // // constructed on one sequence and forever after used from another |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 40 | // // sequence. |
François Doray | 62cb8e9 | 2024-06-05 14:49:02 | [diff] [blame] | 41 | // DETACH_FROM_SEQUENCE(sequence_checker_); |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 42 | // } |
| 43 | // |
| 44 | // ~MyClass() { |
| 45 | // // SequenceChecker doesn't automatically check it's destroyed on origin |
| 46 | // // sequence for the same reason it's sometimes detached in the |
| 47 | // // constructor. It's okay to destroy off sequence if the owner |
| 48 | // // otherwise knows usage on the associated sequence is done. If you're |
| 49 | // // not detaching in the constructor, you probably want to explicitly |
| 50 | // // check in the destructor. |
François Doray | 62cb8e9 | 2024-06-05 14:49:02 | [diff] [blame] | 51 | // DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 52 | // } |
| 53 | // void MyMethod() { |
François Doray | 62cb8e9 | 2024-06-05 14:49:02 | [diff] [blame] | 54 | // DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 55 | // ... (do stuff) ... |
Etienne Pierre-doray | 841f3e8 | 2020-01-14 17:10:25 | [diff] [blame] | 56 | // MyOtherMethod(); |
| 57 | // } |
| 58 | // |
François Doray | 62cb8e9 | 2024-06-05 14:49:02 | [diff] [blame] | 59 | // void MyOtherMethod() VALID_CONTEXT_REQUIRED(sequence_checker_) { |
Etienne Pierre-doray | 841f3e8 | 2020-01-14 17:10:25 | [diff] [blame] | 60 | // foo_ = 42; |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 61 | // } |
| 62 | // |
| 63 | // private: |
Etienne Pierre-doray | 841f3e8 | 2020-01-14 17:10:25 | [diff] [blame] | 64 | // // GUARDED_BY_CONTEXT() enforces that this member is only |
| 65 | // // accessed from a scope that invokes DCHECK_CALLED_ON_VALID_SEQUENCE() |
| 66 | // // or from a function annotated with VALID_CONTEXT_REQUIRED(). A |
| 67 | // // DCHECK build will not compile if the member is accessed and these |
| 68 | // // conditions are not met. |
François Doray | 62cb8e9 | 2024-06-05 14:49:02 | [diff] [blame] | 69 | // int foo_ GUARDED_BY_CONTEXT(sequence_checker_); |
Etienne Pierre-doray | 841f3e8 | 2020-01-14 17:10:25 | [diff] [blame] | 70 | // |
François Doray | 62cb8e9 | 2024-06-05 14:49:02 | [diff] [blame] | 71 | // SEQUENCE_CHECKER(sequence_checker_); |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 72 | // } |
| 73 | |
| 74 | #if DCHECK_IS_ON() |
| 75 | #define SEQUENCE_CHECKER(name) base::SequenceChecker name |
Peter Kasting | 71f64d7 | 2023-08-03 20:41:30 | [diff] [blame] | 76 | #define DCHECK_CALLED_ON_VALID_SEQUENCE(name, ...) \ |
| 77 | base::ScopedValidateSequenceChecker BASE_UNIQUIFY( \ |
Zhenyao Mo | 8f19d6a | 2020-04-14 10:47:30 | [diff] [blame] | 78 | scoped_validate_sequence_checker_)(name, ##__VA_ARGS__) |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 79 | #define DETACH_FROM_SEQUENCE(name) (name).DetachFromSequence() |
| 80 | #else // DCHECK_IS_ON() |
Nico Weber | 140566b1 | 2020-06-20 08:29:18 | [diff] [blame] | 81 | // 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] | 82 | #define SEQUENCE_CHECKER(name) static_assert(true, "") |
Hans Wennborg | 7b53371 | 2020-06-22 20:52:27 | [diff] [blame] | 83 | #define DCHECK_CALLED_ON_VALID_SEQUENCE(name, ...) EAT_CHECK_STREAM_PARAMS() |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 84 | #define DETACH_FROM_SEQUENCE(name) |
| 85 | #endif // DCHECK_IS_ON() |
| 86 | |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 87 | namespace base { |
| 88 | |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 89 | // Do nothing implementation, for use in release mode. |
| 90 | // |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 91 | // Note: You should almost always use the SequenceChecker class (through the |
| 92 | // above macros) to get the right version for your build configuration. |
Etienne Pierre-doray | f19e574 | 2020-12-09 00:47:43 | [diff] [blame] | 93 | // Note: This is marked with "context" capability in order to support |
| 94 | // thread_annotations.h. |
| 95 | class THREAD_ANNOTATION_ATTRIBUTE__(capability("context")) |
| 96 | SequenceCheckerDoNothing { |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 97 | public: |
danakj | 894364e | 2021-01-27 21:51:29 | [diff] [blame] | 98 | static void EnableStackLogging() {} |
| 99 | |
tzik | c342ef57 | 2017-07-21 08:09:50 | [diff] [blame] | 100 | SequenceCheckerDoNothing() = default; |
Gabriel Charette | 9746ffce | 2019-07-30 20:27:17 | [diff] [blame] | 101 | |
| 102 | // Moving between matching sequences is allowed to help classes with |
| 103 | // SequenceCheckers that want a default move-construct/assign. |
| 104 | SequenceCheckerDoNothing(SequenceCheckerDoNothing&& other) = default; |
| 105 | SequenceCheckerDoNothing& operator=(SequenceCheckerDoNothing&& other) = |
| 106 | default; |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 107 | SequenceCheckerDoNothing(const SequenceCheckerDoNothing&) = delete; |
| 108 | SequenceCheckerDoNothing& operator=(const SequenceCheckerDoNothing&) = delete; |
Gabriel Charette | 9746ffce | 2019-07-30 20:27:17 | [diff] [blame] | 109 | |
Daniel Cheng | 4455c984 | 2022-01-13 23:26:37 | [diff] [blame] | 110 | [[nodiscard]] bool CalledOnValidSequence(void* = nullptr) const { |
danakj | 894364e | 2021-01-27 21:51:29 | [diff] [blame] | 111 | return true; |
| 112 | } |
[email protected] | d52426c | 2013-07-30 19:26:40 | [diff] [blame] | 113 | void DetachFromSequence() {} |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 114 | }; |
| 115 | |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 116 | #if DCHECK_IS_ON() |
Etienne Pierre-doray | f19e574 | 2020-12-09 00:47:43 | [diff] [blame] | 117 | using SequenceChecker = SequenceCheckerImpl; |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 118 | #else |
Etienne Pierre-doray | f19e574 | 2020-12-09 00:47:43 | [diff] [blame] | 119 | using SequenceChecker = SequenceCheckerDoNothing; |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 120 | #endif // DCHECK_IS_ON() |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 121 | |
danakj | 894364e | 2021-01-27 21:51:29 | [diff] [blame] | 122 | #if DCHECK_IS_ON() |
Lei Zhang | b9e7c4b8 | 2021-12-21 17:56:05 | [diff] [
|