Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(407)

Side by Side Diff: base/sequence_checker.h

Issue 2869893003: New Sequence/Thread checking API. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "base/logging.h"
8 #include "base/sequence_checker_impl.h" 9 #include "base/sequence_checker_impl.h"
9 10
11 // SequenceChecker is a helper class used to help verify that some methods of a
12 // class are called sequentially (for thread-safety).
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.)
fdoray 2017/05/10 13:14:05 s/.)/)./ ?
gab 2017/05/10 15:40:08 Done.
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 // void MyMethod() {
33 // DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_);
34 // ... (do stuff) ...
35 // }
danakj 2017/05/09 16:04:28 I suggest adding the destructor checking to the ex
gab 2017/05/10 15:40:08 Done.
36 //
37 // private:
38 // SEQUENCE_CHECKER(my_sequence_checker_);
39 // }
40
41 #if DCHECK_IS_ON()
42 #define SEQUENCE_CHECKER(name) base::SequenceChecker name
43 #define DCHECK_CALLED_ON_VALID_SEQUENCE(name) DCHECK((name).CalledOnValidSequenc e())
44 #define DETACH_FROM_SEQUENCE(name) (name).DetachFromSequence()
45 #else // DCHECK_IS_ON()
46 #define SEQUENCE_CHECKER(name)
47 #define DCHECK_CALLED_ON_VALID_SEQUENCE(name)
48 #define DETACH_FROM_SEQUENCE(name)
49 #endif // DCHECK_IS_ON()
50
10 namespace base { 51 namespace base {
11 52
12 // Do nothing implementation, for use in release mode. 53 // Do nothing implementation, for use in release mode.
13 // 54 //
14 // Note: You should almost always use the SequenceChecker class to get 55 // Note: You should almost always use the SequenceChecker class (through the
15 // the right version for your build configuration. 56 // above macros) to get the right version for your build configuration.
16 class SequenceCheckerDoNothing { 57 class SequenceCheckerDoNothing {
17 public: 58 public:
18 bool CalledOnValidSequence() const { return true; } 59 bool CalledOnValidSequence() const { return true; }
19 60
20 void DetachFromSequence() {} 61 void DetachFromSequence() {}
21 }; 62 };
22 63
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.
28 //
29 // Example:
30 // class MyClass {
31 // public:
32 // void Foo() {
33 // DCHECK(sequence_checker_.CalledOnValidSequence());
34 // ... (do stuff) ...
35 // }
36 //
37 // private:
38 // SequenceChecker sequence_checker_;
39 // }
40 //
41 // In Release mode, CalledOnValidSequence() will always return true.
42 #if DCHECK_IS_ON() 64 #if DCHECK_IS_ON()
43 class SequenceChecker : public SequenceCheckerImpl { 65 class SequenceChecker : public SequenceCheckerImpl {
fdoray 2017/05/10 13:14:05 Suggestion: Add a presubmit warning (or error?) to
fdoray 2017/05/10 13:14:56 Oups... I just saw that you added the PRESUBMIT ch
gab 2017/05/10 15:40:08 What I'll do is remove ThreadChecker and ThreadChe
44 }; 66 };
45 #else 67 #else
46 class SequenceChecker : public SequenceCheckerDoNothing { 68 class SequenceChecker : public SequenceCheckerDoNothing {
47 }; 69 };
48 #endif // DCHECK_IS_ON() 70 #endif // DCHECK_IS_ON()
49 71
50 } // namespace base 72 } // namespace base
51 73
52 #endif // BASE_SEQUENCE_CHECKER_H_ 74 #endif // BASE_SEQUENCE_CHECKER_H_
OLDNEW
« no previous file with comments | « PRESUBMIT.py ('k') | base/sequence_checker_unittest.cc » ('j') | base/threading/thread_checker.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698