OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // An implementation of WebThread in terms of base::MessageLoop and | 5 // An implementation of WebThread in terms of base::MessageLoop and |
6 // base::Thread | 6 // base::Thread |
7 | 7 |
8 #include "components/scheduler/child/webthread_base.h" | 8 #include "components/scheduler/child/webthread_base.h" |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 void WebThreadBase::AddTaskObserverInternal( | 65 void WebThreadBase::AddTaskObserverInternal( |
66 base::MessageLoop::TaskObserver* observer) { | 66 base::MessageLoop::TaskObserver* observer) { |
67 base::MessageLoop::current()->AddTaskObserver(observer); | 67 base::MessageLoop::current()->AddTaskObserver(observer); |
68 } | 68 } |
69 | 69 |
70 void WebThreadBase::RemoveTaskObserverInternal( | 70 void WebThreadBase::RemoveTaskObserverInternal( |
71 base::MessageLoop::TaskObserver* observer) { | 71 base::MessageLoop::TaskObserver* observer) { |
72 base::MessageLoop::current()->RemoveTaskObserver(observer); | 72 base::MessageLoop::current()->RemoveTaskObserver(observer); |
73 } | 73 } |
74 | 74 |
75 // RunWebThreadTask takes the ownership of |task| from base::Closure and | |
76 // deletes it on the first invocation of the closure for thread-safety. | |
77 // base::Closure made from RunWebThreadTask is copyable but Closure::Run | |
78 // should be called at most only once. | |
79 // This is because WebThread::Task can contain RefPtr to a | |
80 // thread-unsafe-reference-counted object (e.g. WorkerThreadTask can contain | |
81 // RefPtr to WebKit's StringImpl), and if we don't delete |task| here, | |
82 // it causes a race condition as follows: | |
83 // [A] In task->run(), more RefPtr's to the refcounted object can be created, | |
84 // and the reference counter of the object can be modified via these | |
85 // RefPtr's (as intended) on the thread where the task is executed. | |
86 // [B] However, base::Closure still retains the ownership of WebThread::Task | |
87 // even after RunWebThreadTask is called. | |
88 // When base::Closure is deleted, WebThread::Task is deleted and the | |
89 // reference counter of the object is decreased by one, possibly from a | |
90 // different thread from [A], which is a race condition. | |
91 // Taking the ownership of |task| here by using scoped_ptr and base::Passed | |
92 // removes the reference counter modification of [B] and the race condition. | |
93 // When the closure never runs at all, the corresponding WebThread::Task is | |
94 // destructed when base::Closure is deleted (like [B]). In this case, there | |
95 // are no reference counter modification like [A] (because task->run() is not | |
96 // executed), so there are no race conditions. | |
97 // See https://crbug.com/390851 for more details. | |
98 // | |
99 // static | |
100 void WebThreadBase::RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) { | |
101 task->run(); | |
102 } | |
103 | |
104 // static | 75 // static |
105 void WebThreadBase::RunWebThreadIdleTask( | 76 void WebThreadBase::RunWebThreadIdleTask( |
106 scoped_ptr<blink::WebThread::IdleTask> idle_task, | 77 scoped_ptr<blink::WebThread::IdleTask> idle_task, |
107 base::TimeTicks deadline) { | 78 base::TimeTicks deadline) { |
108 idle_task->run((deadline - base::TimeTicks()).InSecondsF()); | 79 idle_task->run((deadline - base::TimeTicks()).InSecondsF()); |
109 } | 80 } |
110 | 81 |
111 void WebThreadBase::postTask(const blink::WebTraceLocation& location, | |
112 Task* task) { | |
113 postDelayedTask(location, task, 0); | |
114 } | |
115 | |
116 void WebThreadBase::postDelayedTask(const blink::WebTraceLocation& web_location, | |
117 Task* task, | |
118 long long delay_ms) { | |
119 tracked_objects::Location location(web_location.functionName(), | |
120 web_location.fileName(), -1, nullptr); | |
121 TaskRunner()->PostDelayedTask( | |
122 location, | |
123 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))), | |
124 base::TimeDelta::FromMilliseconds(delay_ms)); | |
125 } | |
126 | |
127 void WebThreadBase::postIdleTask(const blink::WebTraceLocation& web_location, | 82 void WebThreadBase::postIdleTask(const blink::WebTraceLocation& web_location, |
128 IdleTask* idle_task) { | 83 IdleTask* idle_task) { |
129 tracked_objects::Location location(web_location.functionName(), | 84 tracked_objects::Location location(web_location.functionName(), |
130 web_location.fileName(), -1, nullptr); | 85 web_location.fileName(), -1, nullptr); |
131 IdleTaskRunner()->PostIdleTask( | 86 IdleTaskRunner()->PostIdleTask( |
132 location, base::Bind(&WebThreadBase::RunWebThreadIdleTask, | 87 location, base::Bind(&WebThreadBase::RunWebThreadIdleTask, |
133 base::Passed(make_scoped_ptr(idle_task)))); | 88 base::Passed(make_scoped_ptr(idle_task)))); |
134 } | 89 } |
135 | 90 |
136 void WebThreadBase::postIdleTaskAfterWakeup( | 91 void WebThreadBase::postIdleTaskAfterWakeup( |
137 const blink::WebTraceLocation& web_location, | 92 const blink::WebTraceLocation& web_location, |
138 IdleTask* idle_task) { | 93 IdleTask* idle_task) { |
139 tracked_objects::Location location(web_location.functionName(), | 94 tracked_objects::Location location(web_location.functionName(), |
140 web_location.fileName(), -1, nullptr); | 95 web_location.fileName(), -1, nullptr); |
141 IdleTaskRunner()->PostIdleTaskAfterWakeup( | 96 IdleTaskRunner()->PostIdleTaskAfterWakeup( |
142 location, base::Bind(&WebThreadBase::RunWebThreadIdleTask, | 97 location, base::Bind(&WebThreadBase::RunWebThreadIdleTask, |
143 base::Passed(make_scoped_ptr(idle_task)))); | 98 base::Passed(make_scoped_ptr(idle_task)))); |
144 } | 99 } |
145 | 100 |
146 bool WebThreadBase::isCurrentThread() const { | 101 bool WebThreadBase::isCurrentThread() const { |
147 return TaskRunner()->BelongsToCurrentThread(); | 102 return TaskRunner()->BelongsToCurrentThread(); |
148 } | 103 } |
149 | 104 |
150 } // namespace scheduler | 105 } // namespace scheduler |
OLD | NEW |