Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | |
| 6 #define BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <queue> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/base_export.h" | |
| 15 #include "base/callback.h" | |
| 16 #include "base/macros.h" | |
| 17 #include "base/task_scheduler/scheduler_lock.h" | |
| 18 #include "base/task_scheduler/task.h" | |
| 19 #include "base/time/time.h" | |
| 20 | |
| 21 namespace base { | |
| 22 namespace internal { | |
| 23 | |
| 24 // A DelayedTaskManager holds delayed tasks until they become ripe for | |
| 25 // execution. When they become ripe for execution, it invokes a custom | |
| 26 // callback. This class is thread-safe. | |
| 27 class BASE_EXPORT DelayedTaskManager { | |
| 28 public: | |
| 29 using PostTaskCallback = Callback<void(std::unique_ptr<Task>)>; | |
| 30 | |
| 31 // |delayed_run_time_changed| is invoked when the next delayed run time | |
| 32 // changes as a result of adding a new delayed task to the manager. | |
| 33 explicit DelayedTaskManager(const Closure& delayed_run_time_changed); | |
| 34 ~DelayedTaskManager(); | |
| 35 | |
| 36 // Adds |task| to a queue of delayed tasks. |post_task_callback| will be | |
| 37 // invoked the first time that PostReadyTasks() is called with the current | |
| 38 // time being greater or equal to |task->delayed_run_time|. | |
| 39 void AddDelayedTask(std::unique_ptr<Task> task, | |
| 40 const PostTaskCallback& post_task_callback); | |
| 41 | |
| 42 // Posts delayed tasks that are ripe for execution. | |
| 43 void PostReadyTasks(); | |
| 44 | |
| 45 // Returns the next time at which a delayed task will become ripe for | |
| 46 // execution, or a null TimeTicks if there is no pending delayed tasks. | |
| 47 TimeTicks GetNextDelayedRunTime() const; | |
| 48 | |
| 49 private: | |
| 50 // Returns the current time. Can be overridden for tests. | |
| 51 virtual TimeTicks Now(); | |
|
robliao
2016/04/04 17:59:53
Nit: This can be const.
fdoray
2016/04/04 19:52:19
Done.
| |
| 52 | |
| 53 struct DelayedTask; | |
|
robliao
2016/04/04 17:59:53
Nit: Move this struct and the one below to the top
fdoray
2016/04/04 19:52:19
Done.
| |
| 54 struct DelayedTaskComparator { | |
| 55 bool operator()(const DelayedTask& left, const DelayedTask& right) const; | |
| 56 }; | |
| 57 | |
| 58 const Closure delayed_run_time_changed_; | |
| 59 | |
| 60 // Synchronizes acces to all members below. | |
|
robliao
2016/04/04 17:59:53
Nit: access
fdoray
2016/04/04 19:52:19
Done.
| |
| 61 mutable SchedulerLock lock_; | |
| 62 | |
| 63 // Priority queue of delayed tasks. The delayed task with the smallest | |
| 64 // |task->delayed_run_time| is in front of the priority queue. | |
| 65 std::priority_queue<DelayedTask, | |
|
robliao
2016/04/04 17:59:53
Nit: This might be more readable as a type.
using
fdoray
2016/04/04 19:52:19
Done.
| |
| 66 std::vector<DelayedTask>, | |
| 67 DelayedTaskComparator> | |
| 68 delayed_tasks_; | |
| 69 | |
| 70 // The index to assign to the next delayed task added to the manager. | |
| 71 size_t next_delayed_task_index_ = 0; | |
| 72 | |
| 73 DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); | |
| 74 }; | |
| 75 | |
| 76 } // namespace internal | |
| 77 } // namespace base | |
| 78 | |
| 79 #endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | |
| OLD | NEW |