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 namespace base { 8 SequenceCheckerImpl()9SequenceCheckerImpl::SequenceCheckerImpl() 10 : sequence_token_assigned_(false) { 11 AutoLock auto_lock(lock_); 12 EnsureSequenceTokenAssigned(); 13 } 14 ~SequenceCheckerImpl()15SequenceCheckerImpl::~SequenceCheckerImpl() {} 16 CalledOnValidSequencedThread() const17bool SequenceCheckerImpl::CalledOnValidSequencedThread() const { 18 AutoLock auto_lock(lock_); 19 EnsureSequenceTokenAssigned(); 20 21 // If this thread is not associated with a SequencedWorkerPool, 22 // SequenceChecker behaves as a ThreadChecker. See header for details. 23 if (!sequence_token_.IsValid()) 24 return thread_checker_.CalledOnValidThread(); 25 26 return sequence_token_.Equals( 27 SequencedWorkerPool::GetSequenceTokenForCurrentThread()); 28 } 29 DetachFromSequence()30void SequenceCheckerImpl::DetachFromSequence() { 31 AutoLock auto_lock(lock_); 32 thread_checker_.DetachFromThread(); 33 sequence_token_assigned_ = false; 34 sequence_token_ = SequencedWorkerPool::SequenceToken(); 35 } 36 EnsureSequenceTokenAssigned() const37void SequenceCheckerImpl::EnsureSequenceTokenAssigned() const { 38 lock_.AssertAcquired(); 39 if (sequence_token_assigned_) 40 return; 41 42 sequence_token_assigned_ = true; 43 sequence_token_ = SequencedWorkerPool::GetSequenceTokenForCurrentThread(); 44 } 45 46 } // namespace base 47