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 { | |
gab
2016/04/07 21:32:08
Does it make sense to hook it up to SchedulerThrea
fdoray
2016/04/08 17:24:51
We need the service thread for this to be function
| |
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); | |
gab
2016/04/07 21:32:08
on_next_delayed_run_time_updated ?
(to make it cl
fdoray
2016/04/08 17:24:51
Done.
| |
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|. | |
gab
2016/04/07 21:32:08
s/with the current time being great or equal to |t
fdoray
2016/04/08 17:24:51
Done.
| |
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 struct DelayedTask; | |
51 struct DelayedTaskComparator { | |
52 bool operator()(const DelayedTask& left, const DelayedTask& right) const; | |
53 }; | |
54 | |
55 // Returns the current time. Can be overridden for tests. | |
56 virtual TimeTicks Now() const; | |
57 | |
58 const Closure delayed_run_time_changed_; | |
59 | |
60 // Synchronizes access to all members below. | |
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 using DelayedTaskQueue = std::priority_queue<DelayedTask, | |
66 std::vector<DelayedTask>, | |
67 DelayedTaskComparator>; | |
gab
2016/04/07 21:32:08
I'm surprised that the definition of DelayedTask i
fdoray
2016/04/08 17:24:51
The definition of DelayedTask isn't required to co
| |
68 DelayedTaskQueue 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 |