akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [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 | |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 8 | #include "base/sequence_checker_impl.h" |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 9 | |
| 10 | namespace base { |
| 11 | |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 12 | // Do nothing implementation, for use in release mode. |
| 13 | // |
| 14 | // Note: You should almost always use the SequenceChecker class to get |
| 15 | // the right version for your build configuration. |
| 16 | class SequenceCheckerDoNothing { |
| 17 | public: |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 18 | bool CalledOnValidSequence() const { return true; } |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 19 | |
tommycli@chromium.org | 623e16c | 2013-07-31 04:26:40 +0900 | [diff] [blame] | 20 | void DetachFromSequence() {} |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 21 | }; |
| 22 | |
fdoray | 4bcf108 | 2016-07-27 07:28:45 +0900 | [diff] [blame] | 23 | // SequenceChecker is a helper class to verify that calls to some methods of a |
| 24 | // class are sequenced. Calls are sequenced when they are issued: |
| 25 | // - From tasks posted to SequencedTaskRunners or SingleThreadTaskRunners bound |
| 26 | // to the same sequence, or, |
| 27 | // - From a single thread outside of any task. |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 28 | // |
| 29 | // Example: |
| 30 | // class MyClass { |
| 31 | // public: |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 32 | // void Foo() { |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 33 | // DCHECK(sequence_checker_.CalledOnValidSequence()); |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 34 | // ... (do stuff) ... |
| 35 | // } |
| 36 | // |
| 37 | // private: |
| 38 | // SequenceChecker sequence_checker_; |
| 39 | // } |
| 40 | // |
fdoray | e4a5827 | 2016-07-29 11:30:16 +0900 | [diff] [blame] | 41 | // In Release mode, CalledOnValidSequence() will always return true. |
gab | fb83c5a | 2016-08-02 05:03:41 +0900 | [diff] [blame] | 42 | #if DCHECK_IS_ON() |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 43 | class SequenceChecker : public SequenceCheckerImpl { |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 44 | }; |
| 45 | #else |
| 46 | class SequenceChecker : public SequenceCheckerDoNothing { |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 47 | }; |
gab | fb83c5a | 2016-08-02 05:03:41 +0900 | [diff] [blame] | 48 | #endif // DCHECK_IS_ON() |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 49 | |
| 50 | } // namespace base |
| 51 | |
| 52 | #endif // BASE_SEQUENCE_CHECKER_H_ |