Index: base/task_scheduler/delayed_task_manager.h |
diff --git a/base/task_scheduler/delayed_task_manager.h b/base/task_scheduler/delayed_task_manager.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ef2ff58e3a34c8cfdddeabf2e5db29f0f1e879f2 |
--- /dev/null |
+++ b/base/task_scheduler/delayed_task_manager.h |
@@ -0,0 +1,92 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
+#define BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
+ |
+#include <queue> |
+ |
+#include "base/base_export.h" |
+#include "base/callback.h" |
+#include "base/macros.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/task_scheduler/scheduler_lock.h" |
+#include "base/task_scheduler/sequence.h" |
+#include "base/task_scheduler/task.h" |
+#include "base/time/time.h" |
+ |
+namespace base { |
+namespace task_scheduler { |
fdoray
2016/02/11 17:30:32
Could we have a subtle namespace for things that p
robliao
2016/02/11 21:56:27
Maybe it's the return of the internal namespace.
fdoray
2016/02/12 04:16:19
Done.
|
+ |
+class PriorityQueue; |
+class ShutdownManager; |
+ |
+// Holds delayed tasks until they become ripe for execution. This class is |
+// thread-safe. |
+class BASE_EXPORT DelayedTaskManager { |
+ public: |
+ // |delayed_run_time_changed| is invoked when the next time at which a delayed |
+ // task will become ripe for execution changes. |shutdown_manager| is used to |
+ // handle shutdown behavior of tasks. |
+ DelayedTaskManager(const Closure& delayed_run_time_changed, |
+ ShutdownManager* shutdown_manager); |
+ ~DelayedTaskManager(); |
+ |
+ // Adds |task| to a queue of delayed tasks. The task will be inserted into |
+ // |sequence| and |priority_queue| the first time that PostReadyTasks() is |
+ // called with Now() >= |task.delayed_run_time|. |
+ void AddDelayedTask(const Task& task, |
+ scoped_refptr<Sequence> sequence, |
+ PriorityQueue* priority_queue); |
+ |
+ // Posts delayed tasks that are ripe for execution. |
+ void PostReadyTasks(); |
+ |
+ // Returns the next time at which a delayed task will become ripe for |
+ // execution, or a null TimeTicks if there is no pending delayed tasks. |
+ TimeTicks GetNextDelayedTaskReadyTime() const; |
fdoray
2016/02/11 17:30:32
GetNextDelayedRunTime()
fdoray
2016/02/12 04:16:19
Done.
|
+ |
+ private: |
+ // Returns the current time. Can be overridden for tests. |
+ virtual TimeTicks Now(); |
+ |
+ struct DelayedTask { |
+ DelayedTask(); |
+ ~DelayedTask(); |
+ |
+ Task task; |
+ |
+ // The sequence and priority queue in which the task is inserted once it |
+ // becomes ripe for execution. |
+ scoped_refptr<Sequence> sequence; |
+ PriorityQueue* priority_queue; |
+ |
+ // Ensures that tasks that have the same |delayed_run_time| are popped from |
+ // |delayed_tasks_| in the order in which they were pushed into it. |
+ size_t index; |
+ |
+ bool operator<(const DelayedTask& other) const; |
+ }; |
+ |
+ // Lock protecting |delayed_tasks_| and |next_delayed_task_index_|. |
+ mutable SchedulerLock lock_; |
+ |
+ // Priority queue of delayed tasks. The delayed task with the smallest |
+ // |delayed_run_time| is in front of the priority queue. |
fdoray
2016/02/11 17:30:32
task.delayed_run_time
fdoray
2016/02/12 04:16:19
Done.
|
+ std::priority_queue<DelayedTask> delayed_tasks_; |
+ |
+ // The index to assign to the next delayed task added to |delayed_tasks_|. |
+ size_t next_delayed_task_index_; |
+ |
+ const Closure delayed_run_time_changed_; |
+ |
+ ShutdownManager* const shutdown_manager_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); |
+}; |
+ |
+} // namespace task_scheduler |
+} // namespace base |
+ |
+#endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |