| 6 | #define BASE_SEQUENCE_CHECKER_H_ |
| 7 |
| 8 | #include "base/compiler_specific.h" |
| 9 | #include "base/logging.h" |
| 10 | #include "base/sequence_checker_impl.h" |
| 11 | #include "base/strings/string_piece.h" |
| 12 | #include "build/build_config.h" |
| 13 |
| 14 | // SequenceChecker is a helper class used to help verify that some methods of a |
| 15 | // class are called sequentially (for thread-safety). It supports thread safety |
| 16 | // annotations (see base/thread_annotations.h). |
| 17 | // |
| 18 | // Use the macros below instead of the SequenceChecker directly so that the |
| 19 | // unused member doesn't result in an extra byte (four when padded) per |
| 20 | // instance in production. |
| 21 | // |
| 22 | // This class is much prefered to ThreadChecker for thread-safety checks. |
| 23 | // ThreadChecker should only be used for classes that are truly thread-affine |
| 24 | // (use thread-local-storage or a third-party API that does). |
| 25 | // |
| 26 | // Usage: |
| 27 | // class MyClass { |
| 28 | // public: |
| 29 | // MyClass() { |
| 30 | // // It's sometimes useful to detach on construction for objects that are |
| 31 | // // constructed in one place and forever after used from another |
| 32 | // // sequence. |
| 33 | // DETACH_FROM_SEQUENCE(my_sequence_checker_); |
| 34 | // } |
| 35 | // |
| 36 | // ~MyClass() { |
| 37 | // // SequenceChecker doesn't automatically check it's destroyed on origin |
| 38 | // // sequence for the same reason it's sometimes detached in the |
| 39 | // // constructor. It's okay to destroy off sequence if the owner |
| 40 | // // otherwise knows usage on the associated sequence is done. If you're |
| 41 | // // not detaching in the constructor, you probably want to explicitly |
| 42 | // // check in the destructor. |
| 43 | // DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_); |
| 44 | // } |
| 45 | // void MyMethod() { |
| 46 | // DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_); |
| 47 | // ... (do stuff) ... |
| 48 | // MyOtherMethod(); |
| 49 | // } |
| 50 | // |
| 51 | // void MyOtherMethod() |
| 52 | // VALID_CONTEXT_REQUIRED(my_sequence_checker_) { |
| 53 | // foo_ = 42; |
| 54 | // } |
| 55 | // |
| 56 | // private: |
| 57 | // // GUARDED_BY_CONTEXT() enforces that this member is only |
| 58 | // // accessed from a scope that invokes DCHECK_CALLED_ON_VALID_SEQUENCE() |
| 59 | // // or from a function annotated with VALID_CONTEXT_REQUIRED(). A |
| 60 | // // DCHECK build will not compile if the member is accessed and these |
| 61 | // // conditions are not met. |
| 62 | // int foo_ GUARDED_BY_CONTEXT(my_sequence_checker_); |
| 63 | // |
| 64 | // SEQUENCE_CHECKER(my_sequence_checker_); |
| 65 | // } |
| 66 |
| 67 | #define SEQUENCE_CHECKER_INTERNAL_CONCAT2(a, b) a##b |
| 68 | #define SEQUENCE_CHECKER_INTERNAL_CONCAT(a, b) \ |
| 69 | SEQUENCE_CHECKER_INTERNAL_CONCAT2(a, b) |
| 70 | #define SEQUENCE_CHECKER_INTERNAL_UID(prefix) \ |
| 71 | SEQUENCE_CHECKER_INTERNAL_CONCAT(prefix, __LINE__) |
| 72 |
| 73 | #if DCHECK_IS_ON() |
| 74 | #define SEQUENCE_CHECKER(name) base::SequenceChecker name |
| 75 | #define DCHECK_CALLED_ON_VALID_SEQUENCE(name, ...) \ |
| 76 | base::ScopedValidateSequenceChecker SEQUENCE_CHECKER_INTERNAL_UID( \ |
| 77 | scoped_validate_sequence_checker_)(name, ##__VA_ARGS__); |
| 78 | #define DETACH_FROM_SEQUENCE(name) (name).DetachFromSequence() |
| 79 | #else // DCHECK_IS_ON() |
| 80 | #if __OBJC__ && defined(OS_IOS) && !HAS_FEATURE(objc_cxx_static_assert) |
| 81 | // TODO(thakis): Remove this branch once Xcode's clang has clang r356148. |
| 82 | #define SEQUENCE_CHECKER(name) |
| 83 | #else |
| 84 | #define SEQUENCE_CHECKER(name) static_assert(true, "") |
| 85 | #endif |
| 86 | #define DCHECK_CALLED_ON_VALID_SEQUENCE(name, ...) EAT_STREAM_PARAMETERS |
| 87 | #define DETACH_FROM_SEQUENCE(name) |
| 88 | #endif // DCHECK_IS_ON() |
| 89 |
| 90 | namespace base { |
| 91 |
| 92 | // Do nothing implementation, for use in release mode. |
| 93 | // |
| 94 | // Note: You should almost always use the SequenceChecker class (through the |
| 95 | // above macros) to get the right version for your build configuration. |
| 96 | // Note: This is only a check, not a "lock". It is marked "LOCKABLE" only in |
| 97 | // order to support thread_annotations.h. |
| 98 | class LOCKABLE SequenceCheckerDoNothing { |
| 99 | public: |
| 100 | SequenceCheckerDoNothing() = default; |
| 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; |
| 107 |
| 108 | bool CalledOnValidSequence() const WARN_UNUSED_RESULT { return true; } |
| 109 | void DetachFromSequence() {} |
| 110 |
| 111 | private: |
| 112 | DISALLOW_COPY_AND_ASSIGN(SequenceCheckerDoNothing); |
| 113 | }; |
| 114 |
| 115 | #if DCHECK_IS_ON() |
| 116 | class SequenceChecker : public SequenceCheckerImpl { |
| 117 | }; |
| 118 | #else |
| 119 | class SequenceChecker : public SequenceCheckerDoNothing { |
| 120 | }; |
| 121 | #endif // DCHECK_IS_ON() |
| 122 |
| 123 | class SCOPED_LOCKABLE ScopedValidateSequenceChecker { |
| 124 | public: |
| 125 | explicit ScopedValidateSequenceChecker(const SequenceChecker& checker) |
| 126 | EXCLUSIVE_LOCK_FUNCTION(checker) { |
| 127 | DCHECK(checker.CalledOnValidSequence()); |
| 128 | } |
| 129 |
| 130 | explicit ScopedValidateSequenceChecker(const SequenceChecker& checker, |
| 131 | const StringPiece& msg) |
| 132 | EXCLUSIVE_LOCK_FUNCTION(checker) { |
| 133 | DCHECK(checker.CalledOnValidSequence()) << msg; |
| 134 | } |
| 135 |
| 136 | ~ScopedValidateSequenceChecker() UNLOCK_FUNCTION() {} |
| 137 |
| 138 | private: |
| 139 | }; |
| 140 |
| 141 | } // namespace base |
| 142 |
| 143 | #endif // BASE_SEQUENCE_CHECKER_H_ |
| 144 |