sequence_checker.h 5.0 KB

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