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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 local_is_failed = is_failed(); | 90 local_is_failed = is_failed(); |
91 has_been_shutdown_ = true; | 91 has_been_shutdown_ = true; |
92 } | 92 } |
93 // Don't do this inside the lock since it will block on the workers, which | 93 // Don't do this inside the lock since it will block on the workers, which |
94 // may be in turn waiting on the lock. | 94 // may be in turn waiting on the lock. |
95 pool_->Shutdown(); | 95 pool_->Shutdown(); |
96 return !local_is_failed; | 96 return !local_is_failed; |
97 } | 97 } |
98 | 98 |
99 void Scheduler::Log(const std::string& verb, const std::string& msg) { | 99 void Scheduler::Log(const std::string& verb, const std::string& msg) { |
100 if (base::MessageLoop::current() == &main_loop_) { | 100 if (task_runner()->BelongsToCurrentThread()) { |
101 LogOnMainThread(verb, msg); | 101 LogOnMainThread(verb, msg); |
102 } else { | 102 } else { |
103 // The run loop always joins on the sub threads, so the lifetime of this | 103 // The run loop always joins on the sub threads, so the lifetime of this |
104 // object outlives the invocations of this function, hence "unretained". | 104 // object outlives the invocations of this function, hence "unretained". |
105 main_loop_.task_runner()->PostTask( | 105 task_runner()->PostTask(FROM_HERE, |
106 FROM_HERE, base::Bind(&Scheduler::LogOnMainThread, | 106 base::Bind(&Scheduler::LogOnMainThread, |
107 base::Unretained(this), verb, msg)); | 107 base::Unretained(this), verb, msg)); |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 void Scheduler::FailWithError(const Err& err) { | 111 void Scheduler::FailWithError(const Err& err) { |
112 DCHECK(err.has_error()); | 112 DCHECK(err.has_error()); |
113 { | 113 { |
114 base::AutoLock lock(lock_); | 114 base::AutoLock lock(lock_); |
115 | 115 |
116 if (is_failed_ || has_been_shutdown_) | 116 if (is_failed_ || has_been_shutdown_) |
117 return; // Ignore errors once we see one. | 117 return; // Ignore errors once we see one. |
118 is_failed_ = true; | 118 is_failed_ = true; |
119 } | 119 } |
120 | 120 |
121 if (base::MessageLoop::current() == &main_loop_) { | 121 if (task_runner()->BelongsToCurrentThread()) { |
122 FailWithErrorOnMainThread(err); | 122 FailWithErrorOnMainThread(err); |
123 } else { | 123 } else { |
124 // The run loop always joins on the sub threads, so the lifetime of this | 124 // The run loop always joins on the sub threads, so the lifetime of this |
125 // object outlives the invocations of this function, hence "unretained". | 125 // object outlives the invocations of this function, hence "unretained". |
126 main_loop_.task_runner()->PostTask( | 126 task_runner()->PostTask(FROM_HERE, |
127 FROM_HERE, base::Bind(&Scheduler::FailWithErrorOnMainThread, | 127 base::Bind(&Scheduler::FailWithErrorOnMainThread, |
128 base::Unretained(this), err)); | 128 base::Unretained(this), err)); |
129 } | 129 } |
130 } | 130 } |
131 | 131 |
132 void Scheduler::ScheduleWork(const base::Closure& work) { | 132 void Scheduler::ScheduleWork(const base::Closure& work) { |
133 IncrementWorkCount(); | 133 IncrementWorkCount(); |
134 pool_->PostWorkerTaskWithShutdownBehavior( | 134 pool_->PostWorkerTaskWithShutdownBehavior( |
135 FROM_HERE, base::Bind(&Scheduler::DoWork, | 135 FROM_HERE, base::Bind(&Scheduler::DoWork, |
136 base::Unretained(this), work), | 136 base::Unretained(this), work), |
137 base::SequencedWorkerPool::BLOCK_SHUTDOWN); | 137 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
138 } | 138 } |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 unknown_generated_inputs_.clear(); | 201 unknown_generated_inputs_.clear(); |
202 written_files_.clear(); | 202 written_files_.clear(); |
203 } | 203 } |
204 | 204 |
205 void Scheduler::IncrementWorkCount() { | 205 void Scheduler::IncrementWorkCount() { |
206 base::AtomicRefCountInc(&work_count_); | 206 base::AtomicRefCountInc(&work_count_); |
207 } | 207 } |
208 | 208 |
209 void Scheduler::DecrementWorkCount() { | 209 void Scheduler::DecrementWorkCount() { |
210 if (!base::AtomicRefCountDec(&work_count_)) { | 210 if (!base::AtomicRefCountDec(&work_count_)) { |
211 if (base::MessageLoop::current() == &main_loop_) { | 211 if (task_runner()->BelongsToCurrentThread()) { |
212 OnComplete(); | 212 OnComplete(); |
213 } else { | 213 } else { |
214 main_loop_.task_runner()->PostTask( | 214 task_runner()->PostTask(FROM_HERE, base::Bind(&Scheduler::OnComplete, |
215 FROM_HERE, | 215 base::Unretained(this))); |
216 base::Bind(&Scheduler::OnComplete, base::Unretained(this))); | |
217 } | 216 } |
218 } | 217 } |
219 } | 218 } |
220 | 219 |
221 void Scheduler::LogOnMainThread(const std::string& verb, | 220 void Scheduler::LogOnMainThread(const std::string& verb, |
222 const std::string& msg) { | 221 const std::string& msg) { |
223 OutputString(verb, DECORATION_YELLOW); | 222 OutputString(verb, DECORATION_YELLOW); |
224 OutputString(" " + msg + "\n"); | 223 OutputString(" " + msg + "\n"); |
225 } | 224 } |
226 | 225 |
227 void Scheduler::FailWithErrorOnMainThread(const Err& err) { | 226 void Scheduler::FailWithErrorOnMainThread(const Err& err) { |
228 err.PrintToStdout(); | 227 err.PrintToStdout(); |
229 runner_.Quit(); | 228 runner_.Quit(); |
230 } | 229 } |
231 | 230 |
232 void Scheduler::DoWork(const base::Closure& closure) { | 231 void Scheduler::DoWork(const base::Closure& closure) { |
233 closure.Run(); | 232 closure.Run(); |
234 DecrementWorkCount(); | 233 DecrementWorkCount(); |
235 } | 234 } |
236 | 235 |
237 void Scheduler::OnComplete() { | 236 void Scheduler::OnComplete() { |
238 // Should be called on the main thread. | 237 // Should be called on the main thread. |
239 DCHECK(base::MessageLoop::current() == main_loop()); | 238 DCHECK(task_runner()->BelongsToCurrentThread()); |
240 runner_.Quit(); | 239 runner_.Quit(); |
241 } | 240 } |
OLD | NEW |