OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | 5 #ifndef BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
6 #define BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | 6 #define BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <queue> | 11 #include <queue> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/base_export.h" | 14 #include "base/base_export.h" |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/task_scheduler/scheduler_lock.h" | 18 #include "base/task_scheduler/scheduler_lock.h" |
19 #include "base/task_scheduler/sequence.h" | 19 #include "base/task_scheduler/sequence.h" |
20 #include "base/task_scheduler/task.h" | 20 #include "base/task_scheduler/task.h" |
21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
22 | 22 |
23 namespace base { | 23 namespace base { |
24 namespace internal { | 24 namespace internal { |
25 | 25 |
26 class SchedulerThreadPool; | 26 class SchedulerThreadPool; |
27 class SchedulerWorkerThread; | |
27 | 28 |
28 // A DelayedTaskManager holds delayed Tasks until they become ripe for | 29 // A DelayedTaskManager holds delayed Tasks until they become ripe for |
29 // execution. When they become ripe for execution, it posts them to their | 30 // execution. This class is thread-safe. |
30 // associated Sequence and SchedulerThreadPool. This class is thread-safe. | |
31 class BASE_EXPORT DelayedTaskManager { | 31 class BASE_EXPORT DelayedTaskManager { |
32 public: | 32 public: |
33 // |on_delayed_run_time_updated| is invoked when the delayed run time is | 33 // |on_delayed_run_time_updated| is invoked when the delayed run time is |
34 // updated as a result of adding a delayed task to the manager. | 34 // updated as a result of adding a delayed task to the manager. |
35 explicit DelayedTaskManager(const Closure& on_delayed_run_time_updated); | 35 explicit DelayedTaskManager(const Closure& on_delayed_run_time_updated); |
36 ~DelayedTaskManager(); | 36 ~DelayedTaskManager(); |
37 | 37 |
38 // Adds |task| to a queue of delayed tasks. The task will be posted to | 38 // Adds |task| to a queue of delayed tasks. The task will be posted to |
39 // |thread_pool| as part of |sequence| the first time that PostReadyTasks() is | 39 // |thread_pool| with |sequence| and |worker_thread| the first time that |
40 // called while Now() is passed |task->delayed_run_time|. | 40 // PostReadyTasks() is called while Now() is passed |task->delayed_run_time|. |
41 // |worker_thread| is optional. | |
gab
2016/04/25 21:00:20
Given the above comment says it will be "passed to
fdoray
2016/04/25 22:34:44
Done.
| |
41 // | 42 // |
42 // TODO(robliao): Find a concrete way to manage |thread_pool|'s memory. It is | 43 // TODO(robliao): Find a concrete way to manage the memory of |worker_thread| |
43 // never deleted in production, but it is better not to spread this assumption | 44 // and |thread_pool|. These objects are never deleted in production, but it is |
44 // throughout the scheduler. | 45 // better not to spread this assumption throughout the scheduler. |
45 void AddDelayedTask(std::unique_ptr<Task> task, | 46 void AddDelayedTask(std::unique_ptr<Task> task, |
46 scoped_refptr<Sequence> sequence, | 47 scoped_refptr<Sequence> sequence, |
48 SchedulerWorkerThread* worker_thread, | |
gab
2016/04/25 21:00:20
I'm not a fan of having a |worker_thread| paramete
robliao
2016/04/25 21:42:38
Alternatively we could have two delayed tasks meth
fdoray
2016/04/25 22:34:44
The 3 solutions are:
1. Keep this as-is.
2. Add a
gab
2016/04/26 11:46:27
Meh, not sure about 3. either as DelayedTask's con
robliao
2016/04/26 16:58:56
Exposing DelayedTask's constructor allows you to h
gab
2016/04/26 20:30:58
Good point, I'd be okay with that then. Maybe in a
fdoray
2016/04/27 16:09:59
If we want a single version of SchedulerThreadPool
robliao
2016/04/27 17:43:45
The thing that I like to avoid is forcing callers
gab
2016/04/27 18:11:43
Either way PostTaskWithSequenceNow() will need to
robliao
2016/04/27 18:21:03
Yup, agreed on the default argument points. I'm fi
fdoray
2016/04/27 19:17:04
Keeping this discussion for another CL.
| |
47 SchedulerThreadPool* thread_pool); | 49 SchedulerThreadPool* thread_pool); |
48 | 50 |
49 // Posts delayed tasks that are ripe for execution. | 51 // Posts delayed tasks that are ripe for execution. |
50 // TODO(robliao): Call this from a service thread. | 52 // TODO(robliao): Call this from a service thread. |
51 void PostReadyTasks(); | 53 void PostReadyTasks(); |
52 | 54 |
53 // Returns the next time at which a delayed task will become ripe for | 55 // Returns the next time at which a delayed task will become ripe for |
54 // execution, or a null TimeTicks if there are no pending delayed tasks. | 56 // execution, or a null TimeTicks if there are no pending delayed tasks. |
55 TimeTicks GetDelayedRunTime() const; | 57 TimeTicks GetDelayedRunTime() const; |
56 | 58 |
57 // Returns the current time. Can be overridden for tests. | 59 // Returns the current time. Can be overridden for tests. |
58 virtual TimeTicks Now() const; | 60 virtual TimeTicks Now() const; |
59 | 61 |
60 private: | 62 private: |
61 struct DelayedTask; | 63 struct DelayedTask; |
robliao
2016/04/25 21:42:38
I wonder if at some point if it makes sense to exp
fdoray
2016/04/25 22:34:44
see previous comment
| |
62 struct DelayedTaskComparator { | 64 struct DelayedTaskComparator { |
63 bool operator()(const DelayedTask& left, const DelayedTask& right) const; | 65 bool operator()(const DelayedTask& left, const DelayedTask& right) const; |
64 }; | 66 }; |
65 | 67 |
66 const Closure on_delayed_run_time_updated_; | 68 const Closure on_delayed_run_time_updated_; |
67 | 69 |
68 // Synchronizes access to all members below. | 70 // Synchronizes access to all members below. |
69 mutable SchedulerLock lock_; | 71 mutable SchedulerLock lock_; |
70 | 72 |
71 // Priority queue of delayed tasks. The delayed task with the smallest | 73 // Priority queue of delayed tasks. The delayed task with the smallest |
72 // |task->delayed_run_time| is in front of the priority queue. | 74 // |task->delayed_run_time| is in front of the priority queue. |
73 using DelayedTaskQueue = std::priority_queue<DelayedTask, | 75 using DelayedTaskQueue = std::priority_queue<DelayedTask, |
74 std::vector<DelayedTask>, | 76 std::vector<DelayedTask>, |
75 DelayedTaskComparator>; | 77 DelayedTaskComparator>; |
76 DelayedTaskQueue delayed_tasks_; | 78 DelayedTaskQueue delayed_tasks_; |
77 | 79 |
78 // The index to assign to the next delayed task added to the manager. | 80 // The index to assign to the next delayed task added to the manager. |
79 uint64_t delayed_task_index_ = 0; | 81 uint64_t delayed_task_index_ = 0; |
80 | 82 |
81 DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); | 83 DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); |
82 }; | 84 }; |
83 | 85 |
84 } // namespace internal | 86 } // namespace internal |
85 } // namespace base | 87 } // namespace base |
86 | 88 |
87 #endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | 89 #endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
OLD | NEW |