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 #ifndef TOOLS_GN_SCHEDULER_H_
6 #define TOOLS_GN_SCHEDULER_H_
10 #include "base/atomic_ref_count.h"
11 #include "base/basictypes.h"
12 #include "base/files/file_path.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h"
15 #include "base/synchronization/lock.h"
16 #include "base/threading/sequenced_worker_pool.h"
17 #include "tools/gn/input_file_manager.h"
21 // Maintains the thread pool and error state.
29 base::MessageLoop
* main_loop() { return &main_loop_
; }
30 base::SequencedWorkerPool
* pool() { return pool_
.get(); }
32 InputFileManager
* input_file_manager() { return input_file_manager_
.get(); }
34 bool verbose_logging() const { return verbose_logging_
; }
35 void set_verbose_logging(bool v
) { verbose_logging_
= v
; }
37 // TODO(brettw) data race on this access (benign?).
38 bool is_failed() const { return is_failed_
; }
40 void Log(const std::string
& verb
, const std::string
& msg
);
41 void FailWithError(const Err
& err
);
43 void ScheduleWork(const base::Closure
& work
);
47 // Declares that the given file was read and affected the build output.
49 // TODO(brettw) this is global rather than per-BuildSettings. If we
50 // start using >1 build settings, then we probably want this to take a
51 // BuildSettings object so we know the depdency on a per-build basis.
52 // If moved, most of the Add/Get functions below should move as well.
53 void AddGenDependency(const base::FilePath
& file
);
54 std::vector
<base::FilePath
> GetGenDependencies() const;
56 // Tracks calls to write_file for resolving with the unknown generated
57 // inputs (see AddUnknownGeneratedInput below).
58 void AddWrittenFile(const SourceFile
& file
);
59 std::vector
<SourceFile
> GetWrittenFiles() const;
61 // Unknown generated inputs are files that a target declares as an input
62 // in the output directory, but which aren't generated by any dependency.
64 // Some of these files will be files written by write_file and will be
65 // GenDependencies (see AddWrittenFile above). There are OK and include
66 // things like response files for scripts. Others cases will be ones where
67 // the file is generated by a target that's not a dependency.
69 // In order to distinguish these two cases, the checking for these input
70 // files needs to be done after all targets are complete. This also has the
71 // nice side effect that if a target generates the file we can find it and
72 // tell the user which dependency is missing.
74 // The result returned by GetUnknownGeneratedInputs will not count any files
75 // that were written by write_file during execution.
76 void AddUnknownGeneratedInput(const Target
* target
, const SourceFile
& file
);
77 std::multimap
<SourceFile
, const Target
*> GetUnknownGeneratedInputs() const;
78 void ClearUnknownGeneratedInputsAndWrittenFiles(); // For testing.
80 // We maintain a count of the things we need to do that works like a
81 // refcount. When this reaches 0, the program exits.
82 void IncrementWorkCount();
83 void DecrementWorkCount();
86 void LogOnMainThread(const std::string
& verb
, const std::string
& msg
);
87 void FailWithErrorOnMainThread(const Err
& err
);
89 void DoTargetFileWrite(const Target
* target
);
91 void DoWork(const base::Closure
& closure
);
95 base::MessageLoop main_loop_
;
96 scoped_refptr
<base::SequencedWorkerPool
> pool_
;
98 scoped_refptr
<InputFileManager
> input_file_manager_
;
100 base::RunLoop runner_
;
102 bool verbose_logging_
;
104 base::AtomicRefCount work_count_
;
106 mutable base::Lock lock_
;
109 // Used to track whether the worker pool has been shutdown. This is necessary
110 // to clean up after tests that make a scheduler but don't run the message
112 bool has_been_shutdown_
;
114 // Protected by the lock. See the corresponding Add/Get functions above.
115 std::vector
<base::FilePath
> gen_dependencies_
;
116 std::vector
<SourceFile
> written_files_
;
117 std::multimap
<SourceFile
, const Target
*> unknown_generated_inputs_
;
119 DISALLOW_COPY_AND_ASSIGN(Scheduler
);
122 extern Scheduler
* g_scheduler
;
124 #endif // TOOLS_GN_SCHEDULER_H_