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 25 matching lines...) Expand all Loading... | |
36 private: | 36 private: |
37 WebThread::TaskObserver* observer_; | 37 WebThread::TaskObserver* observer_; |
38 }; | 38 }; |
39 | 39 |
40 void WebThreadBase::addTaskObserver(TaskObserver* observer) { | 40 void WebThreadBase::addTaskObserver(TaskObserver* observer) { |
41 CHECK(isCurrentThread()); | 41 CHECK(isCurrentThread()); |
42 std::pair<TaskObserverMap::iterator, bool> result = task_observer_map_.insert( | 42 std::pair<TaskObserverMap::iterator, bool> result = task_observer_map_.insert( |
43 std::make_pair(observer, static_cast<TaskObserverAdapter*>(NULL))); | 43 std::make_pair(observer, static_cast<TaskObserverAdapter*>(NULL))); |
44 if (result.second) | 44 if (result.second) |
45 result.first->second = new TaskObserverAdapter(observer); | 45 result.first->second = new TaskObserverAdapter(observer); |
46 base::MessageLoop::current()->AddTaskObserver(result.first->second); | 46 AddTaskObserverInternal(result.first->second); |
47 } | 47 } |
48 | 48 |
49 void WebThreadBase::removeTaskObserver(TaskObserver* observer) { | 49 void WebThreadBase::removeTaskObserver(TaskObserver* observer) { |
50 CHECK(isCurrentThread()); | 50 CHECK(isCurrentThread()); |
51 TaskObserverMap::iterator iter = task_observer_map_.find(observer); | 51 TaskObserverMap::iterator iter = task_observer_map_.find(observer); |
52 if (iter == task_observer_map_.end()) | 52 if (iter == task_observer_map_.end()) |
53 return; | 53 return; |
54 base::MessageLoop::current()->RemoveTaskObserver(iter->second); | 54 RemoveTaskObserverInternal(iter->second); |
55 delete iter->second; | 55 delete iter->second; |
56 task_observer_map_.erase(iter); | 56 task_observer_map_.erase(iter); |
57 } | 57 } |
58 | 58 |
59 void WebThreadBase::AddTaskObserverInternal( | |
60 base::MessageLoop::TaskObserver* observer) { | |
61 base::MessageLoop::current()->AddTaskObserver(observer); | |
alex clarke (OOO till 29th)
2015/02/13 11:02:07
Do we need DCHECK(main_thread_checker_.CalledOnVal
| |
62 } | |
63 | |
64 void WebThreadBase::RemoveTaskObserverInternal( | |
65 base::MessageLoop::TaskObserver* observer) { | |
66 base::MessageLoop::current()->RemoveTaskObserver(observer); | |
67 } | |
68 | |
59 WebThreadImpl::WebThreadImpl(const char* name) | 69 WebThreadImpl::WebThreadImpl(const char* name) |
60 : thread_(new base::Thread(name)) { | 70 : thread_(new base::Thread(name)) { |
61 thread_->Start(); | 71 thread_->Start(); |
62 } | 72 } |
63 | 73 |
64 // RunWebThreadTask takes the ownership of |task| from base::Closure and | 74 // RunWebThreadTask takes the ownership of |task| from base::Closure and |
65 // deletes it on the first invocation of the closure for thread-safety. | 75 // deletes it on the first invocation of the closure for thread-safety. |
66 // base::Closure made from RunWebThreadTask is copyable but Closure::Run | 76 // base::Closure made from RunWebThreadTask is copyable but Closure::Run |
67 // should be called at most only once. | 77 // should be called at most only once. |
68 // This is because WebThread::Task can contain RefPtr to a | 78 // This is because WebThread::Task can contain RefPtr to a |
69 // thread-unsafe-reference-counted object (e.g. WorkerThreadTask can contain | 79 // thread-unsafe-reference-counted object (e.g. WorkerThreadTask can contain |
70 // RefPtr to WebKit's StringImpl), and if we don't delete |task| here, | 80 // RefPtr to WebKit's StringImpl), and if we don't delete |task| here, |
71 // it causes a race condition as follows: | 81 // it causes a race condition as follows: |
72 // [A] In task->run(), more RefPtr's to the refcounted object can be created, | 82 // [A] In task->run(), more RefPtr's to the refcounted object can be created, |
73 // and the reference counter of the object can be modified via these | 83 // and the reference counter of the object can be modified via these |
74 // RefPtr's (as intended) on the thread where the task is executed. | 84 // RefPtr's (as intended) on the thread where the task is executed. |
75 // [B] However, base::Closure still retains the ownership of WebThread::Task | 85 // [B] However, base::Closure still retains the ownership of WebThread::Task |
76 // even after RunWebThreadTask is called. | 86 // even after RunWebThreadTask is called. |
77 // When base::Closure is deleted, WebThread::Task is deleted and the | 87 // When base::Closure is deleted, WebThread::Task is deleted and the |
78 // reference counter of the object is decreased by one, possibly from a | 88 // reference counter of the object is decreased by one, possibly from a |
79 // different thread from [A], which is a race condition. | 89 // different thread from [A], which is a race condition. |
80 // Taking the ownership of |task| here by using scoped_ptr and base::Passed | 90 // Taking the ownership of |task| here by using scoped_ptr and base::Passed |
81 // removes the reference counter modification of [B] and the race condition. | 91 // removes the reference counter modification of [B] and the race condition. |
82 // When the closure never runs at all, the corresponding WebThread::Task is | 92 // When the closure never runs at all, the corresponding WebThread::Task is |
83 // destructed when base::Closure is deleted (like [B]). In this case, there | 93 // destructed when base::Closure is deleted (like [B]). In this case, there |
84 // are no reference counter modification like [A] (because task->run() is not | 94 // are no reference counter modification like [A] (because task->run() is not |
85 // executed), so there are no race conditions. | 95 // executed), so there are no race conditions. |
86 // See https://crbug.com/390851 for more details. | 96 // See https://crbug.com/390851 for more details. |
87 static void RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) { | 97 // |
98 // static | |
99 void WebThreadBase::RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) { | |
88 task->run(); | 100 task->run(); |
89 } | 101 } |
90 | 102 |
91 void WebThreadImpl::postTask(Task* task) { | 103 void WebThreadImpl::postTask(Task* task) { |
92 postDelayedTask(task, 0); | 104 postDelayedTask(task, 0); |
93 } | 105 } |
94 | 106 |
95 void WebThreadImpl::postTask(const blink::WebTraceLocation& location, | 107 void WebThreadImpl::postTask(const blink::WebTraceLocation& location, |
96 Task* task) { | 108 Task* task) { |
97 postDelayedTask(location, task, 0); | 109 postDelayedTask(location, task, 0); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
192 return owning_thread_task_runner_->BelongsToCurrentThread(); | 204 return owning_thread_task_runner_->BelongsToCurrentThread(); |
193 } | 205 } |
194 | 206 |
195 blink::PlatformThreadId WebThreadImplForMessageLoop::threadId() const { | 207 blink::PlatformThreadId WebThreadImplForMessageLoop::threadId() const { |
196 return thread_id_; | 208 return thread_id_; |
197 } | 209 } |
198 | 210 |
199 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {} | 211 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {} |
200 | 212 |
201 } // namespace content | 213 } // namespace content |
OLD | NEW |