Change next_proto member type.
[chromium-blink-merge.git] / tools / gn / scheduler.cc
blob0f6965420e8416c02336e43c8ac760297c9ef643
1 // Copyright (c) 2013 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.
5 #include "tools/gn/scheduler.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "tools/gn/standard_out.h"
11 #include "tools/gn/switches.h"
13 Scheduler* g_scheduler = NULL;
15 namespace {
17 int GetThreadCount() {
18 std::string thread_count =
19 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(switches::kThreads);
21 int result;
22 if (thread_count.empty() || !base::StringToInt(thread_count, &result))
23 return 32;
24 return result;
27 } // namespace
29 Scheduler::Scheduler()
30 : pool_(new base::SequencedWorkerPool(GetThreadCount(), "worker_")),
31 input_file_manager_(new InputFileManager),
32 verbose_logging_(false),
33 work_count_(0),
34 is_failed_(false),
35 has_been_shutdown_(false) {
36 g_scheduler = this;
39 Scheduler::~Scheduler() {
40 if (!has_been_shutdown_)
41 pool_->Shutdown();
42 g_scheduler = NULL;
45 bool Scheduler::Run() {
46 runner_.Run();
47 bool local_is_failed;
49 base::AutoLock lock(lock_);
50 local_is_failed = is_failed();
51 has_been_shutdown_ = true;
53 // Don't do this inside the lock since it will block on the workers, which
54 // may be in turn waiting on the lock.
55 pool_->Shutdown();
56 return !local_is_failed;
59 void Scheduler::Log(const std::string& verb, const std::string& msg) {
60 if (base::MessageLoop::current() == &main_loop_) {
61 LogOnMainThread(verb, msg);
62 } else {
63 // The run loop always joins on the sub threads, so the lifetime of this
64 // object outlives the invocations of this function, hence "unretained".
65 main_loop_.PostTask(FROM_HERE,
66 base::Bind(&Scheduler::LogOnMainThread,
67 base::Unretained(this), verb, msg));
71 void Scheduler::FailWithError(const Err& err) {
72 DCHECK(err.has_error());
74 base::AutoLock lock(lock_);
76 if (is_failed_ || has_been_shutdown_)
77 return; // Ignore errors once we see one.
78 is_failed_ = true;
81 if (base::MessageLoop::current() == &main_loop_) {
82 FailWithErrorOnMainThread(err);
83 } else {
84 // The run loop always joins on the sub threads, so the lifetime of this
85 // object outlives the invocations of this function, hence "unretained".
86 main_loop_.PostTask(FROM_HERE,
87 base::Bind(&Scheduler::FailWithErrorOnMainThread,
88 base::Unretained(this), err));
92 void Scheduler::ScheduleWork(const base::Closure& work) {
93 IncrementWorkCount();
94 pool_->PostWorkerTaskWithShutdownBehavior(
95 FROM_HERE, base::Bind(&Scheduler::DoWork,
96 base::Unretained(this), work),
97 base::SequencedWorkerPool::BLOCK_SHUTDOWN);
100 void Scheduler::AddGenDependency(const base::FilePath& file) {
101 base::AutoLock lock(lock_);
102 gen_dependencies_.push_back(file);
105 std::vector<base::FilePath> Scheduler::GetGenDependencies() const {
106 base::AutoLock lock(lock_);
107 return gen_dependencies_;
110 void Scheduler::IncrementWorkCount() {
111 base::AtomicRefCountInc(&work_count_);
114 void Scheduler::DecrementWorkCount() {
115 if (!base::AtomicRefCountDec(&work_count_)) {
116 if (base::MessageLoop::current() == &main_loop_) {
117 OnComplete();
118 } else {
119 main_loop_.PostTask(FROM_HERE,
120 base::Bind(&Scheduler::OnComplete,
121 base::Unretained(this)));
126 void Scheduler::LogOnMainThread(const std::string& verb,
127 const std::string& msg) {
128 OutputString(verb, DECORATION_YELLOW);
129 OutputString(" " + msg + "\n");
132 void Scheduler::FailWithErrorOnMainThread(const Err& err) {
133 err.PrintToStdout();
134 runner_.Quit();
137 void Scheduler::DoWork(const base::Closure& closure) {
138 closure.Run();
139 DecrementWorkCount();
142 void Scheduler::OnComplete() {
143 // Should be called on the main thread.
144 DCHECK(base::MessageLoop::current() == main_loop());
145 runner_.Quit();