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 "content/child/webthread_impl.h" | 8 #include "content/child/webthread_impl.h" |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 base::MessageLoop::current()->RemoveTaskObserver(iter->second); | 53 base::MessageLoop::current()->RemoveTaskObserver(iter->second); |
54 delete iter->second; | 54 delete iter->second; |
55 task_observer_map_.erase(iter); | 55 task_observer_map_.erase(iter); |
56 } | 56 } |
57 | 57 |
58 WebThreadImpl::WebThreadImpl(const char* name) | 58 WebThreadImpl::WebThreadImpl(const char* name) |
59 : thread_(new base::Thread(name)) { | 59 : thread_(new base::Thread(name)) { |
60 thread_->Start(); | 60 thread_->Start(); |
61 } | 61 } |
62 | 62 |
| 63 // RunWebThreadTask takes the ownership of |task| from base::Closure and |
| 64 // deletes it on the first invocation of the closure for thread-safety. |
| 65 // base::Closure made from RunWebThreadTask is copyable but Closure::Run |
| 66 // should be called at most only once. |
| 67 // This is because WebThread::Task can contain RefPtr to a |
| 68 // thread-unsafe-reference-counted object (e.g. WorkerThreadTask can contain |
| 69 // RefPtr to WebKit's StringImpl), and if we don't delete |task| here, |
| 70 // it causes a race condition as follows: |
| 71 // [A] In task->run(), more RefPtr's to the refcounted object can be created, |
| 72 // and the reference counter of the object can be modified via these |
| 73 // RefPtr's (as intended) on the thread where the task is executed. |
| 74 // [B] However, base::Closure still retains the ownership of WebThread::Task |
| 75 // even after RunWebThreadTask is called. |
| 76 // When base::Closure is deleted, WebThread::Task is deleted and the |
| 77 // reference counter of the object is decreased by one, possibly from a |
| 78 // different thread from [A], which is a race condition. |
| 79 // Taking the ownership of |task| here by using scoped_ptr and base::Passed |
| 80 // removes the reference counter modification of [B] and the race condition. |
| 81 // When the closure never runs at all, the corresponding WebThread::Task is |
| 82 // destructed when base::Closure is deleted (like [B]). In this case, there |
| 83 // are no reference counter modification like [A] (because task->run() is not |
| 84 // executed), so there are no race conditions. |
| 85 // See https://crbug.com/390851 for more details. |
| 86 static void RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) { |
| 87 task->run(); |
| 88 } |
| 89 |
63 void WebThreadImpl::postTask(Task* task) { | 90 void WebThreadImpl::postTask(Task* task) { |
64 thread_->message_loop()->PostTask( | 91 thread_->message_loop()->PostTask( |
65 FROM_HERE, base::Bind(&blink::WebThread::Task::run, base::Owned(task))); | 92 FROM_HERE, |
| 93 base::Bind(RunWebThreadTask, base::Passed(scoped_ptr<Task>(task)))); |
66 } | 94 } |
67 | 95 |
68 void WebThreadImpl::postDelayedTask(Task* task, long long delay_ms) { | 96 void WebThreadImpl::postDelayedTask(Task* task, long long delay_ms) { |
69 thread_->message_loop()->PostDelayedTask( | 97 thread_->message_loop()->PostDelayedTask( |
70 FROM_HERE, | 98 FROM_HERE, |
71 base::Bind(&blink::WebThread::Task::run, base::Owned(task)), | 99 base::Bind(RunWebThreadTask, base::Passed(scoped_ptr<Task>(task))), |
72 base::TimeDelta::FromMilliseconds(delay_ms)); | 100 base::TimeDelta::FromMilliseconds(delay_ms)); |
73 } | 101 } |
74 | 102 |
75 void WebThreadImpl::enterRunLoop() { | 103 void WebThreadImpl::enterRunLoop() { |
76 CHECK(isCurrentThread()); | 104 CHECK(isCurrentThread()); |
77 CHECK(!thread_->message_loop()->is_running()); // We don't support nesting. | 105 CHECK(!thread_->message_loop()->is_running()); // We don't support nesting. |
78 thread_->message_loop()->Run(); | 106 thread_->message_loop()->Run(); |
79 } | 107 } |
80 | 108 |
81 void WebThreadImpl::exitRunLoop() { | 109 void WebThreadImpl::exitRunLoop() { |
(...skipping 14 matching lines...) Expand all Loading... |
96 thread_->Stop(); | 124 thread_->Stop(); |
97 } | 125 } |
98 | 126 |
99 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop( | 127 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop( |
100 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) | 128 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) |
101 : main_thread_task_runner_(main_thread_task_runner), | 129 : main_thread_task_runner_(main_thread_task_runner), |
102 thread_id_(base::PlatformThread::CurrentId()) {} | 130 thread_id_(base::PlatformThread::CurrentId()) {} |
103 | 131 |
104 void WebThreadImplForMessageLoop::postTask(Task* task) { | 132 void WebThreadImplForMessageLoop::postTask(Task* task) { |
105 main_thread_task_runner_->PostTask( | 133 main_thread_task_runner_->PostTask( |
106 FROM_HERE, base::Bind(&blink::WebThread::Task::run, base::Owned(task))); | 134 FROM_HERE, |
| 135 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task)))); |
107 } | 136 } |
108 | 137 |
109 void WebThreadImplForMessageLoop::postDelayedTask(Task* task, | 138 void WebThreadImplForMessageLoop::postDelayedTask(Task* task, |
110 long long delay_ms) { | 139 long long delay_ms) { |
111 main_thread_task_runner_->PostDelayedTask( | 140 main_thread_task_runner_->PostDelayedTask( |
112 FROM_HERE, | 141 FROM_HERE, |
113 base::Bind(&blink::WebThread::Task::run, base::Owned(task)), | 142 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))), |
114 base::TimeDelta::FromMilliseconds(delay_ms)); | 143 base::TimeDelta::FromMilliseconds(delay_ms)); |
115 } | 144 } |
116 | 145 |
117 void WebThreadImplForMessageLoop::enterRunLoop() { | 146 void WebThreadImplForMessageLoop::enterRunLoop() { |
118 CHECK(isCurrentThread()); | 147 CHECK(isCurrentThread()); |
119 // We don't support nesting. | 148 // We don't support nesting. |
120 CHECK(!base::MessageLoop::current()->is_running()); | 149 CHECK(!base::MessageLoop::current()->is_running()); |
121 base::MessageLoop::current()->Run(); | 150 base::MessageLoop::current()->Run(); |
122 } | 151 } |
123 | 152 |
124 void WebThreadImplForMessageLoop::exitRunLoop() { | 153 void WebThreadImplForMessageLoop::exitRunLoop() { |
125 CHECK(isCurrentThread()); | 154 CHECK(isCurrentThread()); |
126 CHECK(base::MessageLoop::current()->is_running()); | 155 CHECK(base::MessageLoop::current()->is_running()); |
127 base::MessageLoop::current()->Quit(); | 156 base::MessageLoop::current()->Quit(); |
128 } | 157 } |
129 | 158 |
130 bool WebThreadImplForMessageLoop::isCurrentThread() const { | 159 bool WebThreadImplForMessageLoop::isCurrentThread() const { |
131 return main_thread_task_runner_->BelongsToCurrentThread(); | 160 return main_thread_task_runner_->BelongsToCurrentThread(); |
132 } | 161 } |
133 | 162 |
134 blink::PlatformThreadId WebThreadImplForMessageLoop::threadId() const { | 163 blink::PlatformThreadId WebThreadImplForMessageLoop::threadId() const { |
135 return thread_id_; | 164 return thread_id_; |
136 } | 165 } |
137 | 166 |
138 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {} | 167 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {} |
139 | 168 |
140 } // namespace content | 169 } // namespace content |
OLD | NEW |