[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / tools / gn / scheduler.h
blob912ca7e10ad089ccc3db26395adc3d66187d7f8a
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 "base/atomic_ref_count.h"
9 #include "base/basictypes.h"
10 #include "base/files/file_path.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/synchronization/lock.h"
14 #include "base/threading/sequenced_worker_pool.h"
15 #include "tools/gn/input_file_manager.h"
17 class Target;
19 // Maintains the thread pool and error state.
20 class Scheduler {
21 public:
22 Scheduler();
23 ~Scheduler();
25 bool Run();
27 base::MessageLoop* main_loop() { return &main_loop_; }
28 base::SequencedWorkerPool* pool() { return pool_.get(); }
30 InputFileManager* input_file_manager() { return input_file_manager_.get(); }
32 bool verbose_logging() const { return verbose_logging_; }
33 void set_verbose_logging(bool v) { verbose_logging_ = v; }
35 // TODO(brettw) data race on this access (benign?).
36 bool is_failed() const { return is_failed_; }
38 void Log(const std::string& verb, const std::string& msg);
39 void FailWithError(const Err& err);
41 void ScheduleWork(const base::Closure& work);
43 void Shutdown();
45 // Declares that the given file was read and affected the build output.
47 // TODO(brettw) this is global rather than per-BuildSettings. If we
48 // start using >1 build settings, then we probably want this to take a
49 // BuildSettings object so we know the depdency on a per-build basis.
50 void AddGenDependency(const base::FilePath& file);
51 std::vector<base::FilePath> GetGenDependencies() const;
53 // We maintain a count of the things we need to do that works like a
54 // refcount. When this reaches 0, the program exits.
55 void IncrementWorkCount();
56 void DecrementWorkCount();
58 private:
59 void LogOnMainThread(const std::string& verb, const std::string& msg);
60 void FailWithErrorOnMainThread(const Err& err);
62 void DoTargetFileWrite(const Target* target);
64 void DoWork(const base::Closure& closure);
66 void OnComplete();
68 base::MessageLoop main_loop_;
69 scoped_refptr<base::SequencedWorkerPool> pool_;
71 scoped_refptr<InputFileManager> input_file_manager_;
73 base::RunLoop runner_;
75 bool verbose_logging_;
77 base::AtomicRefCount work_count_;
79 mutable base::Lock lock_;
80 bool is_failed_;
82 // Used to track whether the worker pool has been shutdown. This is necessary
83 // to clean up after tests that make a scheduler but don't run the message
84 // loop.
85 bool has_been_shutdown_;
87 // Additional input dependencies. Protected by the lock.
88 std::vector<base::FilePath> gen_dependencies_;
90 DISALLOW_COPY_AND_ASSIGN(Scheduler);
93 extern Scheduler* g_scheduler;
95 #endif // TOOLS_GN_SCHEDULER_H_