[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 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 | |
| 5 | #ifndef BASE_SEQUENCE_CHECKER_H_ |
| 6 | #define BASE_SEQUENCE_CHECKER_H_ |
| 7 | |
gab | e66aa55 | 2017-05-26 17:20:58 | [diff] [blame] | 8 | #include "base/compiler_specific.h" |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 10 | #include "base/sequence_checker_impl.h" |
Nico Weber | 6065c40 | 2019-03-19 21:56:42 | [diff] [blame] | 11 | #include "build/build_config.h" |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 12 | |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 13 | // SequenceChecker is a helper class used to help verify that some methods of a |
| 14 | // class are called sequentially (for thread-safety). |
| 15 | // |
| 16 | // Use the macros below instead of the SequenceChecker directly so that the |
| 17 | // unused member doesn't result in an extra byte (four when padded) per |
| 18 | // instance in production. |
| 19 | // |
| 20 | // This class is much prefered to ThreadChecker for thread-safety checks. |
| 21 | // ThreadChecker should only be used for classes that are truly thread-affine |
| 22 | // (use thread-local-storage or a third-party API that does). |
| 23 | // |
| 24 | // Usage: |
| 25 | // class MyClass { |
| 26 | // public: |
| 27 | // MyClass() { |
| 28 | // // It's sometimes useful to detach on construction for objects that are |
| 29 | // // constructed in one place and forever after used from another |
| 30 | // // sequence. |
| 31 | // DETACH_FROM_SEQUENCE(my_sequence_checker_); |
| 32 | // } |
| 33 | // |
| 34 | // ~MyClass() { |
| 35 | // // SequenceChecker doesn't automatically check it's destroyed on origin |
| 36 | // // sequence for the same reason it's sometimes detached in the |
| 37 | // // constructor. It's okay to destroy off sequence if the owner |
| 38 | // // otherwise knows usage on the associated sequence is done. If you're |
| 39 | // // not detaching in the constructor, you probably want to explicitly |
| 40 | // // check in the destructor. |
Christian Fremerey | 177b9b93 | 2017-06-02 16:55:02 | [diff] [blame] | 41 | // DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_); |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 42 | // } |
| 43 | // void MyMethod() { |
| 44 | // DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_); |
| 45 | // ... (do stuff) ... |
| 46 | // } |
| 47 | // |
| 48 | // private: |
| 49 | // SEQUENCE_CHECKER(my_sequence_checker_); |
| 50 | // } |
| 51 | |
| 52 | #if DCHECK_IS_ON() |
| 53 | #define SEQUENCE_CHECKER(name) base::SequenceChecker name |
| 54 | #define DCHECK_CALLED_ON_VALID_SEQUENCE(name) \ |
| 55 | DCHECK((name).CalledOnValidSequence()) |
| 56 | #define DETACH_FROM_SEQUENCE(name) (name).DetachFromSequence() |
| 57 | #else // DCHECK_IS_ON() |
Jose Dapena Paz | 59a9367 | 2019-04-08 17:54:41 | [diff] [blame] | 58 | #if __OBJC__ && defined(OS_IOS) && !HAS_FEATURE(objc_cxx_static_assert) |
Nico Weber | 6065c40 | 2019-03-19 21:56:42 | [diff] [blame] | 59 | // TODO(thakis): Remove this branch once Xcode's clang has clang r356148. |
Nico Weber | ea0dd1a | 2019-03-01 17:04:41 | [diff] [blame] | 60 | #define SEQUENCE_CHECKER(name) |
Nico Weber | dcacb33 | 2019-03-09 02:32:02 | [diff] [blame] | 61 | #else |
| 62 | #define SEQUENCE_CHECKER(name) static_assert(true, "") |
| 63 | #endif |
Kevin Marshall | e6572ee | 2017-05-31 01:02:04 | [diff] [blame] | 64 | #define DCHECK_CALLED_ON_VALID_SEQUENCE(name) EAT_STREAM_PARAMETERS |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 65 | #define DETACH_FROM_SEQUENCE(name) |
| 66 | #endif // DCHECK_IS_ON() |
| 67 | |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 68 | namespace base { |
| 69 | |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 70 | // Do nothing implementation, for use in release mode. |
| 71 | // |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 72 | // Note: You should almost always use the SequenceChecker class (through the |
| 73 | // above macros) to get the right version for your build configuration. |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 74 | class SequenceCheckerDoNothing { |
| 75 | public: |
tzik | c342ef57 | 2017-07-21 08:09:50 | [diff] [blame] | 76 | SequenceCheckerDoNothing() = default; |
Gabriel Charette | 9746ffce | 2019-07-30 20:27:17 | [diff] [blame^] | 77 | |
| 78 | // Moving between matching sequences is allowed to help classes with |
| 79 | // SequenceCheckers that want a default move-construct/assign. |
| 80 | SequenceCheckerDoNothing(SequenceCheckerDoNothing&& other) = default; |
| 81 | SequenceCheckerDoNothing& operator=(SequenceCheckerDoNothing&& other) = |
| 82 | default; |
| 83 | |
gab | e66aa55 | 2017-05-26 17:20:58 | [diff] [blame] | 84 | bool CalledOnValidSequence() const WARN_UNUSED_RESULT { return true; } |
[email protected] | d52426c | 2013-07-30 19:26:40 | [diff] [blame] | 85 | void DetachFromSequence() {} |
tzik | c342ef57 | 2017-07-21 08:09:50 | [diff] [blame] | 86 | |
| 87 | private: |
| 88 | DISALLOW_COPY_AND_ASSIGN(SequenceCheckerDoNothing); |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 89 | }; |
| 90 | |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 91 | #if DCHECK_IS_ON() |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 92 | class SequenceChecker : public SequenceCheckerImpl { |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 93 | }; |
| 94 | #else |
| 95 | class SequenceChecker : public SequenceCheckerDoNothing { |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 96 | }; |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 97 | #endif // DCHECK_IS_ON() |
[email protected] | 399ed42 | 2012-12-27 19:58:00 | [diff] [blame] | 98 | |
| 99 | } // namespace base |
| 100 | |
| 101 | #endif // BASE_SEQUENCE_CHECKER_H_ |