| 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/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/memory/ref_counted.h" |
| 10 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
| 11 #include "base/threading/sequenced_worker_pool.h" | |
| 12 #include "base/threading/thread_checker_impl.h" | 12 #include "base/threading/thread_checker_impl.h" |
| 13 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 | 15 |
| 16 class SequencedTaskRunner; |
| 17 |
| 16 // SequenceCheckerImpl is used to help verify that some methods of a | 18 // SequenceCheckerImpl is used to help verify that some methods of a |
| 17 // class are called in sequence -- that is, called from the same | 19 // class are called in sequence -- that is, called from the same |
| 18 // SequencedTaskRunner. It is a generalization of ThreadChecker; in | 20 // SequencedTaskRunner. It is a generalization of ThreadChecker; in |
| 19 // particular, it behaves exactly like ThreadChecker if constructed | 21 // particular, it behaves exactly like ThreadChecker if its passed a |
| 20 // on a thread that is not part of a SequencedWorkerPool. | 22 // NULL SequencedTaskRunner. |
| 21 class BASE_EXPORT SequenceCheckerImpl { | 23 class BASE_EXPORT SequenceCheckerImpl { |
| 22 public: | 24 public: |
| 23 SequenceCheckerImpl(); | 25 // |sequenced_task_runner| can be NULL. In that case, this object |
| 26 // behaves exactly like a ThreadChecker bound to the current thread, |
| 27 // i.e. CalledOnValidSequence() behaves like CalledOnValidThread(). |
| 28 explicit SequenceCheckerImpl( |
| 29 const scoped_refptr<SequencedTaskRunner>& sequenced_task_runner); |
| 24 ~SequenceCheckerImpl(); | 30 ~SequenceCheckerImpl(); |
| 25 | 31 |
| 26 // Returns whether the we are being called on the same sequence token | 32 // Returns whether the we are being called on the underyling |
| 27 // as previous calls. If there is no associated sequence, then returns | 33 // SequencedTaskRunner. If we're not bound to a |
| 28 // whether we are being called on the underlying ThreadChecker's thread. | 34 // |sequenced_task_runner|, returns whether we are being called on |
| 29 bool CalledOnValidSequencedThread() const; | 35 // the underlying ThreadChecker's thread. |
| 36 bool CalledOnValidSequence() const; |
| 30 | 37 |
| 31 // Unbinds the checker from the currently associated sequence. The | 38 // Changes the underyling SequencedTaskRunner. |
| 32 // checker will be re-bound on the next call to CalledOnValidSequence(). | 39 // |sequenced_task_runner| can be NULL. In that case, this object |
| 33 void DetachFromSequence(); | 40 // behaves exactly like a ThreadChecker that has been detached from |
| 41 // its thread, i.e. we will be bound to the thread on which we next |
| 42 // call CalledOnValidSequence(). |
| 43 void ChangeSequence( |
| 44 const scoped_refptr<SequencedTaskRunner>& sequenced_task_runner); |
| 34 | 45 |
| 35 private: | 46 private: |
| 36 void EnsureSequenceTokenAssigned() const; | |
| 37 | |
| 38 // Guards all variables below. | 47 // Guards all variables below. |
| 39 mutable Lock lock_; | 48 mutable Lock lock_; |
| 40 | 49 scoped_refptr<SequencedTaskRunner> sequenced_task_runner_; |
| 41 // Used if |sequence_token_| is not valid. | 50 // Used if |sequenced_task_runner_| is NULL. |
| 42 ThreadCheckerImpl thread_checker_; | 51 ThreadCheckerImpl thread_checker_; |
| 43 mutable bool sequence_token_assigned_; | |
| 44 | |
| 45 mutable SequencedWorkerPool::SequenceToken sequence_token_; | |
| 46 | 52 |
| 47 DISALLOW_COPY_AND_ASSIGN(SequenceCheckerImpl); | 53 DISALLOW_COPY_AND_ASSIGN(SequenceCheckerImpl); |
| 48 }; | 54 }; |
| 49 | 55 |
| 50 } // namespace base | 56 } // namespace base |
| 51 | 57 |
| 52 #endif // BASE_SEQUENCE_CHECKER_IMPL_H_ | 58 #endif // BASE_SEQUENCE_CHECKER_IMPL_H_ |
| OLD | NEW |