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

Side by Side Diff: third_party/WebKit/Source/web/WebKit.cpp

Issue 1761853002: Shut down all threads before the main thread shuts down (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 static GCTaskRunner* s_gcTaskRunner = nullptr; 89 static GCTaskRunner* s_gcTaskRunner = nullptr;
90 90
91 // Make sure we are not re-initialized in the same address space. 91 // Make sure we are not re-initialized in the same address space.
92 // Doing so may cause hard to reproduce crashes. 92 // Doing so may cause hard to reproduce crashes.
93 static bool s_webKitInitialized = false; 93 static bool s_webKitInitialized = false;
94 94
95 void initialize(Platform* platform) 95 void initialize(Platform* platform)
96 { 96 {
97 initializeWithoutV8(platform); 97 initializeWithoutV8(platform);
98 98
99 V8Initializer::initializeMainThreadIfNeeded(); 99 V8Initializer::initializeMainThreadIfNeeded();
haraken 2016/03/03 12:08:46 This should be renamed to V8Initializer::initializ
100 100
101 OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterruptor (V8PerIsolateData::mainThreadIsolate())); 101 OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterruptor (V8PerIsolateData::mainThreadIsolate()));
102 ThreadState::current()->addInterruptor(interruptor.release()); 102 ThreadState::current()->addInterruptor(interruptor.release());
103 ThreadState::current()->registerTraceDOMWrappers(V8PerIsolateData::mainThrea dIsolate(), V8GCController::traceDOMWrappers); 103 ThreadState::current()->registerTraceDOMWrappers(V8PerIsolateData::mainThrea dIsolate(), V8GCController::traceDOMWrappers);
104 104
105 // currentThread is null if we are running on a thread without a message loo p. 105 // currentThread is null if we are running on a thread without a message loo p.
106 if (WebThread* currentThread = platform->currentThread()) { 106 if (WebThread* currentThread = platform->currentThread()) {
107 ASSERT(!s_endOfTaskRunner); 107 ASSERT(!s_endOfTaskRunner);
108 s_endOfTaskRunner = new EndOfTaskRunner; 108 s_endOfTaskRunner = new EndOfTaskRunner;
109 currentThread->addTaskObserver(s_endOfTaskRunner); 109 currentThread->addTaskObserver(s_endOfTaskRunner);
(...skipping 24 matching lines...) Expand all
134 static void callOnMainThreadFunction(WTF::MainThreadFunction function, void* con text) 134 static void callOnMainThreadFunction(WTF::MainThreadFunction function, void* con text)
135 { 135 {
136 Platform::current()->mainThread()->getWebTaskRunner()->postTask(BLINK_FROM_H ERE, threadSafeBind(function, AllowCrossThreadAccess(context))); 136 Platform::current()->mainThread()->getWebTaskRunner()->postTask(BLINK_FROM_H ERE, threadSafeBind(function, AllowCrossThreadAccess(context)));
137 } 137 }
138 138
139 static void adjustAmountOfExternalAllocatedMemory(int size) 139 static void adjustAmountOfExternalAllocatedMemory(int size)
140 { 140 {
141 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(size); 141 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(size);
142 } 142 }
143 143
144 static ModulesInitializer& modulesInitializer()
145 {
146 DEFINE_STATIC_LOCAL(OwnPtr<ModulesInitializer>, initializer, (adoptPtr(new M odulesInitializer)));
147 return *initializer;
148 }
149
144 void initializeWithoutV8(Platform* platform) 150 void initializeWithoutV8(Platform* platform)
145 { 151 {
146 ASSERT(!s_webKitInitialized); 152 ASSERT(!s_webKitInitialized);
147 s_webKitInitialized = true; 153 s_webKitInitialized = true;
148 154
149 WTF::Partitions::initialize(maxObservedSizeFunction); 155 WTF::Partitions::initialize(maxObservedSizeFunction);
150 ASSERT(platform); 156 ASSERT(platform);
151 Platform::initialize(platform); 157 Platform::initialize(platform);
152 158
153 WTF::initialize(adjustAmountOfExternalAllocatedMemory); 159 WTF::initialize(adjustAmountOfExternalAllocatedMemory);
154 WTF::initializeMainThread(callOnMainThreadFunction); 160 WTF::initializeMainThread(callOnMainThreadFunction);
155 Heap::init(); 161 Heap::init();
156 162
157 ThreadState::attachMainThread(); 163 ThreadState::attachMainThread();
158 // currentThread() is null if we are running on a thread without a message l oop. 164 // currentThread() is null if we are running on a thread without a message l oop.
159 if (WebThread* currentThread = platform->currentThread()) { 165 if (WebThread* currentThread = platform->currentThread()) {
160 ASSERT(!s_gcTaskRunner); 166 ASSERT(!s_gcTaskRunner);
161 s_gcTaskRunner = new GCTaskRunner(currentThread); 167 s_gcTaskRunner = new GCTaskRunner(currentThread);
162 } 168 }
163 169
164 DEFINE_STATIC_LOCAL(ModulesInitializer, initializer, ()); 170 modulesInitializer().init();
165 initializer.init();
166 171
167 setIndexedDBClientCreateFunction(IndexedDBClientImpl::create); 172 setIndexedDBClientCreateFunction(IndexedDBClientImpl::create);
168 } 173 }
169 174
170 void shutdown() 175 void shutdown()
171 { 176 {
172 #if defined(LEAK_SANITIZER) 177 #if defined(LEAK_SANITIZER)
173 // If LSan is about to perform leak detection, release all the registered 178 // If LSan is about to perform leak detection, release all the registered
174 // static Persistent<> root references to global caches that Blink keeps, 179 // static Persistent<> root references to global caches that Blink keeps,
175 // followed by GCs to clear out all they referred to. A full v8 GC cycle 180 // followed by GCs to clear out all they referred to. A full v8 GC cycle
(...skipping 11 matching lines...) Expand all
187 // currentThread() is null if we are running on a thread without a message l oop. 192 // currentThread() is null if we are running on a thread without a message l oop.
188 if (Platform::current()->currentThread()) { 193 if (Platform::current()->currentThread()) {
189 Platform::current()->unregisterMemoryDumpProvider(WebCacheMemoryDumpProv ider::instance()); 194 Platform::current()->unregisterMemoryDumpProvider(WebCacheMemoryDumpProv ider::instance());
190 Platform::current()->unregisterMemoryDumpProvider(FontCacheMemoryDumpPro vider::instance()); 195 Platform::current()->unregisterMemoryDumpProvider(FontCacheMemoryDumpPro vider::instance());
191 196
192 // We don't need to (cannot) remove s_endOfTaskRunner from the current 197 // We don't need to (cannot) remove s_endOfTaskRunner from the current
193 // message loop, because the message loop is already destructed before 198 // message loop, because the message loop is already destructed before
194 // the shutdown() is called. 199 // the shutdown() is called.
195 delete s_endOfTaskRunner; 200 delete s_endOfTaskRunner;
196 s_endOfTaskRunner = nullptr; 201 s_endOfTaskRunner = nullptr;
197
198 ASSERT(s_gcTaskRunner);
199 delete s_gcTaskRunner;
200 s_gcTaskRunner = nullptr;
201 } 202 }
202 203
203 // Shutdown V8-related background threads before V8 is ramped down. Note 204 // Shutdown V8-related background threads before V8 is ramped down. Note
204 // that this will wait the thread to stop its operations. 205 // that this will wait the thread to stop its operations.
205 ScriptStreamerThread::shutdown(); 206 ScriptStreamerThread::shutdown();
206 207
208 ThreadState::current()->unregisterTraceDOMWrappers();
209
207 v8::Isolate* isolate = V8PerIsolateData::mainThreadIsolate(); 210 v8::Isolate* isolate = V8PerIsolateData::mainThreadIsolate();
208 V8PerIsolateData::willBeDestroyed(isolate); 211 V8PerIsolateData::willBeDestroyed(isolate);
209 212
210 CoreInitializer::terminateThreads();
211
212 ModulesInitializer::terminateThreads();
213
214 // Detach the main thread before starting the shutdown sequence
215 // so that the main thread won't get involved in a GC during the shutdown.
216 ThreadState::detachMainThread();
217
218 V8PerIsolateData::destroy(isolate); 213 V8PerIsolateData::destroy(isolate);
haraken 2016/03/03 12:08:46 Line 210 - 213 should be moved to V8Initializer::s
219 214
220 shutdownWithoutV8(); 215 shutdownWithoutV8();
221 } 216 }
222 217
223 void shutdownWithoutV8() 218 void shutdownWithoutV8()
yhirano 2016/03/03 18:03:11 I would like to guarantee that all non-main thread
haraken 2016/03/03 23:47:37 No, that is the key part of this CL. For example,
yhirano 2016/03/03 23:58:17 modulesInitializer().init() creates a HTMLParserTh
224 { 219 {
220 modulesInitializer().shutdown();
221
222 // currentThread() is null if we are running on a thread without a message l oop.
223 if (Platform::current()->currentThread()) {
224 ASSERT(s_gcTaskRunner);
225 delete s_gcTaskRunner;
226 s_gcTaskRunner = nullptr;
227 }
228
229 // Detach the main thread before starting the shutdown sequence
230 // so that the main thread won't get involved in a GC during the shutdown.
231 ThreadState::detachMainThread();
232
225 ASSERT(!s_endOfTaskRunner); 233 ASSERT(!s_endOfTaskRunner);
226 CoreInitializer::shutdown();
227 Heap::shutdown(); 234 Heap::shutdown();
228 WTF::shutdown(); 235 WTF::shutdown();
229 Platform::shutdown(); 236 Platform::shutdown();
230 WebPrerenderingSupport::shutdown(); 237 WebPrerenderingSupport::shutdown();
231 WTF::Partitions::shutdown(); 238 WTF::Partitions::shutdown();
232 } 239 }
233 240
234 // TODO(tkent): The following functions to wrap LayoutTestSupport should be 241 // TODO(tkent): The following functions to wrap LayoutTestSupport should be
235 // moved to public/platform/. 242 // moved to public/platform/.
236 243
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 ASSERT(!reloadPages); 290 ASSERT(!reloadPages);
284 Page::refreshPlugins(); 291 Page::refreshPlugins();
285 } 292 }
286 293
287 void decommitFreeableMemory() 294 void decommitFreeableMemory()
288 { 295 {
289 WTF::Partitions::decommitFreeableMemory(); 296 WTF::Partitions::decommitFreeableMemory();
290 } 297 }
291 298
292 } // namespace blink 299 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698