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.
5 #ifndef BASE_SEQUENCE_CHECKER_H_
6 #define BASE_SEQUENCE_CHECKER_H_
8 #include "base/check.h"
9 #include "base/sequence_checker_impl.h"
10 #include "base/strings/string_piece.h"
11 #include "build/build_config.h"
14 #include "base/debug/stack_trace.h"
17 // SequenceChecker is a helper class used to help verify that some methods of a
18 // class are called sequentially (for thread-safety). It supports thread safety
19 // annotations (see base/thread_annotations.h).
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.
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).
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.
39 // // Detaching on construction is necessary for objects that are
40 // // constructed on one sequence and forever after used from another
42 // DETACH_FROM_SEQUENCE(my_sequence_checker_);
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.
52 // DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_);
55 // DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_);
60 // void MyOtherMethod()
61 // VALID_CONTEXT_REQUIRED(my_sequence_checker_) {
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_);
73 // SEQUENCE_CHECKER(my_sequence_checker_);
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__)
83 #define SEQUENCE_CHECKER(name) base::SequenceChecker name
84 #define DCHECK_CALLED_ON_VALID_SEQUENCE(name, ...) \
85 base::ScopedValidateSequenceChecker SEQUENCE_CHECKER_INTERNAL_UID( \
86 scoped_validate_sequence_checker_)(name, ##__VA_ARGS__)
87 #define DETACH_FROM_SEQUENCE(name) (name).DetachFromSequence()
88 #else // DCHECK_IS_ON()
89 // A no-op expansion that can be followed by a semicolon at class level.
90 #define SEQUENCE_CHECKER(name) static_assert(true, "")
91 #define DCHECK_CALLED_ON_VALID_SEQUENCE(name, ...) EAT_CHECK_STREAM_PARAMS()
92 #define DETACH_FROM_SEQUENCE(name)
93 #endif // DCHECK_IS_ON()
97 // Do nothing implementation, for use in release mode.
99 // Note: You should almost always use the SequenceChecker class (through the
100 // above macros) to get the right version for your build configuration.
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 {
106 static void EnableStackLogging() {}
108 SequenceCheckerDoNothing() = default;
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) =
115 SequenceCheckerDoNothing(const SequenceCheckerDoNothing&) = delete;
116 SequenceCheckerDoNothing& operator=(const SequenceCheckerDoNothing&) = delete;
118 bool CalledOnValidSequence(void* = nullptr) const WARN_UNUSED_RESULT {
121 void DetachFromSequence() {}
125 using SequenceChecker = SequenceCheckerImpl;
127 using SequenceChecker = SequenceCheckerDoNothing;
128 #endif // DCHECK_IS_ON()
131 class SCOPED_LOCKABLE ScopedValidateSequenceChecker {
133 explicit ScopedValidateSequenceChecker(const SequenceChecker& checker)
134 EXCLUSIVE_LOCK_FUNCTION(checker) {
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()
141 explicit ScopedValidateSequenceChecker(const SequenceChecker& checker,
142 const StringPiece& msg)
143 EXCLUSIVE_LOCK_FUNCTION(checker) {
144 std::unique_ptr<debug::StackTrace> bound_at;
145 DCHECK(checker.CalledOnValidSequence(&bound_at))
147 << (bound_at ? "\nWas attached to sequence at:\n" + bound_at->ToString()
151 ~ScopedValidateSequenceChecker() UNLOCK_FUNCTION() {}
159 #endif // BASE_SEQUENCE_CHECKER_H_