1 // Copyright (c) 2011 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/threading/thread_checker_impl.h" 6 7 #include "base/threading/thread_task_runner_handle.h" 8 9 namespace base { 10 ThreadCheckerImpl()11ThreadCheckerImpl::ThreadCheckerImpl() { 12 AutoLock auto_lock(lock_); 13 EnsureAssigned(); 14 } 15 16 ThreadCheckerImpl::~ThreadCheckerImpl() = default; 17 CalledOnValidThread() const18bool ThreadCheckerImpl::CalledOnValidThread() const { 19 AutoLock auto_lock(lock_); 20 EnsureAssigned(); 21 22 // Always return true when called from the task from which this 23 // ThreadCheckerImpl was assigned to a thread. 24 if (task_token_ == TaskToken::GetForCurrentThread()) 25 return true; 26 27 // If this ThreadCheckerImpl is bound to a valid SequenceToken, it must be 28 // equal to the current SequenceToken and there must be a registered 29 // ThreadTaskRunnerHandle. Otherwise, the fact that the current task runs on 30 // the thread to which this ThreadCheckerImpl is bound is fortuitous. 31 if (sequence_token_.IsValid() && 32 (sequence_token_ != SequenceToken::GetForCurrentThread() || 33 !ThreadTaskRunnerHandle::IsSet())) { 34 return false; 35 } 36 37 return thread_id_ == PlatformThread::CurrentRef(); 38 } 39 DetachFromThread()40void ThreadCheckerImpl::DetachFromThread() { 41 AutoLock auto_lock(lock_); 42 thread_id_ = PlatformThreadRef(); 43 task_token_ = TaskToken(); 44 sequence_token_ = SequenceToken(); 45 } 46 EnsureAssigned() const47void ThreadCheckerImpl::EnsureAssigned() const { 48 lock_.AssertAcquired(); 49 if (!thread_id_.is_null()) 50 return; 51 52 thread_id_ = PlatformThread::CurrentRef(); 53 task_token_ = TaskToken::GetForCurrentThread(); 54 sequence_token_ = SequenceToken::GetForCurrentThread(); 55 } 56 57 } // namespace base 58