ApplicationImpl cleanup, part 1:
[chromium-blink-merge.git] / tools / gn / scheduler.h
blob4375f6366fdd0bd9986cb1428a1fddcc1f34aa25
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_
8 #include <map>
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"
19 class Target;
21 // Maintains the thread pool and error state.
22 class Scheduler {
23 public:
24 Scheduler();
25 ~Scheduler();
27 bool Run();
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);
45 void Shutdown();
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);
60 // Unknown generated inputs are files that a target declares as an input
61 // in the output directory, but which aren't generated by any dependency.
63 // Some of these files will be files written by write_file and will be
64 // GenDependencies (see AddWrittenFile above). There are OK and include
65 // things like response files for scripts. Others cases will be ones where
66 // the file is generated by a target that's not a dependency.
68 // In order to distinguish these two cases, the checking for these input
69 // files needs to be done after all targets are complete. This also has the
70 // nice side effect that if a target generates the file we can find it and
71 // tell the user which dependency is missing.
73 // The result returned by GetUnknownGeneratedInputs will not count any files
74 // that were written by write_file during execution.
75 void AddUnknownGeneratedInput(const Target* target, const SourceFile& file);
76 std::multimap<SourceFile, const Target*> GetUnknownGeneratedInputs() const;
77 void ClearUnknownGeneratedInputsAndWrittenFiles(); // For testing.
79 // We maintain a count of the things we need to do that works like a
80 // refcount. When this reaches 0, the program exits.
81 void IncrementWorkCount();
82 void DecrementWorkCount();
84 private:
85 void LogOnMainThread(const std::string& verb, const std::string& msg);
86 void FailWithErrorOnMainThread(const Err& err);
88 void DoTargetFileWrite(const Target* target);
90 void DoWork(const base::Closure& closure);
92 void OnComplete();
94 base::MessageLoop main_loop_;
95 scoped_refptr<base::SequencedWorkerPool> pool_;
97 scoped_refptr<InputFileManager> input_file_manager_;
99 base::RunLoop runner_;
101 bool verbose_logging_;
103 base::AtomicRefCount work_count_;
105 mutable base::Lock lock_;
106 bool is_failed_;
108 // Used to track whether the worker pool has been shutdown. This is necessary
109 // to clean up after tests that make a scheduler but don't run the message
110 // loop.
111 bool has_been_shutdown_;
113 // Protected by the lock. See the corresponding Add/Get functions above.
114 std::vector<base::FilePath> gen_dependencies_;
115 std::vector<SourceFile> written_files_;
116 std::multimap<SourceFile, const Target*> unknown_generated_inputs_;
118 DISALLOW_COPY_AND_ASSIGN(Scheduler);
121 extern Scheduler* g_scheduler;
123 #endif // TOOLS_GN_SCHEDULER_H_