blob: 190e18ffc68557c64f81f5b766a952c37d3c34a7 [file] [log] [blame]
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/threading/thread_task_runner_handle.h"
#include <utility>
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/threading/thread_local.h"
namespace base {
namespace {
base::LazyInstance<base::ThreadLocalPointer<ThreadTaskRunnerHandle>>::Leaky
lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER;
} // namespace
// static
scoped_refptr<SingleThreadTaskRunner> ThreadTaskRunnerHandle::Get() {
ThreadTaskRunnerHandle* current = lazy_tls_ptr.Pointer()->Get();
DCHECK(current);
return current->task_runner_;
}
// static
bool ThreadTaskRunnerHandle::IsSet() {
return !!lazy_tls_ptr.Pointer()->Get();
}
ThreadTaskRunnerHandle::ThreadTaskRunnerHandle(
scoped_refptr<SingleThreadTaskRunner> task_runner)
: task_runner_(std::move(task_runner)) {
DCHECK(task_runner_->BelongsToCurrentThread());
// No SequencedTaskRunnerHandle (which includes ThreadTaskRunnerHandles)
// should already be set for this thread.
DCHECK(!SequencedTaskRunnerHandle::IsSet());
lazy_tls_ptr.Pointer()->Set(this);
}
ThreadTaskRunnerHandle::~ThreadTaskRunnerHandle() {
DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK_EQ(lazy_tls_ptr.Pointer()->Get(), this);
lazy_tls_ptr.Pointer()->Set(nullptr);
}
} // namespace base