OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/gn/scheduler.h" | 5 #include "tools/gn/scheduler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "tools/gn/ninja_target_writer.h" | |
11 #include "tools/gn/standard_out.h" | 10 #include "tools/gn/standard_out.h" |
12 | 11 |
13 Scheduler* g_scheduler = NULL; | 12 Scheduler* g_scheduler = NULL; |
14 | 13 |
15 namespace { | 14 namespace { |
16 | 15 |
17 int GetThreadCount() { | 16 int GetThreadCount() { |
18 std::string thread_count = | 17 std::string thread_count = |
19 CommandLine::ForCurrentProcess()->GetSwitchValueASCII("threads"); | 18 CommandLine::ForCurrentProcess()->GetSwitchValueASCII("threads"); |
20 | 19 |
21 int result; | 20 int result; |
22 if (thread_count.empty() || !base::StringToInt(thread_count, &result)) | 21 if (thread_count.empty() || !base::StringToInt(thread_count, &result)) |
23 return 32; | 22 return 32; |
24 return result; | 23 return result; |
25 } | 24 } |
26 | 25 |
27 } // namespace | 26 } // namespace |
28 | 27 |
29 Scheduler::Scheduler() | 28 Scheduler::Scheduler() |
30 : pool_(new base::SequencedWorkerPool(GetThreadCount(), "worker_")), | 29 : pool_(new base::SequencedWorkerPool(GetThreadCount(), "worker_")), |
31 input_file_manager_(new InputFileManager), | 30 input_file_manager_(new InputFileManager), |
32 verbose_logging_(false), | 31 verbose_logging_(false), |
33 work_count_(0), | 32 work_count_(0), |
34 is_failed_(false) { | 33 is_failed_(false), |
| 34 has_been_shutdown_(false) { |
35 g_scheduler = this; | 35 g_scheduler = this; |
36 } | 36 } |
37 | 37 |
38 Scheduler::~Scheduler() { | 38 Scheduler::~Scheduler() { |
| 39 if (!has_been_shutdown_) |
| 40 pool_->Shutdown(); |
39 g_scheduler = NULL; | 41 g_scheduler = NULL; |
40 } | 42 } |
41 | 43 |
42 bool Scheduler::Run() { | 44 bool Scheduler::Run() { |
43 runner_.Run(); | 45 runner_.Run(); |
| 46 base::AutoLock lock(lock_); |
44 pool_->Shutdown(); | 47 pool_->Shutdown(); |
| 48 has_been_shutdown_ = true; |
45 return !is_failed(); | 49 return !is_failed(); |
46 } | 50 } |
47 | 51 |
48 void Scheduler::Log(const std::string& verb, const std::string& msg) { | 52 void Scheduler::Log(const std::string& verb, const std::string& msg) { |
49 if (base::MessageLoop::current() == &main_loop_) { | 53 if (base::MessageLoop::current() == &main_loop_) { |
50 LogOnMainThread(verb, msg); | 54 LogOnMainThread(verb, msg); |
51 } else { | 55 } else { |
52 // The run loop always joins on the sub threads, so the lifetime of this | 56 // The run loop always joins on the sub threads, so the lifetime of this |
53 // object outlives the invocations of this function, hence "unretained". | 57 // object outlives the invocations of this function, hence "unretained". |
54 main_loop_.PostTask(FROM_HERE, | 58 main_loop_.PostTask(FROM_HERE, |
(...skipping 24 matching lines...) Expand all Loading... |
79 } | 83 } |
80 | 84 |
81 void Scheduler::ScheduleWork(const base::Closure& work) { | 85 void Scheduler::ScheduleWork(const base::Closure& work) { |
82 IncrementWorkCount(); | 86 IncrementWorkCount(); |
83 pool_->PostWorkerTaskWithShutdownBehavior( | 87 pool_->PostWorkerTaskWithShutdownBehavior( |
84 FROM_HERE, base::Bind(&Scheduler::DoWork, | 88 FROM_HERE, base::Bind(&Scheduler::DoWork, |
85 base::Unretained(this), work), | 89 base::Unretained(this), work), |
86 base::SequencedWorkerPool::BLOCK_SHUTDOWN); | 90 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
87 } | 91 } |
88 | 92 |
89 void Scheduler::ScheduleTargetFileWrite(const Target* target) { | |
90 pool_->PostWorkerTaskWithShutdownBehavior( | |
91 FROM_HERE, base::Bind(&Scheduler::DoTargetFileWrite, | |
92 base::Unretained(this), target), | |
93 base::SequencedWorkerPool::BLOCK_SHUTDOWN); | |
94 } | |
95 | |
96 void Scheduler::AddGenDependency(const base::FilePath& file) { | 93 void Scheduler::AddGenDependency(const base::FilePath& file) { |
97 base::AutoLock lock(lock_); | 94 base::AutoLock lock(lock_); |
98 gen_dependencies_.push_back(file); | 95 gen_dependencies_.push_back(file); |
99 } | 96 } |
100 | 97 |
101 std::vector<base::FilePath> Scheduler::GetGenDependencies() const { | 98 std::vector<base::FilePath> Scheduler::GetGenDependencies() const { |
102 base::AutoLock lock(lock_); | 99 base::AutoLock lock(lock_); |
103 return gen_dependencies_; | 100 return gen_dependencies_; |
104 } | 101 } |
105 | 102 |
(...skipping 17 matching lines...) Expand all Loading... |
123 const std::string& msg) { | 120 const std::string& msg) { |
124 OutputString(verb, DECORATION_YELLOW); | 121 OutputString(verb, DECORATION_YELLOW); |
125 OutputString(" " + msg + "\n"); | 122 OutputString(" " + msg + "\n"); |
126 } | 123 } |
127 | 124 |
128 void Scheduler::FailWithErrorOnMainThread(const Err& err) { | 125 void Scheduler::FailWithErrorOnMainThread(const Err& err) { |
129 err.PrintToStdout(); | 126 err.PrintToStdout(); |
130 runner_.Quit(); | 127 runner_.Quit(); |
131 } | 128 } |
132 | 129 |
133 void Scheduler::DoTargetFileWrite(const Target* target) { | |
134 NinjaTargetWriter::RunAndWriteFile(target); | |
135 } | |
136 | |
137 void Scheduler::DoWork(const base::Closure& closure) { | 130 void Scheduler::DoWork(const base::Closure& closure) { |
138 closure.Run(); | 131 closure.Run(); |
139 DecrementWorkCount(); | 132 DecrementWorkCount(); |
140 } | 133 } |
141 | 134 |
142 void Scheduler::OnComplete() { | 135 void Scheduler::OnComplete() { |
143 // Should be called on the main thread. | 136 // Should be called on the main thread. |
144 DCHECK(base::MessageLoop::current() == main_loop()); | 137 DCHECK(base::MessageLoop::current() == main_loop()); |
145 runner_.Quit(); | 138 runner_.Quit(); |
146 } | 139 } |
OLD | NEW |