Home | History | Annotate | Download | only in gn
      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/message_loop/message_loop.h"
     11 #include "base/run_loop.h"
     12 #include "base/synchronization/lock.h"
     13 #include "base/threading/sequenced_worker_pool.h"
     14 #include "tools/gn/input_file_manager.h"
     15 
     16 class Target;
     17 
     18 // Maintains the thread pool and error state.
     19 class Scheduler {
     20  public:
     21   Scheduler();
     22   ~Scheduler();
     23 
     24   bool Run();
     25 
     26   base::MessageLoop* main_loop() { return &main_loop_; }
     27   base::SequencedWorkerPool* pool() { return pool_; }
     28 
     29   InputFileManager* input_file_manager() { return input_file_manager_; }
     30 
     31   bool verbose_logging() const { return verbose_logging_; }
     32   void set_verbose_logging(bool v) { verbose_logging_ = v; }
     33 
     34   // TODO(brettw) data race on this access (benign?).
     35   bool is_failed() const { return is_failed_; }
     36 
     37   void Log(const std::string& verb, const std::string& msg);
     38   void FailWithError(const Err& err);
     39 
     40   void ScheduleWork(const base::Closure& work);
     41 
     42   void ScheduleTargetFileWrite(const Target* target);
     43 
     44   // Declares that the given file was read and affected the build output.
     45   //
     46   // TODO(brettw) this is global rather than per-BuildSettings. If we
     47   // start using >1 build settings, then we probably want this to take a
     48   // BuildSettings object so we know the depdency on a per-build basis.
     49   void AddGenDependency(const SourceFile& source_file);
     50   std::vector<SourceFile> GetGenDependencies() const;
     51 
     52   // We maintain a count of the things we need to do that works like a
     53   // refcount. When this reaches 0, the program exits.
     54   void IncrementWorkCount();
     55   void DecrementWorkCount();
     56 
     57  private:
     58   void LogOnMainThread(const std::string& verb, const std::string& msg);
     59   void FailWithErrorOnMainThread(const Err& err);
     60 
     61   void DoTargetFileWrite(const Target* target);
     62 
     63   void DoWork(const base::Closure& closure);
     64 
     65   void OnComplete();
     66 
     67   base::MessageLoop main_loop_;
     68   scoped_refptr<base::SequencedWorkerPool> pool_;
     69 
     70   scoped_refptr<InputFileManager> input_file_manager_;
     71 
     72   base::RunLoop runner_;
     73 
     74   bool verbose_logging_;
     75 
     76   base::AtomicRefCount work_count_;
     77 
     78   mutable base::Lock lock_;
     79   bool is_failed_;
     80 
     81   // Additional input dependencies. Protected by the lock.
     82   std::vector<SourceFile> gen_dependencies_;
     83 
     84   DISALLOW_COPY_AND_ASSIGN(Scheduler);
     85 };
     86 
     87 extern Scheduler* g_scheduler;
     88 
     89 #endif  // TOOLS_GN_SCHEDULER_H_
     90 
     91