| 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 <memory> |
| 9 | |
| 10 | #include "base/base_export.h" |
| 11 | #include "base/compiler_specific.h" |
| 12 | #include "base/macros.h" |
| 13 | #include "base/synchronization/lock.h" |
| 14 | #include "base/thread_annotations.h" |
| 15 | |
| 16 | namespace base { |
| 17 | |
| 18 | // Real implementation of SequenceChecker for use in debug mode or for temporary |
| 19 | // use in release mode (e.g. to CHECK on a threading issue seen only in the |
| 20 | // wild). |
| 21 | // |
| 22 | // Note: You should almost always use the SequenceChecker class to get the right |
| 23 | // version for your build configuration. |
| 24 | // Note: This is only a check, not a "lock". It is marked "LOCKABLE" only in |
| 25 | // order to support thread_annotations.h. |
| 26 | class LOCKABLE BASE_EXPORT SequenceCheckerImpl { |
| 27 | public: |
| 28 | SequenceCheckerImpl(); |
| 29 | ~SequenceCheckerImpl(); |
| 30 | |
| 31 | // Allow move construct/assign. This must be called on |other|'s associated |
| 32 | // sequence and assignment can only be made into a SequenceCheckerImpl which |
| 33 | // is detached or already associated with the current sequence. This isn't |
| 34 | // thread-safe (|this| and |other| shouldn't be in use while this move is |
| 35 | // performed). If the assignment was legal, the resulting SequenceCheckerImpl |
| 36 | // will be bound to the current sequence and |other| will be detached. |
| 37 | SequenceCheckerImpl(SequenceCheckerImpl&& other); |
| 38 | SequenceCheckerImpl& operator=(SequenceCheckerImpl&& other); |
| 39 | |
| 40 | // Returns true if called in sequence with previous calls to this method and |
| 41 | // the constructor. |
| 42 | bool CalledOnValidSequence() const WARN_UNUSED_RESULT; |
| 43 | |
| 44 | // Unbinds the checker from the currently associated sequence. The checker |
| 45 | // will be re-bound on the next call to CalledOnValidSequence(). |
| 46 | void DetachFromSequence(); |
| 47 | |
| 48 | private: |
| 49 | class Core; |
| 50 | |
| 51 | // Calls straight to ThreadLocalStorage::HasBeenDestroyed(). Exposed purely |
| 52 | // for 'friend' to work. |
| 53 | static bool HasThreadLocalStorageBeenDestroyed(); |
| 54 | |
| 55 | mutable Lock lock_; |
| 56 | mutable std::unique_ptr<Core> core_ GUARDED_BY(lock_); |
| 57 | |
| 58 | DISALLOW_COPY_AND_ASSIGN(SequenceCheckerImpl); |
| 59 | }; |
| 60 | |
| 61 | } // namespace base |
| 62 | |
| 63 | #endif // BASE_SEQUENCE_CHECKER_IMPL_H_ |
| 64 | |