| 6 |
| 7 | #include "base/logging.h" |
| 8 | #include "base/memory/ptr_util.h" |
| 9 | #include "base/sequence_token.h" |
| 10 | #include "base/threading/thread_checker_impl.h" |
| 11 | #include "base/threading/thread_local_storage.h" |
| 12 |
| 13 | namespace base { |
| 14 |
| 15 | class SequenceCheckerImpl::Core { |
| 16 | public: |
| 17 | Core() : sequence_token_(SequenceToken::GetForCurrentThread()) {} |
| 18 |
| 19 | ~Core() = default; |
| 20 |
| 21 | bool CalledOnValidSequence() const { |
| 22 | // SequenceToken::GetForCurrentThread() accesses thread-local storage. |
| 23 | // During destruction the state of thread-local storage is not guaranteed to |
| 24 | // be in a consistent state. Further, task-runner only installs the |
| 25 | // SequenceToken when running a task. For this reason, |sequence_token_| is |
| 26 | // not checked during thread destruction. |
| 27 | if (!SequenceCheckerImpl::HasThreadLocalStorageBeenDestroyed() && |
| 28 | sequence_token_.IsValid()) { |
| 29 | return sequence_token_ == SequenceToken::GetForCurrentThread(); |
| 30 | } |
| 31 |
| 32 | // SequenceChecker behaves as a ThreadChecker when it is not bound to a |
| 33 | // valid sequence token. |
| 34 | return thread_checker_.CalledOnValidThread(); |
| 35 | } |
| 36 |
| 37 | private: |
| 38 | SequenceToken sequence_token_; |
| 39 |
| 40 | // Used when |sequence_token_| is invalid, or during thread destruction. |
| 41 | ThreadCheckerImpl thread_checker_; |
| 42 | }; |
| 43 |
| 44 | SequenceCheckerImpl::SequenceCheckerImpl() : core_(std::make_unique<Core>()) {} |
| 45 | SequenceCheckerImpl::~SequenceCheckerImpl() = default; |
| 46 |
| 47 | SequenceCheckerImpl::SequenceCheckerImpl(SequenceCheckerImpl&& other) { |
| 48 | // Verify that |other| is called on its associated sequence and bind it now if |
| 49 | // it is currently detached (even if this isn't a DCHECK build). |
| 50 | const bool other_called_on_valid_sequence = other.CalledOnValidSequence(); |
| 51 | DCHECK(other_called_on_valid_sequence); |
| 52 |
| 53 | core_ = std::move(other.core_); |
| 54 | } |
| 55 |
| 56 | SequenceCheckerImpl& SequenceCheckerImpl::operator=( |
| 57 | SequenceCheckerImpl&& other) { |
| 58 | // If |this| is not in a detached state it needs to be bound to the current |
| 59 | // sequence. |
| 60 | DCHECK(CalledOnValidSequence()); |
| 61 |
| 62 | // Verify that |other| is called on its associated sequence and bind it now if |
| 63 | // it is currently detached (even if this isn't a DCHECK build). |
| 64 | const bool other_called_on_valid_sequence = other.CalledOnValidSequence(); |
| 65 | DCHECK(other_called_on_valid_sequence); |
| 66 |
| 67 | // Intentionally not using either |lock_| in this method to let TSAN catch |
| 68 | // racy assign. |
| 69 | TS_UNCHECKED_READ(core_) = std::move(TS_UNCHECKED_READ(other.core_)); |
| 70 |
| 71 | return *this; |
| 72 | } |
| 73 |
| 74 | bool SequenceCheckerImpl::CalledOnValidSequence() const { |
| 75 | AutoLock auto_lock(lock_); |
| 76 | if (!core_) |
| 77 | core_ = std::make_unique<Core>(); |
| 78 | return core_->CalledOnValidSequence(); |
| 79 | } |
| 80 |
| 81 | void SequenceCheckerImpl::DetachFromSequence() { |
| 82 | AutoLock auto_lock(lock_); |
| 83 | core_.reset(); |
| 84 | } |
| 85 |
| 86 | // static |
| 87 | bool SequenceCheckerImpl::HasThreadLocalStorageBeenDestroyed() { |
| 88 | return ThreadLocalStorage::HasBeenDestroyed(); |
| 89 | } |
| 90 |
| 91 | } // namespace base |
| 92 |