Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1075)

Unified Diff: base/task_scheduler/delayed_task_manager.h

Issue 1704113002: TaskScheduler [6] SchedulerWorkerThread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_4_shutdown
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..ba51b6c4d3689f3396ad812efe03b7a65a70c065
--- /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 internal {
+
+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 delayed run time
+ // changes as a result of adding a new delayed task to the manager.
+ // |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 GetNextDelayedRunTime() const;
+
+ 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
+ // |task.delayed_run_time| is in front of the priority queue.
+ 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 internal
+} // namespace base
+
+#endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_

Powered by Google App Engine
This is Rietveld 408576698