#include "base/threading/thread_checker_impl.h"
#include "base/check.h"
#include "base/debug/stack_trace.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread_checker.h"
#include "base/threading/thread_local.h"
namespace {
bool g_log_thread_and_sequence_checker_binding = false;
}
namespace base {
void ThreadCheckerImpl::EnableStackLogging() {
g_log_thread_and_sequence_checker_binding = true;
}
ThreadCheckerImpl::ThreadCheckerImpl() {
AutoLock auto_lock(lock_);
EnsureAssigned();
}
ThreadCheckerImpl::~ThreadCheckerImpl() = default;
ThreadCheckerImpl::ThreadCheckerImpl(ThreadCheckerImpl&& other) {
const bool other_called_on_valid_thread = other.CalledOnValidThread();
DCHECK(other_called_on_valid_thread);
bound_at_ = std::move(other.bound_at_);
thread_id_ = other.thread_id_;
task_token_ = other.task_token_;
sequence_token_ = other.sequence_token_;
other.thread_id_ = PlatformThreadRef();
other.task_token_ = TaskToken();
other.sequence_token_ = SequenceToken();
}
ThreadCheckerImpl& ThreadCheckerImpl::operator=(ThreadCheckerImpl&& other) {
DCHECK(CalledOnValidThread());
const bool other_called_on_valid_thread = other.CalledOnValidThread();
DCHECK(other_called_on_valid_thread);
TS_UNCHECKED_READ(thread_id_) = TS_UNCHECKED_READ(other.thread_id_);
TS_UNCHECKED_READ(task_token_) = TS_UNCHECKED_READ(other.task_token_);
TS_UNCHECKED_READ(sequence_token_) = TS_UNCHECKED_READ(other.sequence_token_);
TS_UNCHECKED_READ(other.thread_id_) = PlatformThreadRef();
TS_UNCHECKED_READ(other.task_token_) = TaskToken();
TS_UNCHECKED_READ(other.sequence_token_) = SequenceToken();
return *this;
}
bool ThreadCheckerImpl::CalledOnValidThread(
std::unique_ptr<debug::StackTrace>* out_bound_at) const {
const bool has_thread_been_destroyed = ThreadLocalStorage::HasBeenDestroyed();
AutoLock auto_lock(lock_);
return CalledOnValidThreadInternal(out_bound_at, has_thread_been_destroyed);
}
bool ThreadCheckerImpl::CalledOnValidThreadInternal(
std::unique_ptr<debug::StackTrace>* out_bound_at,
bool has_thread_been_destroyed) const {
if (!has_thread_been_destroyed) {
EnsureAssigned();
if (task_token_ == TaskToken::GetForCurrentThread())
return true;
if (sequence_token_.IsValid() &&
(sequence_token_ != SequenceToken::GetForCurrentThread() ||
!SingleThreadTaskRunner::HasCurrentDefault())) {
if (out_bound_at && bound_at_) {
*out_bound_at = std::make_unique<debug::StackTrace>(*bound_at_);
}
return false;
}
} else if (thread_id_.is_null()) {
if (g_log_thread_and_sequence_checker_binding)
bound_at_ = std::make_unique<debug::StackTrace>(size_t{10});
thread_id_ = PlatformThread::CurrentRef();
return true;
}
if (thread_id_ != PlatformThread::CurrentRef()) {
if (out_bound_at && bound_at_) {
*out_bound_at = std::make_unique<debug::StackTrace>(*bound_at_);
}
return false;
}
return true;
}
void ThreadCheckerImpl::DetachFromThread() {
AutoLock auto_lock(lock_);
bound_at_ = nullptr;
thread_id_ = PlatformThreadRef();
task_token_ = TaskToken();
sequence_token_ = SequenceToken();
}
std::unique_ptr<debug::StackTrace> ThreadCheckerImpl::GetBoundAt() const {
if (!bound_at_)
return nullptr;
return std::make_unique<debug::StackTrace>(*bound_at_);
}
void ThreadCheckerImpl::EnsureAssigned() const {
if (!thread_id_.is_null())
return;
if (g_log_thread_and_sequence_checker_binding)
bound_at_ = std::make_unique<debug::StackTrace>(size_t{10});
thread_id_ = PlatformThread::CurrentRef();
task_token_ = TaskToken::GetForCurrentThread();
sequence_token_ = SequenceToken::GetForCurrentThread();
}
}