Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(820)

Side by Side Diff: content/child/webthread_impl.cc

Issue 1088933002: Revert of Add a WorkerScheduler and a WebThreadImplForWorker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/child/webthread_impl.h ('k') | content/content_child.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 // An implementation of WebThread in terms of base::MessageLoop and
6 // base::Thread
7
8 #include "content/child/webthread_impl.h"
9
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/pending_task.h"
13 #include "base/threading/platform_thread.h"
14 #include "third_party/WebKit/public/platform/WebTraceLocation.h"
15
16 namespace content {
17
18 class WebThreadBase::TaskObserverAdapter
19 : public base::MessageLoop::TaskObserver {
20 public:
21 TaskObserverAdapter(WebThread::TaskObserver* observer)
22 : observer_(observer) {}
23
24 void WillProcessTask(const base::PendingTask& pending_task) override {
25 observer_->willProcessTask();
26 }
27
28 void DidProcessTask(const base::PendingTask& pending_task) override {
29 observer_->didProcessTask();
30 }
31
32 private:
33 WebThread::TaskObserver* observer_;
34 };
35
36 WebThreadBase::WebThreadBase() {
37 }
38
39 WebThreadBase::~WebThreadBase() {
40 for (auto& observer_entry : task_observer_map_) {
41 delete observer_entry.second;
42 }
43 }
44
45 void WebThreadBase::addTaskObserver(TaskObserver* observer) {
46 CHECK(isCurrentThread());
47 std::pair<TaskObserverMap::iterator, bool> result = task_observer_map_.insert(
48 std::make_pair(observer, static_cast<TaskObserverAdapter*>(NULL)));
49 if (result.second)
50 result.first->second = new TaskObserverAdapter(observer);
51 AddTaskObserverInternal(result.first->second);
52 }
53
54 void WebThreadBase::removeTaskObserver(TaskObserver* observer) {
55 CHECK(isCurrentThread());
56 TaskObserverMap::iterator iter = task_observer_map_.find(observer);
57 if (iter == task_observer_map_.end())
58 return;
59 RemoveTaskObserverInternal(iter->second);
60 delete iter->second;
61 task_observer_map_.erase(iter);
62 }
63
64 void WebThreadBase::AddTaskObserverInternal(
65 base::MessageLoop::TaskObserver* observer) {
66 base::MessageLoop::current()->AddTaskObserver(observer);
67 }
68
69 void WebThreadBase::RemoveTaskObserverInternal(
70 base::MessageLoop::TaskObserver* observer) {
71 base::MessageLoop::current()->RemoveTaskObserver(observer);
72 }
73
74 // RunWebThreadTask takes the ownership of |task| from base::Closure and
75 // deletes it on the first invocation of the closure for thread-safety.
76 // base::Closure made from RunWebThreadTask is copyable but Closure::Run
77 // should be called at most only once.
78 // This is because WebThread::Task can contain RefPtr to a
79 // thread-unsafe-reference-counted object (e.g. WorkerThreadTask can contain
80 // RefPtr to WebKit's StringImpl), and if we don't delete |task| here,
81 // it causes a race condition as follows:
82 // [A] In task->run(), more RefPtr's to the refcounted object can be created,
83 // and the reference counter of the object can be modified via these
84 // RefPtr's (as intended) on the thread where the task is executed.
85 // [B] However, base::Closure still retains the ownership of WebThread::Task
86 // even after RunWebThreadTask is called.
87 // When base::Closure is deleted, WebThread::Task is deleted and the
88 // reference counter of the object is decreased by one, possibly from a
89 // different thread from [A], which is a race condition.
90 // Taking the ownership of |task| here by using scoped_ptr and base::Passed
91 // removes the reference counter modification of [B] and the race condition.
92 // When the closure never runs at all, the corresponding WebThread::Task is
93 // destructed when base::Closure is deleted (like [B]). In this case, there
94 // are no reference counter modification like [A] (because task->run() is not
95 // executed), so there are no race conditions.
96 // See https://crbug.com/390851 for more details.
97 //
98 // static
99 void WebThreadBase::RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) {
100 task->run();
101 }
102
103 void WebThreadBase::postTask(const blink::WebTraceLocation& location,
104 Task* task) {
105 postDelayedTask(location, task, 0);
106 }
107
108 void WebThreadBase::postDelayedTask(const blink::WebTraceLocation& web_location,
109 Task* task,
110 long long delay_ms) {
111 tracked_objects::Location location(web_location.functionName(),
112 web_location.fileName(), -1, nullptr);
113 TaskRunner()->PostDelayedTask(
114 location,
115 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))),
116 base::TimeDelta::FromMilliseconds(delay_ms));
117 }
118
119 void WebThreadBase::enterRunLoop() {
120 CHECK(isCurrentThread());
121 CHECK(MessageLoop());
122 CHECK(!MessageLoop()->is_running()); // We don't support nesting.
123 MessageLoop()->Run();
124 }
125
126 void WebThreadBase::exitRunLoop() {
127 CHECK(isCurrentThread());
128 CHECK(MessageLoop());
129 CHECK(MessageLoop()->is_running());
130 MessageLoop()->Quit();
131 }
132
133 bool WebThreadBase::isCurrentThread() const {
134 return TaskRunner()->BelongsToCurrentThread();
135 }
136
137 blink::PlatformThreadId WebThreadImpl::threadId() const {
138 return thread_->thread_id();
139 }
140
141 WebThreadImpl::WebThreadImpl(const char* name)
142 : thread_(new base::Thread(name)) {
143 thread_->Start();
144 }
145
146 WebThreadImpl::~WebThreadImpl() {
147 thread_->Stop();
148 }
149
150 base::MessageLoop* WebThreadImpl::MessageLoop() const {
151 return nullptr;
152 }
153
154 base::SingleThreadTaskRunner* WebThreadImpl::TaskRunner() const {
155 return thread_->message_loop_proxy().get();
156 }
157
158 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webthread_impl.h ('k') | content/content_child.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698