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