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 |
index 55cf8e521d0568eb30dff66fc9ca170f434eca8f..c9d487c7d4c80f6009f5c0589e96557816ea76d8 100644 |
--- a/base/task_scheduler/delayed_task_manager.h |
+++ b/base/task_scheduler/delayed_task_manager.h |
@@ -6,11 +6,18 @@ |
#define BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
#include <memory> |
+#include <utility> |
+#include <vector> |
#include "base/base_export.h" |
-#include "base/callback_forward.h" |
+#include "base/callback.h" |
#include "base/macros.h" |
+#include "base/memory/ptr_util.h" |
#include "base/memory/ref_counted.h" |
+#include "base/synchronization/atomic_flag.h" |
+#include "base/task_scheduler/scheduler_lock.h" |
+#include "base/time/default_tick_clock.h" |
+#include "base/time/tick_clock.h" |
namespace base { |
@@ -20,26 +27,51 @@ namespace internal { |
struct Task; |
-// The DelayedTaskManager forwards tasks to various scheduler components when |
-// they become ripe for execution. This class is thread-safe. |
+// The DelayedTaskManager forwards tasks to post task callbacks when they become |
+// ripe for execution. Tasks are not forwarded before Start() is called. This |
+// class is thread-safe. |
class BASE_EXPORT DelayedTaskManager { |
public: |
// Posts |task| for execution immediately. |
- using PostTaskNowCallback = Callback<void(std::unique_ptr<Task> task)>; |
+ using PostTaskNowCallback = OnceCallback<void(std::unique_ptr<Task> task)>; |
+ // |tick_clock| can be specified for testing. |
+ DelayedTaskManager( |
+ std::unique_ptr<TickClock> tick_clock = MakeUnique<DefaultTickClock>()); |
+ ~DelayedTaskManager(); |
+ |
+ // Starts the delayed task manager, allowing past and future tasks to be |
+ // forwarded to their callbacks as they become ripe for execution. |
// |service_thread_task_runner| posts tasks to the TaskScheduler service |
// thread. |
- explicit DelayedTaskManager( |
- scoped_refptr<TaskRunner> service_thread_task_runner); |
- ~DelayedTaskManager(); |
+ void Start(scoped_refptr<TaskRunner> service_thread_task_runner); |
- // Calls |post_task_now_callback| with |task| when |task| is ripe for |
- // execution. |
+ // Schedules a call to |post_task_now_callback| with |task| as argument when |
+ // |task| is ripe for execution and Start() has been called. |
void AddDelayedTask(std::unique_ptr<Task> task, |
- const PostTaskNowCallback& post_task_now_callback); |
+ PostTaskNowCallback post_task_now_callback); |
private: |
- const scoped_refptr<TaskRunner> service_thread_task_runner_; |
+ // Schedules a call to |post_task_now_callback| with |task| as argument when |
+ // |delay| expires. Start() must have been called before this. |
+ void AddDelayedTaskNow(std::unique_ptr<Task> task, |
+ TimeDelta delay, |
+ PostTaskNowCallback post_task_now_callback); |
+ |
+ const std::unique_ptr<TickClock> tick_clock_; |
+ |
+ AtomicFlag started_; |
+ |
+ // Synchronizes access to all members below before |started_| is set. Once |
+ // |started_| is set: |
+ // - |service_thread_task_runner| doest not change, so it can be read without |
+ // holding the lock. |
+ // - |tasks_added_before_start_| isn't accessed anymore. |
+ SchedulerLock lock_; |
+ |
+ scoped_refptr<TaskRunner> service_thread_task_runner_; |
+ std::vector<std::pair<std::unique_ptr<Task>, PostTaskNowCallback>> |
+ tasks_added_before_start_; |
DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); |
}; |