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 #include "base/sequence_checker_impl.h" 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 12 namespace base { 13 14 class SequenceCheckerImpl::Core { 15 public: Core()16 Core() : sequence_token_(SequenceToken::GetForCurrentThread()) {} 17 18 ~Core() = default; 19 CalledOnValidSequence() const20 bool CalledOnValidSequence() const { 21 if (sequence_token_.IsValid()) 22 return sequence_token_ == SequenceToken::GetForCurrentThread(); 23 24 // SequenceChecker behaves as a ThreadChecker when it is not bound to a 25 // valid sequence token. 26 return thread_checker_.CalledOnValidThread(); 27 } 28 29 private: 30 SequenceToken sequence_token_; 31 32 // Used when |sequence_token_| is invalid. 33 ThreadCheckerImpl thread_checker_; 34 }; 35 SequenceCheckerImpl()36SequenceCheckerImpl::SequenceCheckerImpl() : core_(std::make_unique<Core>()) {} 37 SequenceCheckerImpl::~SequenceCheckerImpl() = default; 38 CalledOnValidSequence() const39bool SequenceCheckerImpl::CalledOnValidSequence() const { 40 AutoLock auto_lock(lock_); 41 if (!core_) 42 core_ = std::make_unique<Core>(); 43 return core_->CalledOnValidSequence(); 44 } 45 DetachFromSequence()46void SequenceCheckerImpl::DetachFromSequence() { 47 AutoLock auto_lock(lock_); 48 core_.reset(); 49 } 50 51 } // namespace base 52