1 // Copyright 2014 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.
5 #include "content/child/scheduler/single_thread_idle_task_runner.h"
7 #include "base/location.h"
8 #include "base/trace_event/trace_event.h"
12 SingleThreadIdleTaskRunner::SingleThreadIdleTaskRunner(
13 scoped_refptr
<base::SingleThreadTaskRunner
> idle_priority_task_runner
,
14 scoped_refptr
<base::SingleThreadTaskRunner
> after_wakeup_task_runner
,
15 base::Callback
<void(base::TimeTicks
*)> deadline_supplier
,
16 const char* tracing_category
)
17 : idle_priority_task_runner_(idle_priority_task_runner
),
18 after_wakeup_task_runner_(after_wakeup_task_runner
),
19 deadline_supplier_(deadline_supplier
),
20 tracing_category_(tracing_category
),
22 DCHECK(!idle_priority_task_runner_
||
23 idle_priority_task_runner_
->RunsTasksOnCurrentThread());
24 DCHECK(!after_wakeup_task_runner_
||
25 after_wakeup_task_runner_
->RunsTasksOnCurrentThread());
26 weak_scheduler_ptr_
= weak_factory_
.GetWeakPtr();
29 SingleThreadIdleTaskRunner::~SingleThreadIdleTaskRunner() {
32 bool SingleThreadIdleTaskRunner::RunsTasksOnCurrentThread() const {
33 return idle_priority_task_runner_
->RunsTasksOnCurrentThread();
36 void SingleThreadIdleTaskRunner::PostIdleTask(
37 const tracked_objects::Location
& from_here
,
38 const IdleTask
& idle_task
) {
39 idle_priority_task_runner_
->PostTask(
41 base::Bind(&SingleThreadIdleTaskRunner::RunTask
,
42 weak_scheduler_ptr_
, idle_task
));
45 void SingleThreadIdleTaskRunner::PostNonNestableIdleTask(
46 const tracked_objects::Location
& from_here
,
47 const IdleTask
& idle_task
) {
48 idle_priority_task_runner_
->PostNonNestableTask(
50 base::Bind(&SingleThreadIdleTaskRunner::RunTask
,
51 weak_scheduler_ptr_
, idle_task
));
54 void SingleThreadIdleTaskRunner::PostIdleTaskAfterWakeup(
55 const tracked_objects::Location
& from_here
,
56 const IdleTask
& idle_task
) {
57 after_wakeup_task_runner_
->PostTask(
58 FROM_HERE
, base::Bind(&SingleThreadIdleTaskRunner::PostIdleTask
,
59 weak_scheduler_ptr_
, from_here
, idle_task
));
62 void SingleThreadIdleTaskRunner::RunTask(IdleTask idle_task
) {
63 base::TimeTicks deadline
;
64 deadline_supplier_
.Run(&deadline
);
65 TRACE_EVENT1(tracing_category_
, "SingleThreadIdleTaskRunner::RunTask",
67 (deadline
- base::TimeTicks::Now()).InMillisecondsF());
68 idle_task
.Run(deadline
);
70 TRACE_EVENT_CATEGORY_GROUP_ENABLED(tracing_category_
, &is_tracing
);
71 if (is_tracing
&& base::TimeTicks::Now() > deadline
) {
72 TRACE_EVENT_INSTANT0(tracing_category_
,
73 "SingleThreadIdleTaskRunner::DidOverrunDeadline",
74 TRACE_EVENT_SCOPE_THREAD
);
78 } // namespace content