Chromium Code Reviews| Index: base/task_scheduler/delayed_task_manager.h |
| diff --git a/base/task_scheduler/delayed_task_manager.h b/base/task_scheduler/delayed_task_manager.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ee5273d364559b46949256d7bfadbad1ae22d041 |
| --- /dev/null |
| +++ b/base/task_scheduler/delayed_task_manager.h |
| @@ -0,0 +1,87 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
| +#define BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
| + |
| +#include <stddef.h> |
| + |
| +#include <memory> |
| +#include <queue> |
| + |
| +#include "base/base_export.h" |
| +#include "base/callback.h" |
| +#include "base/macros.h" |
| +#include "base/task_scheduler/scheduler_lock.h" |
| +#include "base/task_scheduler/task.h" |
| +#include "base/time/time.h" |
| + |
| +namespace base { |
| +namespace internal { |
| + |
| +// A DelayedTaskManager holds delayed tasks until they become ripe for |
| +// execution. When they become ripe for execution, it invokes a custom |
| +// callback. This class is thread-safe. |
| +class BASE_EXPORT DelayedTaskManager { |
| + public: |
| + 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!
|
| + |
| + // |delayed_run_time_changed| is invoked when the next delayed run time |
| + // changes as a result of adding a new delayed task to the manager. |
| + explicit DelayedTaskManager(const Closure& delayed_run_time_changed); |
| + ~DelayedTaskManager(); |
| + |
| + // Adds |task| to a queue of delayed tasks. |post_task_callback| will be |
| + // invoked the first time that PostReadyTasks() is called with the current |
| + // time being greater or equal to |task->delayed_run_time|. |
| + void AddDelayedTask(std::unique_ptr<Task> task, |
| + const PostTaskCallback& post_task_callback); |
| + |
| + // Posts delayed tasks that are ripe for execution. |
| + void PostReadyTasks(); |
| + |
| + // Returns the next time at which a delayed task will become ripe for |
| + // execution, or a null TimeTicks if there is no pending delayed tasks. |
| + TimeTicks GetNextDelayedRunTime() const; |
| + |
| + private: |
| + // Returns the current time. Can be overridden for tests. |
| + virtual TimeTicks Now(); |
| + |
| + 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.
|
| + 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.
|
| + ~DelayedTask(); |
| + |
| + DelayedTask(DelayedTask&& other); |
| + DelayedTask& operator=(DelayedTask&& other); |
| + |
| + std::unique_ptr<Task> task; |
| + PostTaskCallback post_task_callback; |
| + |
| + // Ensures that tasks that have the same |delayed_run_time| are sorted |
| + // according to the order in which they were created. |
| + size_t index; |
| + |
| + 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.
|
| + }; |
| + |
| + const Closure delayed_run_time_changed_; |
| + |
| + // Synchronizes acces to all members below. |
| + mutable SchedulerLock lock_; |
| + |
| + // Priority queue of delayed tasks. The delayed task with the smallest |
| + // |task->delayed_run_time| is in front of the priority queue. |
| + std::priority_queue<DelayedTask> delayed_tasks_; |
| + |
| + // The index to assign to the next delayed task added to the manager. |
| + size_t next_delayed_task_index_ = 0; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); |
| +}; |
| + |
| +} // namespace internal |
| +} // namespace base |
| + |
| +#endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |