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 | |
| 13 #include "base/base_export.h" | |
| 14 #include "base/callback.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/task_scheduler/scheduler_lock.h" | |
| 17 #include "base/task_scheduler/task.h" | |
| 18 #include "base/time/time.h" | |
| 19 | |
| 20 namespace base { | |
| 21 namespace internal { | |
| 22 | |
| 23 // A DelayedTaskManager holds delayed tasks until they become ripe for | |
| 24 // execution. When they become ripe for execution, it invokes a custom | |
| 25 // callback. This class is thread-safe. | |
| 26 class BASE_EXPORT DelayedTaskManager { | |
| 27 public: | |
| 28 using PostTaskCallback = Callback<void(std::unique_ptr<Task>)>; | |
|
robliao
2016/04/01 20:48:25
It seems to guidance is to continue using scoped_p
robliao
2016/04/01 20:52:46
I just saw the e-mail. Nevermind!
| |
| 29 | |
| 30 // |delayed_run_time_changed| is invoked when the next delayed run time | |
| 31 // changes as a result of adding a new delayed task to the manager. | |
| 32 explicit DelayedTaskManager(const Closure& delayed_run_time_changed); | |
| 33 ~DelayedTaskManager(); | |
| 34 | |
| 35 // Adds |task| to a queue of delayed tasks. |post_task_callback| will be | |
| 36 // invoked the first time that PostReadyTasks() is called with the current | |
| 37 // time being greater or equal to |task->delayed_run_time|. | |
| 38 void AddDelayedTask(std::unique_ptr<Task> task, | |
| 39 const PostTaskCallback& post_task_callback); | |
| 40 | |
| 41 // Posts delayed tasks that are ripe for execution. | |
| 42 void PostReadyTasks(); | |
| 43 | |
| 44 // Returns the next time at which a delayed task will become ripe for | |
| 45 // execution, or a null TimeTicks if there is no pending delayed tasks. | |
| 46 TimeTicks GetNextDelayedRunTime() const; | |
| 47 | |
| 48 private: | |
| 49 // Returns the current time. Can be overridden for tests. | |
| 50 virtual TimeTicks Now(); | |
| 51 | |
| 52 struct DelayedTask { | |
|
robliao
2016/04/01 20:48:25
Given that this is only used via a template here,
fdoray
2016/04/01 21:21:58
Done.
| |
| 53 DelayedTask(); | |
|
robliao
2016/04/01 20:48:25
Would it make sense to have an additional construc
fdoray
2016/04/01 21:21:58
Done.
| |
| 54 ~DelayedTask(); | |
| 55 | |
| 56 DelayedTask(DelayedTask&& other); | |
| 57 DelayedTask& operator=(DelayedTask&& other); | |
| 58 | |
| 59 std::unique_ptr<Task> task; | |
| 60 PostTaskCallback post_task_callback; | |
| 61 | |
| 62 // Ensures that tasks that have the same |delayed_run_time| are sorted | |
| 63 // according to the order in which they were created. | |
| 64 size_t index; | |
| 65 | |
| 66 bool operator<(const DelayedTask& other) const; | |
|
robliao
2016/04/01 20:48:25
Looks like we'll also need operator> and operator=
fdoray
2016/04/01 21:21:57
The style guide says "don't define operator overlo
robliao
2016/04/04 17:59:53
sgtm.
| |
| 67 }; | |
| 68 | |
| 69 const Closure delayed_run_time_changed_; | |
| 70 | |
| 71 // Synchronizes acces to all members below. | |
| 72 mutable SchedulerLock lock_; | |
| 73 | |
| 74 // Priority queue of delayed tasks. The delayed task with the smallest | |
| 75 // |task->delayed_run_time| is in front of the priority queue. | |
| 76 std::priority_queue<DelayedTask> delayed_tasks_; | |
| 77 | |
| 78 // The index to assign to the next delayed task added to the manager. | |
| 79 size_t next_delayed_task_index_ = 0; | |
| 80 | |
| 81 DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); | |
| 82 }; | |
| 83 | |
| 84 } // namespace internal | |
| 85 } // namespace base | |
| 86 | |
| 87 #endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | |
| OLD | NEW |