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_IMPL_H_ | 5 #ifndef BASE_SEQUENCE_CHECKER_IMPL_H_ |
6 #define BASE_SEQUENCE_CHECKER_IMPL_H_ | 6 #define BASE_SEQUENCE_CHECKER_IMPL_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
| 9 #include "base/compiler_specific.h" |
9 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/sequence_token.h" |
10 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
11 #include "base/threading/sequenced_worker_pool.h" | 13 #include "base/threading/sequenced_worker_pool.h" |
12 #include "base/threading/thread_checker_impl.h" | 14 #include "base/threading/thread_checker_impl.h" |
13 | 15 |
14 namespace base { | 16 namespace base { |
15 | 17 |
16 // SequenceCheckerImpl is used to help verify that some methods of a | 18 // Real implementation of SequenceChecker for use in debug mode or for temporary |
17 // class are called in sequence -- that is, called from the same | 19 // use in release mode (e.g. to CHECK on a threading issue seen only in the |
18 // SequencedTaskRunner. It is a generalization of ThreadChecker; in | 20 // wild). |
19 // particular, it behaves exactly like ThreadChecker if constructed | 21 // |
20 // on a thread that is not part of a SequencedWorkerPool. | 22 // Note: You should almost always use the SequenceChecker class to get the right |
| 23 // version for your build configuration. |
21 class BASE_EXPORT SequenceCheckerImpl { | 24 class BASE_EXPORT SequenceCheckerImpl { |
22 public: | 25 public: |
23 SequenceCheckerImpl(); | 26 SequenceCheckerImpl(); |
24 ~SequenceCheckerImpl(); | 27 ~SequenceCheckerImpl(); |
25 | 28 |
26 // Returns whether the we are being called on the same sequence token | 29 // Returns true if called in sequence with previous calls to this method and |
27 // as previous calls. If there is no associated sequence, then returns | 30 // the constructor. |
28 // whether we are being called on the underlying ThreadChecker's thread. | 31 bool CalledOnValidSequencedThread() const WARN_UNUSED_RESULT; |
29 bool CalledOnValidSequencedThread() const; | |
30 | 32 |
31 // Unbinds the checker from the currently associated sequence. The | 33 // Unbinds the checker from the currently associated sequence. The checker |
32 // checker will be re-bound on the next call to CalledOnValidSequence(). | 34 // will be re-bound on the next call to CalledOnValidSequencedThread(). |
33 void DetachFromSequence(); | 35 void DetachFromSequence(); |
34 | 36 |
35 private: | 37 private: |
36 void EnsureSequenceTokenAssigned() const; | 38 void EnsureSequenceTokenAssigned() const; |
37 | 39 |
38 // Guards all variables below. | 40 // Guards all variables below. |
39 mutable Lock lock_; | 41 mutable Lock lock_; |
40 | 42 |
41 // Used if |sequence_token_| is not valid. | 43 // True when the SequenceChecker is bound to a sequence or a thread. |
| 44 mutable bool is_assigned_ = false; |
| 45 |
| 46 mutable SequenceToken sequence_token_; |
| 47 |
| 48 // TODO(gab): Remove this when SequencedWorkerPool is deprecated in favor of |
| 49 // TaskScheduler. crbug.com/622400 |
| 50 mutable SequencedWorkerPool::SequenceToken sequenced_worker_pool_token_; |
| 51 |
| 52 // Used when |sequenced_worker_pool_token_| and |sequence_token_| are invalid. |
42 ThreadCheckerImpl thread_checker_; | 53 ThreadCheckerImpl thread_checker_; |
43 mutable bool sequence_token_assigned_; | |
44 | |
45 mutable SequencedWorkerPool::SequenceToken sequence_token_; | |
46 | 54 |
47 DISALLOW_COPY_AND_ASSIGN(SequenceCheckerImpl); | 55 DISALLOW_COPY_AND_ASSIGN(SequenceCheckerImpl); |
48 }; | 56 }; |
49 | 57 |
50 } // namespace base | 58 } // namespace base |
51 | 59 |
52 #endif // BASE_SEQUENCE_CHECKER_IMPL_H_ | 60 #endif // BASE_SEQUENCE_CHECKER_IMPL_H_ |
OLD | NEW |