sequence_checker.h 5.1 KB

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