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_IMPL_H_ |
| 6 | #define BASE_SEQUENCE_CHECKER_IMPL_H_ |
| 7 | |
| 8 | #include "base/base_export.h" |
| 9 | #include "base/basictypes.h" |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 10 | #include "base/synchronization/lock.h" |
tommycli@chromium.org | 623e16c | 2013-07-31 04:26:40 +0900 | [diff] [blame] | 11 | #include "base/threading/sequenced_worker_pool.h" |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 12 | #include "base/threading/thread_checker_impl.h" |
| 13 | |
| 14 | namespace base { |
| 15 | |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 16 | // SequenceCheckerImpl is used to help verify that some methods of a |
| 17 | // class are called in sequence -- that is, called from the same |
| 18 | // SequencedTaskRunner. It is a generalization of ThreadChecker; in |
tommycli@chromium.org | 623e16c | 2013-07-31 04:26:40 +0900 | [diff] [blame] | 19 | // particular, it behaves exactly like ThreadChecker if constructed |
| 20 | // on a thread that is not part of a SequencedWorkerPool. |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 21 | class BASE_EXPORT SequenceCheckerImpl { |
| 22 | public: |
tommycli@chromium.org | 623e16c | 2013-07-31 04:26:40 +0900 | [diff] [blame] | 23 | SequenceCheckerImpl(); |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 24 | ~SequenceCheckerImpl(); |
| 25 | |
tommycli@chromium.org | 623e16c | 2013-07-31 04:26:40 +0900 | [diff] [blame] | 26 | // Returns whether the we are being called on the same sequence token |
| 27 | // as previous calls. If there is no associated sequence, then returns |
| 28 | // whether we are being called on the underlying ThreadChecker's thread. |
| 29 | bool CalledOnValidSequencedThread() const; |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 30 | |
tommycli@chromium.org | 623e16c | 2013-07-31 04:26:40 +0900 | [diff] [blame] | 31 | // Unbinds the checker from the currently associated sequence. The |
| 32 | // checker will be re-bound on the next call to CalledOnValidSequence(). |
| 33 | void DetachFromSequence(); |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 34 | |
| 35 | private: |
tommycli@chromium.org | 623e16c | 2013-07-31 04:26:40 +0900 | [diff] [blame] | 36 | void EnsureSequenceTokenAssigned() const; |
| 37 | |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 38 | // Guards all variables below. |
| 39 | mutable Lock lock_; |
tommycli@chromium.org | 623e16c | 2013-07-31 04:26:40 +0900 | [diff] [blame] | 40 | |
| 41 | // Used if |sequence_token_| is not valid. |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 42 | ThreadCheckerImpl thread_checker_; |
tommycli@chromium.org | 623e16c | 2013-07-31 04:26:40 +0900 | [diff] [blame] | 43 | mutable bool sequence_token_assigned_; |
| 44 | |
| 45 | mutable SequencedWorkerPool::SequenceToken sequence_token_; |
akalin@chromium.org | 4fb2deb | 2012-12-28 04:58:00 +0900 | [diff] [blame] | 46 | |
| 47 | DISALLOW_COPY_AND_ASSIGN(SequenceCheckerImpl); |
| 48 | }; |
| 49 | |
| 50 | } // namespace base |
| 51 | |
| 52 | #endif // BASE_SEQUENCE_CHECKER_IMPL_H_ |