Commit f0544fc6 authored by fdoray's avatar fdoray Committed by Commit bot

Remove calls to MessageLoop::current() in tools.

Why?
The fact that there's a MessageLoop on the thread is an
unnecessary implementation detail. When browser threads
are migrated to base/task_scheduler, tasks will no longer
have access to a MessageLoop.

These changes were generated manually.

BUG=616447

Review-Url: https://codereview.chromium.org/2130443002
Cr-Commit-Position: refs/heads/master@{#406006}
parent 9a08b8c6
......@@ -6,8 +6,7 @@
#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "tools/gn/build_settings.h"
#include "tools/gn/err.h"
#include "tools/gn/filesystem_utils.h"
......@@ -100,9 +99,11 @@ SourceFile Loader::BuildFileForLabel(const Label& label) {
// -----------------------------------------------------------------------------
LoaderImpl::LoaderImpl(const BuildSettings* build_settings)
: main_loop_(base::MessageLoop::current()),
pending_loads_(0),
build_settings_(build_settings) {
: pending_loads_(0), build_settings_(build_settings) {
// There may not be an active TaskRunner at this point. When that's the case,
// the calling code is expected to call set_task_runner().
if (base::ThreadTaskRunnerHandle::IsSet())
task_runner_ = base::ThreadTaskRunnerHandle::Get();
}
LoaderImpl::~LoaderImpl() {
......@@ -239,7 +240,7 @@ void LoaderImpl::BackgroundLoadFile(const Settings* settings,
const LocationRange& origin,
const ParseNode* root) {
if (!root) {
main_loop_->task_runner()->PostTask(
task_runner_->PostTask(
FROM_HERE, base::Bind(&LoaderImpl::DecrementPendingLoads, this));
return;
}
......@@ -280,8 +281,7 @@ void LoaderImpl::BackgroundLoadFile(const Settings* settings,
trace.Done();
main_loop_->task_runner()->PostTask(
FROM_HERE, base::Bind(&LoaderImpl::DidLoadFile, this));
task_runner_->PostTask(FROM_HERE, base::Bind(&LoaderImpl::DidLoadFile, this));
}
void LoaderImpl::BackgroundLoadBuildConfig(
......@@ -289,7 +289,7 @@ void LoaderImpl::BackgroundLoadBuildConfig(
const Scope::KeyValueMap& toolchain_overrides,
const ParseNode* root) {
if (!root) {
main_loop_->task_runner()->PostTask(
task_runner_->PostTask(
FROM_HERE, base::Bind(&LoaderImpl::DecrementPendingLoads, this));
return;
}
......@@ -339,9 +339,9 @@ void LoaderImpl::BackgroundLoadBuildConfig(
}
}
main_loop_->task_runner()->PostTask(
FROM_HERE, base::Bind(&LoaderImpl::DidLoadBuildConfig, this,
settings->toolchain_label()));
task_runner_->PostTask(FROM_HERE,
base::Bind(&LoaderImpl::DidLoadBuildConfig, this,
settings->toolchain_label()));
}
void LoaderImpl::DidLoadFile() {
......
......@@ -11,13 +11,10 @@
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "tools/gn/label.h"
#include "tools/gn/scope.h"
namespace base {
class MessageLoop;
}
class BuildSettings;
class LocationRange;
class Settings;
......@@ -90,10 +87,13 @@ class LoaderImpl : public Loader {
Label GetDefaultToolchain() const override;
const Settings* GetToolchainSettings(const Label& label) const override;
// Sets the message loop corresponding to the main thread. By default this
// Sets the task runner corresponding to the main thread. By default this
// class will use the thread active during construction, but there is not
// a message loop active during construction all the time.
void set_main_loop(base::MessageLoop* loop) { main_loop_ = loop; }
// a task runner active during construction all the time.
void set_task_runner(
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
task_runner_ = task_runner;
}
// The complete callback is called whenever there are no more pending loads.
// Called on the main thread only. This may be called more than once if the
......@@ -159,7 +159,7 @@ class LoaderImpl : public Loader {
const base::Callback<void(const ParseNode*)>& callback,
Err* err);
base::MessageLoop* main_loop_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
int pending_loads_;
base::Closure complete_callback_;
......
......@@ -7,7 +7,6 @@
#include <vector>
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "tools/gn/build_settings.h"
......
......@@ -97,14 +97,14 @@ bool Scheduler::Run() {
}
void Scheduler::Log(const std::string& verb, const std::string& msg) {
if (base::MessageLoop::current() == &main_loop_) {
if (task_runner()->BelongsToCurrentThread()) {
LogOnMainThread(verb, msg);
} else {
// The run loop always joins on the sub threads, so the lifetime of this
// object outlives the invocations of this function, hence "unretained".
main_loop_.task_runner()->PostTask(
FROM_HERE, base::Bind(&Scheduler::LogOnMainThread,
base::Unretained(this), verb, msg));
task_runner()->PostTask(FROM_HERE,
base::Bind(&Scheduler::LogOnMainThread,
base::Unretained(this), verb, msg));
}
}
......@@ -118,14 +118,14 @@ void Scheduler::FailWithError(const Err& err) {
is_failed_ = true;
}
if (base::MessageLoop::current() == &main_loop_) {
if (task_runner()->BelongsToCurrentThread()) {
FailWithErrorOnMainThread(err);
} else {
// The run loop always joins on the sub threads, so the lifetime of this
// object outlives the invocations of this function, hence "unretained".
main_loop_.task_runner()->PostTask(
FROM_HERE, base::Bind(&Scheduler::FailWithErrorOnMainThread,
base::Unretained(this), err));
task_runner()->PostTask(FROM_HERE,
base::Bind(&Scheduler::FailWithErrorOnMainThread,
base::Unretained(this), err));
}
}
......@@ -208,12 +208,11 @@ void Scheduler::IncrementWorkCount() {
void Scheduler::DecrementWorkCount() {
if (!base::AtomicRefCountDec(&work_count_)) {
if (base::MessageLoop::current() == &main_loop_) {
if (task_runner()->BelongsToCurrentThread()) {
OnComplete();
} else {
main_loop_.task_runner()->PostTask(
FROM_HERE,
base::Bind(&Scheduler::OnComplete, base::Unretained(this)));
task_runner()->PostTask(FROM_HERE, base::Bind(&Scheduler::OnComplete,
base::Unretained(this)));
}
}
}
......@@ -236,6 +235,6 @@ void Scheduler::DoWork(const base::Closure& closure) {
void Scheduler::OnComplete() {
// Should be called on the main thread.
DCHECK(base::MessageLoop::current() == main_loop());
DCHECK(task_runner()->BelongsToCurrentThread());
runner_.Quit();
}
......@@ -12,6 +12,7 @@
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/lock.h"
#include "base/threading/sequenced_worker_pool.h"
#include "tools/gn/input_file_manager.h"
......@@ -29,7 +30,9 @@ class Scheduler {
bool Run();
base::MessageLoop* main_loop() { return &main_loop_; }
scoped_refptr<base::SingleThreadTaskRunner> task_runner() {
return main_loop_.task_runner();
}
base::SequencedWorkerPool* pool() { return pool_.get(); }
InputFileManager* input_file_manager() { return input_file_manager_.get(); }
......
......@@ -13,6 +13,7 @@
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/process/launch.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_split.h"
......@@ -139,13 +140,13 @@ base::FilePath FindDotFile(const base::FilePath& current_dir) {
}
// Called on any thread. Post the item to the builder on the main thread.
void ItemDefinedCallback(base::MessageLoop* main_loop,
scoped_refptr<Builder> builder,
std::unique_ptr<Item> item) {
void ItemDefinedCallback(
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
scoped_refptr<Builder> builder,
std::unique_ptr<Item> item) {
DCHECK(item);
main_loop->task_runner()->PostTask(
FROM_HERE,
base::Bind(&Builder::ItemDefined, builder, base::Passed(&item)));
task_runner->PostTask(FROM_HERE, base::Bind(&Builder::ItemDefined, builder,
base::Passed(&item)));
}
void DecrementWorkCount() {
......@@ -258,12 +259,12 @@ Setup::Setup()
fill_arguments_(true) {
dotfile_settings_.set_toolchain_label(Label());
build_settings_.set_item_defined_callback(
base::Bind(&ItemDefinedCallback, scheduler_.main_loop(), builder_));
base::Bind(&ItemDefinedCallback, scheduler_.task_runner(), builder_));
loader_->set_complete_callback(base::Bind(&DecrementWorkCount));
// The scheduler's main loop wasn't created when the Loader was created, so
// The scheduler's task runner wasn't created when the Loader was created, so
// we need to set it now.
loader_->set_main_loop(scheduler_.main_loop());
loader_->set_task_runner(scheduler_.task_runner());
}
Setup::~Setup() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment