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 if (WorkerGlobalScope* globalScope = m_workerThread->workerGlobalScope() ) | |
74 ASSERT_UNUSED(globalScope, !globalScope->isClosing()); | |
haraken
2015/05/11 13:06:46
Slightly more common:
WorkerGlobalScope* global
Sami
2015/05/11 13:15:04
Thanks, done!
| |
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 ASSERT(workerGlobalScope); |
212 // ensure none of those are ran. | |
213 if (!workerGlobalScope) | |
214 return; | |
215 | 222 |
216 if (m_isInstrumented) | 223 if (m_isInstrumented) |
217 InspectorInstrumentation::willPerformExecutionContextTask(workerGlob alScope, m_task.get()); | 224 InspectorInstrumentation::willPerformExecutionContextTask(workerGlob alScope, m_task.get()); |
218 if ((!workerGlobalScope->isClosing() && !m_workerThread.terminated()) || m_task->isCleanupTask()) | 225 m_task->performTask(workerGlobalScope); |
219 m_task->performTask(workerGlobalScope); | |
220 if (m_isInstrumented) | 226 if (m_isInstrumented) |
221 InspectorInstrumentation::didPerformExecutionContextTask(workerGloba lScope); | 227 InspectorInstrumentation::didPerformExecutionContextTask(workerGloba lScope); |
222 } | 228 } |
223 | 229 |
224 private: | 230 private: |
225 WorkerThreadTask(WorkerThread& workerThread, PassOwnPtr<ExecutionContextTask > task, bool isInstrumented) | 231 WorkerThreadTask(WorkerThread& workerThread, PassOwnPtr<ExecutionContextTask > task, bool isInstrumented) |
226 : m_workerThread(workerThread) | 232 : m_workerThread(workerThread) |
227 , m_task(task) | 233 , m_task(task) |
228 , m_isInstrumented(isInstrumented) | 234 , m_isInstrumented(isInstrumented) |
229 { | 235 { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
312 MutexLocker lock(m_threadCreationMutex); | 318 MutexLocker lock(m_threadCreationMutex); |
313 | 319 |
314 // The worker was terminated before the thread had a chance to run. | 320 // The worker was terminated before the thread had a chance to run. |
315 if (m_terminated) { | 321 if (m_terminated) { |
316 // Notify the proxy that the WorkerGlobalScope has been disposed of. | 322 // 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. | 323 // This can free this thread object, hence it must not be touched af terwards. |
318 m_workerReportingProxy.workerThreadTerminated(); | 324 m_workerReportingProxy.workerThreadTerminated(); |
319 return; | 325 return; |
320 } | 326 } |
321 | 327 |
322 m_microtaskRunner = adoptPtr(new MicrotaskRunner(this)); | 328 m_microtaskRunner = adoptPtr(new WorkerMicrotaskRunner(this)); |
323 backingThread().addTaskObserver(m_microtaskRunner.get()); | 329 backingThread().addTaskObserver(m_microtaskRunner.get()); |
324 backingThread().attachGC(); | 330 backingThread().initialize(); |
325 | 331 |
326 m_isolate = initializeIsolate(); | 332 m_isolate = initializeIsolate(); |
327 m_workerGlobalScope = createWorkerGlobalScope(m_startupData.release()); | 333 m_workerGlobalScope = createWorkerGlobalScope(m_startupData.release()); |
328 m_workerGlobalScope->scriptLoaded(sourceCode.length(), cachedMetaData.ge t() ? cachedMetaData->size() : 0); | 334 m_workerGlobalScope->scriptLoaded(sourceCode.length(), cachedMetaData.ge t() ? cachedMetaData->size() : 0); |
329 | 335 |
330 PlatformThreadData::current().threadTimers().setSharedTimer(adoptPtr(new WorkerSharedTimer(this))); | 336 PlatformThreadData::current().threadTimers().setSharedTimer(adoptPtr(new WorkerSharedTimer(this))); |
331 } | 337 } |
332 | 338 |
333 // The corresponding call to stopRunLoop() is in ~WorkerScriptController(). | 339 // The corresponding call to stopRunLoop() is in ~WorkerScriptController(). |
334 didStartRunLoop(); | 340 didStartRunLoop(); |
(...skipping 10 matching lines...) Expand all Loading... | |
345 OwnPtr<CachedMetadataHandler> handler(workerGlobalScope()->createWorkerScrip tCachedMetadataHandler(scriptURL, cachedMetaData.get())); | 351 OwnPtr<CachedMetadataHandler> handler(workerGlobalScope()->createWorkerScrip tCachedMetadataHandler(scriptURL, cachedMetaData.get())); |
346 bool success = script->evaluate(ScriptSourceCode(sourceCode, scriptURL), nul lptr, handler.get(), v8CacheOptions); | 352 bool success = script->evaluate(ScriptSourceCode(sourceCode, scriptURL), nul lptr, handler.get(), v8CacheOptions); |
347 m_workerGlobalScope->didEvaluateWorkerScript(); | 353 m_workerGlobalScope->didEvaluateWorkerScript(); |
348 m_workerReportingProxy.didEvaluateWorkerScript(success); | 354 m_workerReportingProxy.didEvaluateWorkerScript(success); |
349 | 355 |
350 postInitialize(); | 356 postInitialize(); |
351 | 357 |
352 postDelayedTask(FROM_HERE, createSameThreadTask(&WorkerThread::idleHandler, this), kShortIdleHandlerDelayMs); | 358 postDelayedTask(FROM_HERE, createSameThreadTask(&WorkerThread::idleHandler, this), kShortIdleHandlerDelayMs); |
353 } | 359 } |
354 | 360 |
355 void WorkerThread::cleanup() | 361 void WorkerThread::shutdown() |
356 { | 362 { |
363 MutexLocker lock(m_threadCreationMutex); | |
364 ASSERT(isCurrentThread()); | |
365 | |
366 PlatformThreadData::current().threadTimers().setSharedTimer(nullptr); | |
367 workerGlobalScope()->dispose(); | |
368 willDestroyIsolate(); | |
369 | |
357 // This should be called before we start the shutdown procedure. | 370 // This should be called before we start the shutdown procedure. |
358 workerReportingProxy().willDestroyWorkerGlobalScope(); | 371 workerReportingProxy().willDestroyWorkerGlobalScope(); |
359 | 372 |
360 // The below assignment will destroy the context, which will in turn notify messaging proxy. | 373 // 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. | 374 // 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. | 375 // 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) | 376 #if !ENABLE(OILPAN) |
364 ASSERT(m_workerGlobalScope->hasOneRef()); | 377 ASSERT(m_workerGlobalScope->hasOneRef()); |
365 #endif | 378 #endif |
366 m_workerGlobalScope->notifyContextDestroyed(); | 379 m_workerGlobalScope->notifyContextDestroyed(); |
367 m_workerGlobalScope = nullptr; | 380 m_workerGlobalScope = nullptr; |
368 | 381 |
369 backingThread().detachGC(); | 382 backingThread().removeTaskObserver(m_microtaskRunner.get()); |
383 backingThread().shutdown(); | |
370 destroyIsolate(); | 384 destroyIsolate(); |
371 | 385 |
372 backingThread().removeTaskObserver(m_microtaskRunner.get()); | |
373 m_microtaskRunner = nullptr; | 386 m_microtaskRunner = nullptr; |
374 | 387 |
375 // Notify the proxy that the WorkerGlobalScope has been disposed of. | 388 // Notify the proxy that the WorkerGlobalScope has been disposed of. |
376 // This can free this thread object, hence it must not be touched afterwards . | 389 // This can free this thread object, hence it must not be touched afterwards . |
377 workerReportingProxy().workerThreadTerminated(); | 390 workerReportingProxy().workerThreadTerminated(); |
378 | 391 |
379 m_terminationEvent->signal(); | 392 m_terminationEvent->signal(); |
380 | 393 |
381 // Clean up PlatformThreadData before WTF::WTFThreadData goes away! | 394 // Clean up PlatformThreadData before WTF::WTFThreadData goes away! |
382 PlatformThreadData::current().destroy(); | 395 PlatformThreadData::current().destroy(); |
383 } | 396 } |
384 | 397 |
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 | 398 |
430 void WorkerThread::stop() | 399 void WorkerThread::stop() |
431 { | 400 { |
432 // Prevent the deadlock between GC and an attempt to stop a thread. | 401 // Prevent the deadlock between GC and an attempt to stop a thread. |
433 SafePointScope safePointScope(ThreadState::HeapPointersOnStack); | 402 SafePointScope safePointScope(ThreadState::HeapPointersOnStack); |
434 stopInternal(); | 403 stopInternal(); |
435 } | 404 } |
436 | 405 |
437 void WorkerThread::stopInShutdownSequence() | 406 void WorkerThread::stopInShutdownSequence() |
438 { | 407 { |
(...skipping 27 matching lines...) Expand all Loading... | |
466 m_shutdownEvent->signal(); | 435 m_shutdownEvent->signal(); |
467 | 436 |
468 if (!m_workerGlobalScope) | 437 if (!m_workerGlobalScope) |
469 return; | 438 return; |
470 | 439 |
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. | 440 // 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(); | 441 terminateV8Execution(); |
473 | 442 |
474 InspectorInstrumentation::didKillAllExecutionContextTasks(m_workerGlobalScop e.get()); | 443 InspectorInstrumentation::didKillAllExecutionContextTasks(m_workerGlobalScop e.get()); |
475 m_debuggerMessageQueue.kill(); | 444 m_debuggerMessageQueue.kill(); |
476 postTask(FROM_HERE, WorkerThreadShutdownStartTask::create()); | 445 backingThread().postTask(FROM_HERE, new Task(threadSafeBind(&WorkerThread::s hutdown, AllowCrossThreadAccess(this)))); |
477 } | 446 } |
478 | 447 |
479 void WorkerThread::didStartRunLoop() | 448 void WorkerThread::didStartRunLoop() |
480 { | 449 { |
481 ASSERT(isCurrentThread()); | 450 ASSERT(isCurrentThread()); |
482 Platform::current()->didStartWorkerRunLoop(); | 451 Platform::current()->didStartWorkerRunLoop(); |
483 } | 452 } |
484 | 453 |
485 void WorkerThread::didStopRunLoop() | 454 void WorkerThread::didStopRunLoop() |
486 { | 455 { |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
606 InspectorInstrumentation::didLeaveNestedRunLoop(m_workerGlobalScope.get()); | 575 InspectorInstrumentation::didLeaveNestedRunLoop(m_workerGlobalScope.get()); |
607 } | 576 } |
608 | 577 |
609 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke rInspectorController) | 578 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke rInspectorController) |
610 { | 579 { |
611 MutexLocker locker(m_workerInspectorControllerMutex); | 580 MutexLocker locker(m_workerInspectorControllerMutex); |
612 m_workerInspectorController = workerInspectorController; | 581 m_workerInspectorController = workerInspectorController; |
613 } | 582 } |
614 | 583 |
615 } // namespace blink | 584 } // namespace blink |
OLD | NEW |