[go: nahoru, domu]

cc: Implement states for Task for stricter control.

Implement states for task which gives stricter control for managing
states of a task in single variable vs. multiple variables. The states
are as given below.

  NEW, SCHEDULED, RUNNING, FINISHED, CANCELED

As TaskGraphWorkQueue manages life cycle of task, this patch also moves
state management to TaskGraphWorkQueue from all TaskGraphRunners, i.e.
WillRun() and DidRun() are moved to places where |work_queue_| inserts
task in |running_tasks| and |completed_tasks_| respectively. This
also ensures that task if gets scheduled must end in either FINISHED or
CANCELED state and will help catch any bugs which go out of expected
behavior for task state.

BUG=599863
CQ_INCLUDE_TRYBOTS=tryserver.blink:linux_blink_rel

Review URL: https://codereview.chromium.org/1903733003

Cr-Commit-Position: refs/heads/master@{#389538}
diff --git a/cc/raster/task.h b/cc/raster/task.h
index 96959c1..54bcf8b6 100644
--- a/cc/raster/task.h
+++ b/cc/raster/task.h
@@ -13,6 +13,41 @@
 #include "cc/base/cc_export.h"
 
 namespace cc {
+class Task;
+
+// States to manage life cycle of a task. Task gets created with NEW state and
+// concludes either in FINISHED or CANCELLED state. So possible life cycle
+// paths for task are -
+//   NEW -> SCHEDULED -> RUNNING -> FINISHED
+//   NEW -> SCHEDULED -> CANCELED
+class CC_EXPORT TaskState {
+ public:
+  bool IsScheduled() const;
+  bool IsRunning() const;
+  bool IsFinished() const;
+  bool IsCanceled() const;
+
+  // Functions to change the state of task. These functions should be called
+  // only from TaskGraphWorkQueue where the life cycle of a task is decided or
+  // from tests. These functions are not thread-safe. Caller is responsible for
+  // thread safety.
+  void Reset();  // Sets state to NEW.
+  void DidSchedule();
+  void DidStart();
+  void DidFinish();
+  void DidCancel();
+
+ private:
+  friend class Task;
+
+  // Let only Task class create the TaskState.
+  TaskState();
+  ~TaskState();
+
+  enum class Value : uint16_t { NEW, SCHEDULED, RUNNING, FINISHED, CANCELED };
+
+  Value value_;
+};
 
 // A task which can be run by a TaskGraphRunner. To run a Task, it should be
 // inserted into a TaskGraph, which can then be scheduled on the
@@ -21,23 +56,21 @@
  public:
   typedef std::vector<scoped_refptr<Task>> Vector;
 
+  TaskState& state() { return state_; }
+
   // Subclasses should implement this method. RunOnWorkerThread may be called
   // on any thread, and subclasses are responsible for locking and thread
   // safety.
   virtual void RunOnWorkerThread() = 0;
 
-  void WillRun();
-  void DidRun();
-  bool HasFinishedRunning() const;
-
  protected:
   friend class base::RefCountedThreadSafe<Task>;
 
   Task();
   virtual ~Task();
 
-  bool will_run_;
-  bool did_run_;
+ private:
+  TaskState state_;
 };
 
 // A task dependency graph describes the order in which to execute a set