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/input_file_manager.h" | 5 #include "tools/gn/input_file_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "tools/gn/filesystem_utils.h" | 9 #include "tools/gn/filesystem_utils.h" |
10 #include "tools/gn/parser.h" | 10 #include "tools/gn/parser.h" |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 { | 208 { |
209 base::AutoUnlock unlock(lock_); | 209 base::AutoUnlock unlock(lock_); |
210 data->completion_event->Wait(); | 210 data->completion_event->Wait(); |
211 } | 211 } |
212 // If there were multiple waiters on the same event, we now need to wake | 212 // If there were multiple waiters on the same event, we now need to wake |
213 // up the next one. | 213 // up the next one. |
214 data->completion_event->Signal(); | 214 data->completion_event->Signal(); |
215 } | 215 } |
216 } | 216 } |
217 | 217 |
218 // The other load could have failed. In this case that error was probably | 218 // The other load could have failed. It is possible that this thread's error |
219 // printed to the console, but we need to return something here, so make up a | 219 // will be reported to the scheduler before the other thread's (and the first |
220 // dummy error. | 220 // error reported "wins"). Forward the parse error from the other load for |
221 // | 221 // this thread so that the error message is useful. |
222 // There is a race condition. The other load could have failed, but if the | 222 if (!data->parsed_root) |
223 // other thread is delayed for some reason, this thread could end up | 223 *err = data->parse_error; |
224 // reporting the error to the scheduler first (since first error report | |
225 // wins). The user will see this one and the "real" one will be discarded. | |
226 if (!data->parsed_root) { | |
227 *err = Err(origin, "File parse failed.", | |
228 "If you see this, I'm really sorry, but a race condition has caused\n" | |
229 "me to eat your error message. It was crunchy. If the parse error\n" | |
230 "in your imported file isn't obvious, try re-running GN."); | |
231 } | |
232 return data->parsed_root.get(); | 224 return data->parsed_root.get(); |
233 } | 225 } |
234 | 226 |
235 void InputFileManager::AddDynamicInput(const SourceFile& name, | 227 void InputFileManager::AddDynamicInput(const SourceFile& name, |
236 InputFile** file, | 228 InputFile** file, |
237 std::vector<Token>** tokens, | 229 std::vector<Token>** tokens, |
238 scoped_ptr<ParseNode>** parse_root) { | 230 scoped_ptr<ParseNode>** parse_root) { |
239 InputFileData* data = new InputFileData(name); | 231 InputFileData* data = new InputFileData(name); |
240 { | 232 { |
241 base::AutoLock lock(lock_); | 233 base::AutoLock lock(lock_); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 std::vector<FileLoadCallback> callbacks; | 281 std::vector<FileLoadCallback> callbacks; |
290 { | 282 { |
291 base::AutoLock lock(lock_); | 283 base::AutoLock lock(lock_); |
292 DCHECK(input_files_.find(name) != input_files_.end()); | 284 DCHECK(input_files_.find(name) != input_files_.end()); |
293 | 285 |
294 InputFileData* data = input_files_[name]; | 286 InputFileData* data = input_files_[name]; |
295 data->loaded = true; | 287 data->loaded = true; |
296 if (success) { | 288 if (success) { |
297 data->tokens.swap(tokens); | 289 data->tokens.swap(tokens); |
298 data->parsed_root = root.Pass(); | 290 data->parsed_root = root.Pass(); |
| 291 } else { |
| 292 data->parse_error = *err; |
299 } | 293 } |
300 | 294 |
301 // Unblock waiters on this event. | 295 // Unblock waiters on this event. |
302 // | 296 // |
303 // It's somewhat bad to signal this inside the lock. When it's used, it's | 297 // It's somewhat bad to signal this inside the lock. When it's used, it's |
304 // lazily created inside the lock. So we need to do the check and signal | 298 // lazily created inside the lock. So we need to do the check and signal |
305 // inside the lock to avoid race conditions on the lazy creation of the | 299 // inside the lock to avoid race conditions on the lazy creation of the |
306 // lock. | 300 // lock. |
307 // | 301 // |
308 // We could avoid this by creating the lock every time, but the lock is | 302 // We could avoid this by creating the lock every time, but the lock is |
309 // very seldom used and will generally be NULL, so my current theory is that | 303 // very seldom used and will generally be NULL, so my current theory is that |
310 // several signals of a completion event inside a lock is better than | 304 // several signals of a completion event inside a lock is better than |
311 // creating about 1000 extra locks (one for each file). | 305 // creating about 1000 extra locks (one for each file). |
312 if (data->completion_event) | 306 if (data->completion_event) |
313 data->completion_event->Signal(); | 307 data->completion_event->Signal(); |
314 | 308 |
315 callbacks.swap(data->scheduled_callbacks); | 309 callbacks.swap(data->scheduled_callbacks); |
316 } | 310 } |
317 | 311 |
318 // Run pending invocations. Theoretically we could schedule each of these | 312 // Run pending invocations. Theoretically we could schedule each of these |
319 // separately to get some parallelism. But normally there will only be one | 313 // separately to get some parallelism. But normally there will only be one |
320 // item in the list, so that's extra overhead and complexity for no gain. | 314 // item in the list, so that's extra overhead and complexity for no gain. |
321 if (success) { | 315 if (success) { |
322 for (const auto& cb : callbacks) | 316 for (const auto& cb : callbacks) |
323 cb.Run(unowned_root); | 317 cb.Run(unowned_root); |
324 } | 318 } |
325 return success; | 319 return success; |
326 } | 320 } |
OLD | NEW |