• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef TOOLS_GN_SCHEDULER_H_
6 #define TOOLS_GN_SCHEDULER_H_
7 
8 #include <condition_variable>
9 #include <functional>
10 #include <map>
11 #include <mutex>
12 
13 #include "base/atomic_ref_count.h"
14 #include "base/files/file_path.h"
15 #include "gn/input_file_manager.h"
16 #include "gn/label.h"
17 #include "gn/source_file.h"
18 #include "gn/token.h"
19 #include "util/msg_loop.h"
20 #include "util/worker_pool.h"
21 
22 class Target;
23 
24 // Maintains the thread pool and error state.
25 class Scheduler {
26  public:
27   Scheduler();
28   ~Scheduler();
29 
30   bool Run();
31 
task_runner()32   MsgLoop* task_runner() {
33     DCHECK(main_thread_run_loop_);
34     return main_thread_run_loop_;
35   }
36 
input_file_manager()37   InputFileManager* input_file_manager() { return input_file_manager_.get(); }
38 
verbose_logging()39   bool verbose_logging() const { return verbose_logging_; }
set_verbose_logging(bool v)40   void set_verbose_logging(bool v) { verbose_logging_ = v; }
41 
42   // TODO(brettw) data race on this access (benign?).
is_failed()43   bool is_failed() const { return is_failed_; }
44 
45   void Log(const std::string& verb, const std::string& msg);
46   void FailWithError(const Err& err);
47 
48   void ScheduleWork(std::function<void()> work);
49 
50   void Shutdown();
51 
52   // Declares that the given file was read and affected the build output.
53   //
54   // Some consumers expect provided path to be absolute.kk
55   //
56   // TODO(brettw) this is global rather than per-BuildSettings. If we
57   // start using >1 build settings, then we probably want this to take a
58   // BuildSettings object so we know the dependency on a per-build basis.
59   // If moved, most of the Add/Get functions below should move as well.
60   void AddGenDependency(const base::FilePath& file);
61   std::vector<base::FilePath> GetGenDependencies() const;
62 
63   // Tracks calls to write_file for resolving with the unknown generated
64   // inputs (see AddUnknownGeneratedInput below).
65   void AddWrittenFile(const SourceFile& file);
66 
67   // Schedules a file to be written due to a target setting write_runtime_deps.
68   void AddWriteRuntimeDepsTarget(const Target* entry);
69   std::vector<const Target*> GetWriteRuntimeDepsTargets() const;
70   bool IsFileGeneratedByWriteRuntimeDeps(const OutputFile& file) const;
71 
72   // Tracks generated_file calls.
73   void AddGeneratedFile(const SourceFile& entry);
74   bool IsFileGeneratedByTarget(const SourceFile& file) const;
75 
76   // Unknown generated inputs are files that a target declares as an input
77   // in the output directory, but which aren't generated by any dependency.
78   //
79   // Some of these files will be files written by write_file and will be
80   // GenDependencies (see AddWrittenFile above). There are OK and include
81   // things like response files for scripts. Others cases will be ones where
82   // the file is generated by a target that's not a dependency.
83   //
84   // In order to distinguish these two cases, the checking for these input
85   // files needs to be done after all targets are complete. This also has the
86   // nice side effect that if a target generates the file we can find it and
87   // tell the user which dependency is missing.
88   //
89   // The result returned by GetUnknownGeneratedInputs will not count any files
90   // that were written by write_file during execution.
91   void AddUnknownGeneratedInput(const Target* target, const SourceFile& file);
92   std::multimap<SourceFile, const Target*> GetUnknownGeneratedInputs() const;
93   void ClearUnknownGeneratedInputsAndWrittenFiles();  // For testing.
94 
95   // We maintain a count of the things we need to do that works like a
96   // refcount. When this reaches 0, the program exits.
97   void IncrementWorkCount();
98   void DecrementWorkCount();
99 
100   void SuppressOutputForTesting(bool suppress);
101 
102  private:
103   void LogOnMainThread(const std::string& verb, const std::string& msg);
104   void FailWithErrorOnMainThread(const Err& err);
105 
106   void DoTargetFileWrite(const Target* target);
107 
108   void OnComplete();
109 
110   // Waits for tasks scheduled via ScheduleWork() to complete their execution.
111   void WaitForPoolTasks();
112 
113   MsgLoop* main_thread_run_loop_;
114 
115   scoped_refptr<InputFileManager> input_file_manager_;
116 
117   bool verbose_logging_ = false;
118 
119   base::AtomicRefCount work_count_;
120 
121   // Number of tasks scheduled by ScheduleWork() that haven't completed their
122   // execution.
123   base::AtomicRefCount pool_work_count_;
124 
125   // Lock for |pool_work_count_cv_|.
126   std::mutex pool_work_count_lock_;
127 
128   // Condition variable signaled when |pool_work_count_| reaches zero.
129   std::condition_variable pool_work_count_cv_;
130 
131   WorkerPool worker_pool_;
132 
133   mutable std::mutex lock_;
134   bool is_failed_ = false;
135 
136   bool suppress_output_for_testing_ = false;
137 
138   // Used to track whether the worker pool has been shutdown. This is necessary
139   // to clean up after tests that make a scheduler but don't run the message
140   // loop.
141   bool has_been_shutdown_ = false;
142 
143   // Protected by the lock. See the corresponding Add/Get functions above.
144   std::vector<base::FilePath> gen_dependencies_;
145   std::vector<SourceFile> written_files_;
146   std::vector<const Target*> write_runtime_deps_targets_;
147   std::multimap<SourceFile, const Target*> unknown_generated_inputs_;
148   std::map<SourceFile, bool> generated_files_;
149 
150   Scheduler(const Scheduler&) = delete;
151   Scheduler& operator=(const Scheduler&) = delete;
152 };
153 
154 extern Scheduler* g_scheduler;
155 
156 #endif  // TOOLS_GN_SCHEDULER_H_
157