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

Side by Side Diff: base/task_scheduler/delayed_task_manager.h

Issue 1685423002: Task Scheduler. (Closed) Base URL: https://luckyluke-private.googlesource.com/src@a_master
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 unified diff | Download patch
OLDNEW
(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 <queue>
9
10 #include "base/base_export.h"
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/task_scheduler/scheduler_lock.h"
15 #include "base/task_scheduler/sequence.h"
16 #include "base/task_scheduler/task.h"
17 #include "base/time/time.h"
18
19 namespace base {
20 namespace task_scheduler {
fdoray 2016/02/11 17:30:32 Could we have a subtle namespace for things that p
robliao 2016/02/11 21:56:27 Maybe it's the return of the internal namespace.
fdoray 2016/02/12 04:16:19 Done.
21
22 class PriorityQueue;
23 class ShutdownManager;
24
25 // Holds delayed tasks until they become ripe for execution. This class is
26 // thread-safe.
27 class BASE_EXPORT DelayedTaskManager {
28 public:
29 // |delayed_run_time_changed| is invoked when the next time at which a delayed
30 // task will become ripe for execution changes. |shutdown_manager| is used to
31 // handle shutdown behavior of tasks.
32 DelayedTaskManager(const Closure& delayed_run_time_changed,
33 ShutdownManager* shutdown_manager);
34 ~DelayedTaskManager();
35
36 // Adds |task| to a queue of delayed tasks. The task will be inserted into
37 // |sequence| and |priority_queue| the first time that PostReadyTasks() is
38 // called with Now() >= |task.delayed_run_time|.
39 void AddDelayedTask(const Task& task,
40 scoped_refptr<Sequence> sequence,
41 PriorityQueue* priority_queue);
42
43 // Posts delayed tasks that are ripe for execution.
44 void PostReadyTasks();
45
46 // Returns the next time at which a delayed task will become ripe for
47 // execution, or a null TimeTicks if there is no pending delayed tasks.
48 TimeTicks GetNextDelayedTaskReadyTime() const;
fdoray 2016/02/11 17:30:32 GetNextDelayedRunTime()
fdoray 2016/02/12 04:16:19 Done.
49
50 private:
51 // Returns the current time. Can be overridden for tests.
52 virtual TimeTicks Now();
53
54 struct DelayedTask {
55 DelayedTask();
56 ~DelayedTask();
57
58 Task task;
59
60 // The sequence and priority queue in which the task is inserted once it
61 // becomes ripe for execution.
62 scoped_refptr<Sequence> sequence;
63 PriorityQueue* priority_queue;
64
65 // Ensures that tasks that have the same |delayed_run_time| are popped from
66 // |delayed_tasks_| in the order in which they were pushed into it.
67 size_t index;
68
69 bool operator<(const DelayedTask& other) const;
70 };
71
72 // Lock protecting |delayed_tasks_| and |next_delayed_task_index_|.
73 mutable SchedulerLock lock_;
74
75 // Priority queue of delayed tasks. The delayed task with the smallest
76 // |delayed_run_time| is in front of the priority queue.
fdoray 2016/02/11 17:30:32 task.delayed_run_time
fdoray 2016/02/12 04:16:19 Done.
77 std::priority_queue<DelayedTask> delayed_tasks_;
78
79 // The index to assign to the next delayed task added to |delayed_tasks_|.
80 size_t next_delayed_task_index_;
81
82 const Closure delayed_run_time_changed_;
83
84 ShutdownManager* const shutdown_manager_;
85
86 DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager);
87 };
88
89 } // namespace task_scheduler
90 } // namespace base
91
92 #endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698