OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_SEQUENCE_CHECKER_H_ | 5 #ifndef BASE_SEQUENCE_CHECKER_H_ |
6 #define BASE_SEQUENCE_CHECKER_H_ | 6 #define BASE_SEQUENCE_CHECKER_H_ |
7 | 7 |
8 // See comments for the similar block in thread_checker.h. | 8 // See comments for the similar block in thread_checker.h. |
9 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) | 9 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
10 #define ENABLE_SEQUENCE_CHECKER 1 | 10 #define ENABLE_SEQUENCE_CHECKER 1 |
11 #else | 11 #else |
12 #define ENABLE_SEQUENCE_CHECKER 0 | 12 #define ENABLE_SEQUENCE_CHECKER 0 |
13 #endif | 13 #endif |
14 | 14 |
15 #include "base/sequence_checker_impl.h" | 15 #include "base/sequence_checker_impl.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 | 18 |
19 // Do nothing implementation, for use in release mode. | 19 // Do nothing implementation, for use in release mode. |
20 // | 20 // |
21 // Note: You should almost always use the SequenceChecker class to get | 21 // Note: You should almost always use the SequenceChecker class to get |
22 // the right version for your build configuration. | 22 // the right version for your build configuration. |
23 class SequenceCheckerDoNothing { | 23 class SequenceCheckerDoNothing { |
24 public: | 24 public: |
25 bool CalledOnValidSequencedThread() const { | 25 bool CalledOnValidSequence() const { return true; } |
26 return true; | |
27 } | |
28 | 26 |
29 void DetachFromSequence() {} | 27 void DetachFromSequence() {} |
30 }; | 28 }; |
31 | 29 |
32 // SequenceChecker is a helper class to verify that calls to some methods of a | 30 // SequenceChecker is a helper class to verify that calls to some methods of a |
33 // class are sequenced. Calls are sequenced when they are issued: | 31 // class are sequenced. Calls are sequenced when they are issued: |
34 // - From tasks posted to SequencedTaskRunners or SingleThreadTaskRunners bound | 32 // - From tasks posted to SequencedTaskRunners or SingleThreadTaskRunners bound |
35 // to the same sequence, or, | 33 // to the same sequence, or, |
36 // - From a single thread outside of any task. | 34 // - From a single thread outside of any task. |
37 // | 35 // |
38 // Example: | 36 // Example: |
39 // class MyClass { | 37 // class MyClass { |
40 // public: | 38 // public: |
41 // void Foo() { | 39 // void Foo() { |
42 // DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 40 // DCHECK(sequence_checker_.CalledOnValidSequence()); |
43 // ... (do stuff) ... | 41 // ... (do stuff) ... |
44 // } | 42 // } |
45 // | 43 // |
46 // private: | 44 // private: |
47 // SequenceChecker sequence_checker_; | 45 // SequenceChecker sequence_checker_; |
48 // } | 46 // } |
49 // | 47 // |
50 // In Release mode, CalledOnValidSequencedThread() will always return true. | 48 // In Release mode, CalledOnValidSequence() will always return true. |
51 #if ENABLE_SEQUENCE_CHECKER | 49 #if ENABLE_SEQUENCE_CHECKER |
52 class SequenceChecker : public SequenceCheckerImpl { | 50 class SequenceChecker : public SequenceCheckerImpl { |
53 }; | 51 }; |
54 #else | 52 #else |
55 class SequenceChecker : public SequenceCheckerDoNothing { | 53 class SequenceChecker : public SequenceCheckerDoNothing { |
56 }; | 54 }; |
57 #endif // ENABLE_SEQUENCE_CHECKER | 55 #endif // ENABLE_SEQUENCE_CHECKER |
58 | 56 |
59 #undef ENABLE_SEQUENCE_CHECKER | 57 #undef ENABLE_SEQUENCE_CHECKER |
60 | 58 |
61 } // namespace base | 59 } // namespace base |
62 | 60 |
63 #endif // BASE_SEQUENCE_CHECKER_H_ | 61 #endif // BASE_SEQUENCE_CHECKER_H_ |
OLD | NEW |