OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple 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 | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
51 #include "wtf/Noncopyable.h" | 51 #include "wtf/Noncopyable.h" |
52 #include "wtf/WeakPtr.h" | 52 #include "wtf/WeakPtr.h" |
53 #include "wtf/text/WTFString.h" | 53 #include "wtf/text/WTFString.h" |
54 | 54 |
55 namespace blink { | 55 namespace blink { |
56 | 56 |
57 namespace { | 57 namespace { |
58 const int64_t kShortIdleHandlerDelayMs = 1000; | 58 const int64_t kShortIdleHandlerDelayMs = 1000; |
59 const int64_t kLongIdleHandlerDelayMs = 10*1000; | 59 const int64_t kLongIdleHandlerDelayMs = 10*1000; |
60 | 60 |
61 class MicrotaskRunner : public WebThread::TaskObserver { | 61 } // namespace |
62 | |
63 class WorkerMicrotaskRunner : public WebThread::TaskObserver { | |
62 public: | 64 public: |
63 explicit MicrotaskRunner(WorkerThread* workerThread) | 65 explicit WorkerMicrotaskRunner(WorkerThread* workerThread) |
64 : m_workerThread(workerThread) | 66 : m_workerThread(workerThread) |
65 { | 67 { |
66 } | 68 } |
67 | 69 |
68 virtual void willProcessTask() override { } | 70 virtual void willProcessTask() override |
71 { | |
72 // No tasks should get executed after we have closed. | |
73 WorkerGlobalScope* globalScope = m_workerThread->workerGlobalScope(); | |
74 ASSERT_UNUSED(globalScope, !globalScope || !globalScope->isClosing()); | |
75 } | |
76 | |
69 virtual void didProcessTask() override | 77 virtual void didProcessTask() override |
70 { | 78 { |
71 Microtask::performCheckpoint(); | 79 Microtask::performCheckpoint(); |
72 if (WorkerGlobalScope* globalScope = m_workerThread->workerGlobalScope() ) { | 80 if (WorkerGlobalScope* globalScope = m_workerThread->workerGlobalScope() ) { |
73 if (WorkerScriptController* scriptController = globalScope->script() ) | 81 if (WorkerScriptController* scriptController = globalScope->script() ) |
74 scriptController->rejectedPromises()->processQueue(); | 82 scriptController->rejectedPromises()->processQueue(); |
83 if (globalScope->isClosing()) { | |
84 m_workerThread->workerReportingProxy().workerGlobalScopeClosed() ; | |
85 m_workerThread->shutdown(); | |
86 } | |
75 } | 87 } |
76 } | 88 } |
77 | 89 |
78 private: | 90 private: |
79 // Thread owns the microtask runner; reference remains | 91 // Thread owns the microtask runner; reference remains |
80 // valid for the lifetime of this object. | 92 // valid for the lifetime of this object. |
81 WorkerThread* m_workerThread; | 93 WorkerThread* m_workerThread; |
82 }; | 94 }; |
83 | 95 |
84 } // namespace | |
85 | |
86 static Mutex& threadSetMutex() | 96 static Mutex& threadSetMutex() |
87 { | 97 { |
88 AtomicallyInitializedStaticReference(Mutex, mutex, new Mutex); | 98 AtomicallyInitializedStaticReference(Mutex, mutex, new Mutex); |
89 return mutex; | 99 return mutex; |
90 } | 100 } |
91 | 101 |
92 static HashSet<WorkerThread*>& workerThreads() | 102 static HashSet<WorkerThread*>& workerThreads() |
93 { | 103 { |
94 DEFINE_STATIC_LOCAL(HashSet<WorkerThread*>, threads, ()); | 104 DEFINE_STATIC_LOCAL(HashSet<WorkerThread*>, threads, ()); |
95 return threads; | 105 return threads; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 static PassOwnPtr<WorkerThreadTask> create(WorkerThread& workerThread, PassO wnPtr<ExecutionContextTask> task, bool isInstrumented) | 211 static PassOwnPtr<WorkerThreadTask> create(WorkerThread& workerThread, PassO wnPtr<ExecutionContextTask> task, bool isInstrumented) |
202 { | 212 { |
203 return adoptPtr(new WorkerThreadTask(workerThread, task, isInstrumented) ); | 213 return adoptPtr(new WorkerThreadTask(workerThread, task, isInstrumented) ); |
204 } | 214 } |
205 | 215 |
206 virtual ~WorkerThreadTask() { } | 216 virtual ~WorkerThreadTask() { } |
207 | 217 |
208 virtual void run() override | 218 virtual void run() override |
209 { | 219 { |
210 WorkerGlobalScope* workerGlobalScope = m_workerThread.workerGlobalScope( ); | 220 WorkerGlobalScope* workerGlobalScope = m_workerThread.workerGlobalScope( ); |
211 // Tasks could be put on the message loop after the cleanup task, | 221 // If the thread is terminated before it had a chance initialize (see |
212 // ensure none of those are ran. | 222 // WorkerThread::Initialize()), we mustn't run any of the posted tasks. |
213 if (!workerGlobalScope) | 223 if (!workerGlobalScope) { |
224 ASSERT(m_workerThread.terminated()); | |
214 return; | 225 return; |
226 } | |
215 | 227 |
216 if (m_isInstrumented) | 228 if (m_isInstrumented) |
217 InspectorInstrumentation::willPerformExecutionContextTask(workerGlob alScope, m_task.get()); | 229 InspectorInstrumentation::willPerformExecutionContextTask(workerGlob alScope, m_task.get()); |
218 if ((!workerGlobalScope->isClosing() && !m_workerThread.terminated()) || m_task->isCleanupTask()) | 230 m_task->performTask(workerGlobalScope); |
219 m_task->performTask(workerGlobalScope); | |
220 if (m_isInstrumented) | 231 if (m_isInstrumented) |
221 InspectorInstrumentation::didPerformExecutionContextTask(workerGloba lScope); | 232 InspectorInstrumentation::didPerformExecutionContextTask(workerGloba lScope); |
222 } | 233 } |
223 | 234 |
224 private: | 235 private: |
225 WorkerThreadTask(WorkerThread& workerThread, PassOwnPtr<ExecutionContextTask > task, bool isInstrumented) | 236 WorkerThreadTask(WorkerThread& workerThread, PassOwnPtr<ExecutionContextTask > task, bool isInstrumented) |
226 : m_workerThread(workerThread) | 237 : m_workerThread(workerThread) |
227 , m_task(task) | 238 , m_task(task) |
228 , m_isInstrumented(isInstrumented) | 239 , m_isInstrumented(isInstrumented) |
229 { | 240 { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
302 | 313 |
303 void WorkerThread::initialize() | 314 void WorkerThread::initialize() |
304 { | 315 { |
305 KURL scriptURL = m_startupData->m_scriptURL; | 316 KURL scriptURL = m_startupData->m_scriptURL; |
306 String sourceCode = m_startupData->m_sourceCode; | 317 String sourceCode = m_startupData->m_sourceCode; |
307 WorkerThreadStartMode startMode = m_startupData->m_startMode; | 318 WorkerThreadStartMode startMode = m_startupData->m_startMode; |
308 OwnPtr<Vector<char>> cachedMetaData = m_startupData->m_cachedMetaData.releas e(); | 319 OwnPtr<Vector<char>> cachedMetaData = m_startupData->m_cachedMetaData.releas e(); |
309 V8CacheOptions v8CacheOptions = m_startupData->m_v8CacheOptions; | 320 V8CacheOptions v8CacheOptions = m_startupData->m_v8CacheOptions; |
310 | 321 |
311 { | 322 { |
312 MutexLocker lock(m_threadCreationMutex); | 323 MutexLocker lock(m_threadStateMutex); |
313 | 324 |
314 // The worker was terminated before the thread had a chance to run. | 325 // The worker was terminated before the thread had a chance to run. |
315 if (m_terminated) { | 326 if (m_terminated) { |
316 // Notify the proxy that the WorkerGlobalScope has been disposed of. | 327 // Notify the proxy that the WorkerGlobalScope has been disposed of. |
317 // This can free this thread object, hence it must not be touched af terwards. | 328 // This can free this thread object, hence it must not be touched af terwards. |
318 m_workerReportingProxy.workerThreadTerminated(); | 329 m_workerReportingProxy.workerThreadTerminated(); |
319 return; | 330 return; |
320 } | 331 } |
321 | 332 |
322 m_microtaskRunner = adoptPtr(new MicrotaskRunner(this)); | 333 m_microtaskRunner = adoptPtr(new WorkerMicrotaskRunner(this)); |
323 backingThread().addTaskObserver(m_microtaskRunner.get()); | 334 backingThread().addTaskObserver(m_microtaskRunner.get()); |
324 backingThread().attachGC(); | 335 backingThread().initialize(); |
325 | 336 |
326 m_isolate = initializeIsolate(); | 337 m_isolate = initializeIsolate(); |
327 m_workerGlobalScope = createWorkerGlobalScope(m_startupData.release()); | 338 m_workerGlobalScope = createWorkerGlobalScope(m_startupData.release()); |
328 m_workerGlobalScope->scriptLoaded(sourceCode.length(), cachedMetaData.ge t() ? cachedMetaData->size() : 0); | 339 m_workerGlobalScope->scriptLoaded(sourceCode.length(), cachedMetaData.ge t() ? cachedMetaData->size() : 0); |
329 | 340 |
330 PlatformThreadData::current().threadTimers().setSharedTimer(adoptPtr(new WorkerSharedTimer(this))); | 341 PlatformThreadData::current().threadTimers().setSharedTimer(adoptPtr(new WorkerSharedTimer(this))); |
331 } | 342 } |
332 | 343 |
333 // The corresponding call to stopRunLoop() is in ~WorkerScriptController(). | 344 // The corresponding call to stopRunLoop() is in ~WorkerScriptController(). |
334 didStartRunLoop(); | 345 didStartRunLoop(); |
(...skipping 10 matching lines...) Expand all Loading... | |
345 OwnPtr<CachedMetadataHandler> handler(workerGlobalScope()->createWorkerScrip tCachedMetadataHandler(scriptURL, cachedMetaData.get())); | 356 OwnPtr<CachedMetadataHandler> handler(workerGlobalScope()->createWorkerScrip tCachedMetadataHandler(scriptURL, cachedMetaData.get())); |
346 bool success = script->evaluate(ScriptSourceCode(sourceCode, scriptURL), nul lptr, handler.get(), v8CacheOptions); | 357 bool success = script->evaluate(ScriptSourceCode(sourceCode, scriptURL), nul lptr, handler.get(), v8CacheOptions); |
347 m_workerGlobalScope->didEvaluateWorkerScript(); | 358 m_workerGlobalScope->didEvaluateWorkerScript(); |
348 m_workerReportingProxy.didEvaluateWorkerScript(success); | 359 m_workerReportingProxy.didEvaluateWorkerScript(success); |
349 | 360 |
350 postInitialize(); | 361 postInitialize(); |
351 | 362 |
352 postDelayedTask(FROM_HERE, createSameThreadTask(&WorkerThread::idleHandler, this), kShortIdleHandlerDelayMs); | 363 postDelayedTask(FROM_HERE, createSameThreadTask(&WorkerThread::idleHandler, this), kShortIdleHandlerDelayMs); |
353 } | 364 } |
354 | 365 |
355 void WorkerThread::cleanup() | 366 void WorkerThread::shutdown() |
356 { | 367 { |
368 MutexLocker lock(m_threadStateMutex); | |
369 ASSERT(isCurrentThread()); | |
370 | |
371 PlatformThreadData::current().threadTimers().setSharedTimer(nullptr); | |
372 workerGlobalScope()->dispose(); | |
373 willDestroyIsolate(); | |
374 | |
357 // This should be called before we start the shutdown procedure. | 375 // This should be called before we start the shutdown procedure. |
358 workerReportingProxy().willDestroyWorkerGlobalScope(); | 376 workerReportingProxy().willDestroyWorkerGlobalScope(); |
359 | 377 |
360 // The below assignment will destroy the context, which will in turn notify messaging proxy. | 378 // The below assignment will destroy the context, which will in turn notify messaging proxy. |
361 // We cannot let any objects survive past thread exit, because no other thre ad will run GC or otherwise destroy them. | 379 // We cannot let any objects survive past thread exit, because no other thre ad will run GC or otherwise destroy them. |
362 // If Oilpan is enabled, we detach of the context/global scope, with the fin al heap cleanup below sweeping it out. | 380 // If Oilpan is enabled, we detach of the context/global scope, with the fin al heap cleanup below sweeping it out. |
363 #if !ENABLE(OILPAN) | 381 #if !ENABLE(OILPAN) |
364 ASSERT(m_workerGlobalScope->hasOneRef()); | 382 ASSERT(m_workerGlobalScope->hasOneRef()); |
365 #endif | 383 #endif |
366 m_workerGlobalScope->notifyContextDestroyed(); | 384 m_workerGlobalScope->notifyContextDestroyed(); |
367 m_workerGlobalScope = nullptr; | 385 m_workerGlobalScope = nullptr; |
368 | 386 |
369 backingThread().detachGC(); | 387 backingThread().removeTaskObserver(m_microtaskRunner.get()); |
388 backingThread().shutdown(); | |
370 destroyIsolate(); | 389 destroyIsolate(); |
371 | 390 |
372 backingThread().removeTaskObserver(m_microtaskRunner.get()); | |
373 m_microtaskRunner = nullptr; | 391 m_microtaskRunner = nullptr; |
374 | 392 |
375 // Notify the proxy that the WorkerGlobalScope has been disposed of. | 393 // Notify the proxy that the WorkerGlobalScope has been disposed of. |
376 // This can free this thread object, hence it must not be touched afterwards . | 394 // This can free this thread object, hence it must not be touched afterwards . |
377 workerReportingProxy().workerThreadTerminated(); | 395 workerReportingProxy().workerThreadTerminated(); |
378 | 396 |
379 m_terminationEvent->signal(); | 397 m_terminationEvent->signal(); |
380 | 398 |
381 // Clean up PlatformThreadData before WTF::WTFThreadData goes away! | 399 // Clean up PlatformThreadData before WTF::WTFThreadData goes away! |
382 PlatformThreadData::current().destroy(); | 400 PlatformThreadData::current().destroy(); |
383 } | 401 } |
384 | 402 |
385 class WorkerThreadShutdownFinishTask : public ExecutionContextTask { | |
386 public: | |
387 static PassOwnPtr<WorkerThreadShutdownFinishTask> create() | |
388 { | |
389 return adoptPtr(new WorkerThreadShutdownFinishTask()); | |
390 } | |
391 | |
392 virtual void performTask(ExecutionContext *context) | |
393 { | |
394 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); | |
395 workerGlobalScope->dispose(); | |
396 | |
397 WorkerThread* workerThread = workerGlobalScope->thread(); | |
398 workerThread->willDestroyIsolate(); | |
399 workerThread->backingThread().postTask(FROM_HERE, new Task(WTF::bind(&Wo rkerThread::cleanup, workerThread))); | |
400 } | |
401 | |
402 virtual bool isCleanupTask() const { return true; } | |
403 }; | |
404 | |
405 class WorkerThreadShutdownStartTask : public ExecutionContextTask { | |
406 public: | |
407 static PassOwnPtr<WorkerThreadShutdownStartTask> create() | |
408 { | |
409 return adoptPtr(new WorkerThreadShutdownStartTask()); | |
410 } | |
411 | |
412 virtual void performTask(ExecutionContext *context) | |
413 { | |
414 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); | |
415 workerGlobalScope->stopActiveDOMObjects(); | |
416 PlatformThreadData::current().threadTimers().setSharedTimer(nullptr); | |
417 | |
418 // Event listeners would keep DOMWrapperWorld objects alive for too long . Also, they have references to JS objects, | |
419 // which become dangling once Heap is destroyed. | |
420 workerGlobalScope->removeAllEventListeners(); | |
421 | |
422 // Stick a shutdown command at the end of the queue, so that we deal | |
423 // with all the cleanup tasks the databases post first. | |
424 workerGlobalScope->postTask(FROM_HERE, WorkerThreadShutdownFinishTask::c reate()); | |
425 } | |
426 | |
427 virtual bool isCleanupTask() const { return true; } | |
428 }; | |
429 | 403 |
430 void WorkerThread::stop() | 404 void WorkerThread::stop() |
431 { | 405 { |
432 // Prevent the deadlock between GC and an attempt to stop a thread. | 406 // Prevent the deadlock between GC and an attempt to stop a thread. |
433 SafePointScope safePointScope(ThreadState::HeapPointersOnStack); | 407 SafePointScope safePointScope(ThreadState::HeapPointersOnStack); |
434 stopInternal(); | 408 stopInternal(); |
435 } | 409 } |
436 | 410 |
437 void WorkerThread::stopInShutdownSequence() | 411 void WorkerThread::stopInShutdownSequence() |
438 { | 412 { |
439 stopInternal(); | 413 stopInternal(); |
440 } | 414 } |
441 | 415 |
442 void WorkerThread::terminateAndWait() | 416 void WorkerThread::terminateAndWait() |
443 { | 417 { |
444 stop(); | 418 stop(); |
445 m_terminationEvent->wait(); | 419 m_terminationEvent->wait(); |
446 } | 420 } |
447 | 421 |
448 bool WorkerThread::terminated() | 422 bool WorkerThread::terminated() |
449 { | 423 { |
450 MutexLocker lock(m_threadCreationMutex); | 424 MutexLocker lock(m_threadStateMutex); |
451 return m_terminated; | 425 return m_terminated; |
452 } | 426 } |
453 | 427 |
454 void WorkerThread::stopInternal() | 428 void WorkerThread::stopInternal() |
455 { | 429 { |
456 // Protect against this method and initialize() racing each other. | 430 // Protect against this method, initialize() or termination via the global s cope racing each other. |
457 MutexLocker lock(m_threadCreationMutex); | 431 MutexLocker lock(m_threadStateMutex); |
458 | 432 |
459 // If stop has already been called, just return. | 433 // If stop has already been called, just return. |
460 if (m_terminated) | 434 if (m_terminated) |
461 return; | 435 return; |
462 m_terminated = true; | 436 m_terminated = true; |
463 | 437 |
464 // Signal the thread to notify that the thread's stopping. | 438 // Signal the thread to notify that the thread's stopping. |
465 if (m_shutdownEvent) | 439 if (m_shutdownEvent) |
466 m_shutdownEvent->signal(); | 440 m_shutdownEvent->signal(); |
467 | 441 |
468 if (!m_workerGlobalScope) | 442 if (!m_workerGlobalScope) |
kinuko
2015/05/14 11:27:40
Um... it's not really related to this patch, but i
Sami
2015/05/14 11:32:17
Yes, I believe that would lead to a deadlock. Let
| |
469 return; | 443 return; |
470 | 444 |
471 // Ensure that tasks are being handled by thread event loop. If script execu tion weren't forbidden, a while(1) loop in JS could keep the thread alive foreve r. | 445 // Ensure that tasks are being handled by thread event loop. If script execu tion weren't forbidden, a while(1) loop in JS could keep the thread alive foreve r. |
472 terminateV8Execution(); | 446 terminateV8Execution(); |
473 | 447 |
474 InspectorInstrumentation::didKillAllExecutionContextTasks(m_workerGlobalScop e.get()); | 448 InspectorInstrumentation::didKillAllExecutionContextTasks(m_workerGlobalScop e.get()); |
475 m_debuggerMessageQueue.kill(); | 449 m_debuggerMessageQueue.kill(); |
476 postTask(FROM_HERE, WorkerThreadShutdownStartTask::create()); | 450 backingThread().postTask(FROM_HERE, new Task(threadSafeBind(&WorkerThread::s hutdown, AllowCrossThreadAccess(this)))); |
477 } | 451 } |
478 | 452 |
479 void WorkerThread::didStartRunLoop() | 453 void WorkerThread::didStartRunLoop() |
480 { | 454 { |
481 ASSERT(isCurrentThread()); | 455 ASSERT(isCurrentThread()); |
482 Platform::current()->didStartWorkerRunLoop(); | 456 Platform::current()->didStartWorkerRunLoop(); |
483 } | 457 } |
484 | 458 |
485 void WorkerThread::didStopRunLoop() | 459 void WorkerThread::didStopRunLoop() |
486 { | 460 { |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
606 InspectorInstrumentation::didLeaveNestedRunLoop(m_workerGlobalScope.get()); | 580 InspectorInstrumentation::didLeaveNestedRunLoop(m_workerGlobalScope.get()); |
607 } | 581 } |
608 | 582 |
609 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke rInspectorController) | 583 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke rInspectorController) |
610 { | 584 { |
611 MutexLocker locker(m_workerInspectorControllerMutex); | 585 MutexLocker locker(m_workerInspectorControllerMutex); |
612 m_workerInspectorController = workerInspectorController; | 586 m_workerInspectorController = workerInspectorController; |
613 } | 587 } |
614 | 588 |
615 } // namespace blink | 589 } // namespace blink |
OLD | NEW |