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 "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" 16 17 class Target; 18 19 // Maintains the thread pool and error state. 20 class Scheduler { 21 public: 22 Scheduler(); 23 ~Scheduler(); 24 25 bool Run(); 26 27 base::MessageLoop* main_loop() { return &main_loop_; } 28 base::SequencedWorkerPool* pool() { return pool_; } 29 30 InputFileManager* input_file_manager() { return input_file_manager_; } 31 32 bool verbose_logging() const { return verbose_logging_; } 33 void set_verbose_logging(bool v) { verbose_logging_ = v; } 34 35 // TODO(brettw) data race on this access (benign?). 36 bool is_failed() const { return is_failed_; } 37 38 void Log(const std::string& verb, const std::string& msg); 39 void FailWithError(const Err& err); 40 41 void ScheduleWork(const base::Closure& work); 42 43 void Shutdown(); 44 45 // Declares that the given file was read and affected the build output. 46 // 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; 52 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(); 57 58 private: 59 void LogOnMainThread(const std::string& verb, const std::string& msg); 60 void FailWithErrorOnMainThread(const Err& err); 61 62 void DoTargetFileWrite(const Target* target); 63 64 void DoWork(const base::Closure& closure); 65 66 void OnComplete(); 67 68 base::MessageLoop main_loop_; 69 scoped_refptr<base::SequencedWorkerPool> pool_; 70 71 scoped_refptr<InputFileManager> input_file_manager_; 72 73 base::RunLoop runner_; 74 75 bool verbose_logging_; 76 77 base::AtomicRefCount work_count_; 78 79 mutable base::Lock lock_; 80 bool is_failed_; 81 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_; 86 87 // Additional input dependencies. Protected by the lock. 88 std::vector<base::FilePath> gen_dependencies_; 89 90 DISALLOW_COPY_AND_ASSIGN(Scheduler); 91 }; 92 93 extern Scheduler* g_scheduler; 94 95 #endif // TOOLS_GN_SCHEDULER_H_ 96 97