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 #include "base/task_scheduler/delayed_task_manager.h"
6
7 #include <algorithm>
8
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/task_runner.h"
12 #include "base/task_scheduler/task.h"
13
14 namespace base {
15 namespace internal {
16
DelayedTaskManager(std::unique_ptr<const TickClock> tick_clock)17 DelayedTaskManager::DelayedTaskManager(
18 std::unique_ptr<const TickClock> tick_clock)
19 : tick_clock_(std::move(tick_clock)) {
20 DCHECK(tick_clock_);
21 }
22
23 DelayedTaskManager::~DelayedTaskManager() = default;
24
Start(scoped_refptr<TaskRunner> service_thread_task_runner)25 void DelayedTaskManager::Start(
26 scoped_refptr<TaskRunner> service_thread_task_runner) {
27 DCHECK(service_thread_task_runner);
28
29 decltype(tasks_added_before_start_) tasks_added_before_start;
30
31 {
32 AutoSchedulerLock auto_lock(lock_);
33 DCHECK(!service_thread_task_runner_);
34 DCHECK(!started_.IsSet());
35 service_thread_task_runner_ = std::move(service_thread_task_runner);
36 tasks_added_before_start = std::move(tasks_added_before_start_);
37 // |service_thread_task_runner_| must not change after |started_| is set
38 // (cf. comment above |lock_| in header file).
39 started_.Set();
40 }
41
42 const TimeTicks now = tick_clock_->NowTicks();
43 for (auto& task_and_callback : tasks_added_before_start) {
44 const TimeDelta delay =
45 std::max(TimeDelta(), task_and_callback.first.delayed_run_time - now);
46 AddDelayedTaskNow(std::move(task_and_callback.first), delay,
47 std::move(task_and_callback.second));
48 }
49 }
50
AddDelayedTask(Task task,PostTaskNowCallback post_task_now_callback)51 void DelayedTaskManager::AddDelayedTask(
52 Task task,
53 PostTaskNowCallback post_task_now_callback) {
54 DCHECK(task.task);
55
56 const TimeDelta delay = task.delay;
57 DCHECK(!delay.is_zero());
58
59 // Use CHECK instead of DCHECK to crash earlier. See http://crbug.com/711167
60 // for details.
61 CHECK(task.task);
62
63 // If |started_| is set, the DelayedTaskManager is in a stable state and
64 // AddDelayedTaskNow() can be called without synchronization. Otherwise, it is
65 // necessary to acquire |lock_| and recheck.
66 if (started_.IsSet()) {
67 AddDelayedTaskNow(std::move(task), delay,
68 std::move(post_task_now_callback));
69 } else {
70 AutoSchedulerLock auto_lock(lock_);
71 if (started_.IsSet()) {
72 AddDelayedTaskNow(std::move(task), delay,
73 std::move(post_task_now_callback));
74 } else {
75 tasks_added_before_start_.push_back(
76 {std::move(task), std::move(post_task_now_callback)});
77 }
78 }
79 }
80
AddDelayedTaskNow(Task task,TimeDelta delay,PostTaskNowCallback post_task_now_callback)81 void DelayedTaskManager::AddDelayedTaskNow(
82 Task task,
83 TimeDelta delay,
84 PostTaskNowCallback post_task_now_callback) {
85 DCHECK(task.task);
86 DCHECK(started_.IsSet());
87 // TODO(fdoray): Use |task->delayed_run_time| on the service thread
88 // MessageLoop rather than recomputing it from |delay|.
89 service_thread_task_runner_->PostDelayedTask(
90 FROM_HERE, BindOnce(std::move(post_task_now_callback), std::move(task)),
91 delay);
92 }
93
94 } // namespace internal
95 } // namespace base
96