Chromium Code Reviews| 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 28 matching lines...) Expand all Loading... | |
| 39 #include "core/workers/WorkerReportingProxy.h" | 39 #include "core/workers/WorkerReportingProxy.h" |
| 40 #include "core/workers/WorkerThreadStartupData.h" | 40 #include "core/workers/WorkerThreadStartupData.h" |
| 41 #include "platform/PlatformThreadData.h" | 41 #include "platform/PlatformThreadData.h" |
| 42 #include "platform/Task.h" | 42 #include "platform/Task.h" |
| 43 #include "platform/ThreadSafeFunctional.h" | 43 #include "platform/ThreadSafeFunctional.h" |
| 44 #include "platform/ThreadTimers.h" | 44 #include "platform/ThreadTimers.h" |
| 45 #include "platform/heap/SafePoint.h" | 45 #include "platform/heap/SafePoint.h" |
| 46 #include "platform/heap/ThreadState.h" | 46 #include "platform/heap/ThreadState.h" |
| 47 #include "platform/weborigin/KURL.h" | 47 #include "platform/weborigin/KURL.h" |
| 48 #include "public/platform/Platform.h" | 48 #include "public/platform/Platform.h" |
| 49 #include "public/platform/WebScheduler.h" | |
| 49 #include "public/platform/WebThread.h" | 50 #include "public/platform/WebThread.h" |
| 50 #include "public/platform/WebWaitableEvent.h" | 51 #include "public/platform/WebWaitableEvent.h" |
| 51 #include "wtf/Noncopyable.h" | 52 #include "wtf/Noncopyable.h" |
| 52 #include "wtf/WeakPtr.h" | 53 #include "wtf/WeakPtr.h" |
| 53 #include "wtf/text/WTFString.h" | 54 #include "wtf/text/WTFString.h" |
| 54 | 55 |
| 55 namespace blink { | 56 namespace blink { |
| 56 | 57 |
| 57 namespace { | 58 namespace { |
| 58 const int64_t kShortIdleHandlerDelayMs = 1000; | 59 const int64_t kShortIdleHandlerDelayMs = 1000; |
| 59 const int64_t kLongIdleHandlerDelayMs = 10*1000; | 60 const int64_t kLongIdleHandlerDelayMs = 10*1000; |
| 60 | 61 |
| 61 class MicrotaskRunner : public WebThread::TaskObserver { | 62 } // namespace |
| 63 | |
| 64 class WorkerMicrotaskRunner : public WebThread::TaskObserver { | |
| 62 public: | 65 public: |
| 63 explicit MicrotaskRunner(WorkerThread* workerThread) | 66 explicit WorkerMicrotaskRunner(WorkerThread* workerThread) |
| 64 : m_workerThread(workerThread) | 67 : m_workerThread(workerThread) |
| 68 , m_wasClosed(false) | |
| 65 { | 69 { |
| 66 } | 70 } |
| 67 | 71 |
| 68 virtual void willProcessTask() override { } | 72 virtual void willProcessTask() override |
| 73 { | |
| 74 // No tasks should get executed after we have closed. | |
| 75 ASSERT(!m_wasClosed); | |
|
sadrul
2015/05/06 18:04:50
Would it make sense to check m_workerThread->worke
Sami
2015/05/07 14:20:14
I was worried that workerGlobalScope is null for t
| |
| 76 } | |
| 77 | |
| 69 virtual void didProcessTask() override | 78 virtual void didProcessTask() override |
| 70 { | 79 { |
| 71 Microtask::performCheckpoint(); | 80 Microtask::performCheckpoint(); |
| 72 if (WorkerGlobalScope* globalScope = m_workerThread->workerGlobalScope() ) { | 81 if (WorkerGlobalScope* globalScope = m_workerThread->workerGlobalScope() ) { |
| 73 if (WorkerScriptController* scriptController = globalScope->script() ) | 82 if (WorkerScriptController* scriptController = globalScope->script() ) |
| 74 scriptController->rejectedPromises()->processQueue(); | 83 scriptController->rejectedPromises()->processQueue(); |
| 84 if (globalScope->isClosing()) { | |
| 85 m_wasClosed = true; | |
| 86 m_workerThread->workerReportingProxy().workerGlobalScopeClosed() ; | |
| 87 m_workerThread->cleanup(); | |
| 88 } | |
| 75 } | 89 } |
| 76 } | 90 } |
| 77 | 91 |
| 78 private: | 92 private: |
| 79 // Thread owns the microtask runner; reference remains | 93 // Thread owns the microtask runner; reference remains |
| 80 // valid for the lifetime of this object. | 94 // valid for the lifetime of this object. |
| 81 WorkerThread* m_workerThread; | 95 WorkerThread* m_workerThread; |
| 96 | |
| 97 bool m_wasClosed; | |
| 82 }; | 98 }; |
| 83 | 99 |
| 84 } // namespace | |
| 85 | |
| 86 static Mutex& threadSetMutex() | 100 static Mutex& threadSetMutex() |
| 87 { | 101 { |
| 88 AtomicallyInitializedStaticReference(Mutex, mutex, new Mutex); | 102 AtomicallyInitializedStaticReference(Mutex, mutex, new Mutex); |
| 89 return mutex; | 103 return mutex; |
| 90 } | 104 } |
| 91 | 105 |
| 92 static HashSet<WorkerThread*>& workerThreads() | 106 static HashSet<WorkerThread*>& workerThreads() |
| 93 { | 107 { |
| 94 DEFINE_STATIC_LOCAL(HashSet<WorkerThread*>, threads, ()); | 108 DEFINE_STATIC_LOCAL(HashSet<WorkerThread*>, threads, ()); |
| 95 return threads; | 109 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) | 215 static PassOwnPtr<WorkerThreadTask> create(WorkerThread& workerThread, PassO wnPtr<ExecutionContextTask> task, bool isInstrumented) |
| 202 { | 216 { |
| 203 return adoptPtr(new WorkerThreadTask(workerThread, task, isInstrumented) ); | 217 return adoptPtr(new WorkerThreadTask(workerThread, task, isInstrumented) ); |
| 204 } | 218 } |
| 205 | 219 |
| 206 virtual ~WorkerThreadTask() { } | 220 virtual ~WorkerThreadTask() { } |
| 207 | 221 |
| 208 virtual void run() override | 222 virtual void run() override |
| 209 { | 223 { |
| 210 WorkerGlobalScope* workerGlobalScope = m_workerThread.workerGlobalScope( ); | 224 WorkerGlobalScope* workerGlobalScope = m_workerThread.workerGlobalScope( ); |
| 211 // Tasks could be put on the message loop after the cleanup task, | 225 ASSERT(workerGlobalScope); |
| 212 // ensure none of those are ran. | |
| 213 if (!workerGlobalScope) | |
| 214 return; | |
| 215 | 226 |
| 216 if (m_isInstrumented) | 227 if (m_isInstrumented) |
| 217 InspectorInstrumentation::willPerformExecutionContextTask(workerGlob alScope, m_task.get()); | 228 InspectorInstrumentation::willPerformExecutionContextTask(workerGlob alScope, m_task.get()); |
| 218 if ((!workerGlobalScope->isClosing() && !m_workerThread.terminated()) || m_task->isCleanupTask()) | 229 m_task->performTask(workerGlobalScope); |
| 219 m_task->performTask(workerGlobalScope); | |
| 220 if (m_isInstrumented) | 230 if (m_isInstrumented) |
| 221 InspectorInstrumentation::didPerformExecutionContextTask(workerGloba lScope); | 231 InspectorInstrumentation::didPerformExecutionContextTask(workerGloba lScope); |
| 222 } | 232 } |
| 223 | 233 |
| 224 private: | 234 private: |
| 225 WorkerThreadTask(WorkerThread& workerThread, PassOwnPtr<ExecutionContextTask > task, bool isInstrumented) | 235 WorkerThreadTask(WorkerThread& workerThread, PassOwnPtr<ExecutionContextTask > task, bool isInstrumented) |
| 226 : m_workerThread(workerThread) | 236 : m_workerThread(workerThread) |
| 227 , m_task(task) | 237 , m_task(task) |
| 228 , m_isInstrumented(isInstrumented) | 238 , m_isInstrumented(isInstrumented) |
| 229 { | 239 { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 312 MutexLocker lock(m_threadCreationMutex); | 322 MutexLocker lock(m_threadCreationMutex); |
| 313 | 323 |
| 314 // The worker was terminated before the thread had a chance to run. | 324 // The worker was terminated before the thread had a chance to run. |
| 315 if (m_terminated) { | 325 if (m_terminated) { |
| 316 // Notify the proxy that the WorkerGlobalScope has been disposed of. | 326 // 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. | 327 // This can free this thread object, hence it must not be touched af terwards. |
| 318 m_workerReportingProxy.workerThreadTerminated(); | 328 m_workerReportingProxy.workerThreadTerminated(); |
| 319 return; | 329 return; |
| 320 } | 330 } |
| 321 | 331 |
| 322 m_microtaskRunner = adoptPtr(new MicrotaskRunner(this)); | 332 m_microtaskRunner = adoptPtr(new WorkerMicrotaskRunner(this)); |
| 323 backingThread().addTaskObserver(m_microtaskRunner.get()); | 333 backingThread().addTaskObserver(m_microtaskRunner.get()); |
| 324 backingThread().attachGC(); | 334 backingThread().attachGC(); |
| 325 | 335 |
| 326 m_isolate = initializeIsolate(); | 336 m_isolate = initializeIsolate(); |
| 327 m_workerGlobalScope = createWorkerGlobalScope(m_startupData.release()); | 337 m_workerGlobalScope = createWorkerGlobalScope(m_startupData.release()); |
| 328 m_workerGlobalScope->scriptLoaded(sourceCode.length(), cachedMetaData.ge t() ? cachedMetaData->size() : 0); | 338 m_workerGlobalScope->scriptLoaded(sourceCode.length(), cachedMetaData.ge t() ? cachedMetaData->size() : 0); |
| 329 | 339 |
| 330 PlatformThreadData::current().threadTimers().setSharedTimer(adoptPtr(new WorkerSharedTimer(this))); | 340 PlatformThreadData::current().threadTimers().setSharedTimer(adoptPtr(new WorkerSharedTimer(this))); |
| 331 } | 341 } |
| 332 | 342 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 347 m_workerGlobalScope->didEvaluateWorkerScript(); | 357 m_workerGlobalScope->didEvaluateWorkerScript(); |
| 348 m_workerReportingProxy.didEvaluateWorkerScript(success); | 358 m_workerReportingProxy.didEvaluateWorkerScript(success); |
| 349 | 359 |
| 350 postInitialize(); | 360 postInitialize(); |
| 351 | 361 |
| 352 postDelayedTask(FROM_HERE, createSameThreadTask(&WorkerThread::idleHandler, this), kShortIdleHandlerDelayMs); | 362 postDelayedTask(FROM_HERE, createSameThreadTask(&WorkerThread::idleHandler, this), kShortIdleHandlerDelayMs); |
| 353 } | 363 } |
| 354 | 364 |
| 355 void WorkerThread::cleanup() | 365 void WorkerThread::cleanup() |
| 356 { | 366 { |
| 367 MutexLocker lock(m_threadCreationMutex); | |
| 368 ASSERT(isCurrentThread()); | |
| 369 workerGlobalScope()->stopActiveDOMObjects(); | |
| 370 PlatformThreadData::current().threadTimers().setSharedTimer(nullptr); | |
| 371 | |
| 372 // Event listeners would keep DOMWrapperWorld objects alive for too long. Al so, they have references to JS objects, | |
| 373 // which become dangling once Heap is destroyed. | |
| 374 workerGlobalScope()->removeAllEventListeners(); | |
| 375 workerGlobalScope()->dispose(); | |
|
sadrul
2015/05/06 18:04:50
Can stopActiveDOMObjects() and removeAllEventListe
Sami
2015/05/07 14:20:14
Good idea, done.
| |
| 376 | |
| 377 willDestroyIsolate(); | |
| 378 | |
| 357 // This should be called before we start the shutdown procedure. | 379 // This should be called before we start the shutdown procedure. |
| 358 workerReportingProxy().willDestroyWorkerGlobalScope(); | 380 workerReportingProxy().willDestroyWorkerGlobalScope(); |
| 359 | 381 |
| 360 // The below assignment will destroy the context, which will in turn notify messaging proxy. | 382 // 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. | 383 // 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. | 384 // 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) | 385 #if !ENABLE(OILPAN) |
| 364 ASSERT(m_workerGlobalScope->hasOneRef()); | 386 ASSERT(m_workerGlobalScope->hasOneRef()); |
| 365 #endif | 387 #endif |
| 366 m_workerGlobalScope->notifyContextDestroyed(); | 388 m_workerGlobalScope->notifyContextDestroyed(); |
| 367 m_workerGlobalScope = nullptr; | 389 m_workerGlobalScope = nullptr; |
| 368 | 390 |
| 369 backingThread().detachGC(); | 391 backingThread().detachGC(); |
| 370 destroyIsolate(); | 392 destroyIsolate(); |
| 371 | 393 |
| 372 backingThread().removeTaskObserver(m_microtaskRunner.get()); | 394 backingThread().removeTaskObserver(m_microtaskRunner.get()); |
| 373 m_microtaskRunner = nullptr; | 395 m_microtaskRunner = nullptr; |
| 374 | 396 |
| 397 // Ensure no posted tasks will run from this point on. | |
| 398 backingThread().platformThread().scheduler()->shutdown(); | |
|
sadrul
2015/05/06 18:04:50
Can this move into WebThreadSuppportingGC? For exa
Sami
2015/05/07 14:20:14
Yes, that definitely sounds more maintainable. I'v
| |
| 399 | |
| 375 // Notify the proxy that the WorkerGlobalScope has been disposed of. | 400 // Notify the proxy that the WorkerGlobalScope has been disposed of. |
| 376 // This can free this thread object, hence it must not be touched afterwards . | 401 // This can free this thread object, hence it must not be touched afterwards . |
| 377 workerReportingProxy().workerThreadTerminated(); | 402 workerReportingProxy().workerThreadTerminated(); |
| 378 | 403 |
| 379 m_terminationEvent->signal(); | 404 m_terminationEvent->signal(); |
| 380 | 405 |
| 381 // Clean up PlatformThreadData before WTF::WTFThreadData goes away! | 406 // Clean up PlatformThreadData before WTF::WTFThreadData goes away! |
| 382 PlatformThreadData::current().destroy(); | 407 PlatformThreadData::current().destroy(); |
| 383 } | 408 } |
| 384 | 409 |
| 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 | 410 |
| 430 void WorkerThread::stop() | 411 void WorkerThread::stop() |
| 431 { | 412 { |
| 432 // Prevent the deadlock between GC and an attempt to stop a thread. | 413 // Prevent the deadlock between GC and an attempt to stop a thread. |
| 433 SafePointScope safePointScope(ThreadState::HeapPointersOnStack); | 414 SafePointScope safePointScope(ThreadState::HeapPointersOnStack); |
| 434 stopInternal(); | 415 stopInternal(); |
| 435 } | 416 } |
| 436 | 417 |
| 437 void WorkerThread::stopInShutdownSequence() | 418 void WorkerThread::stopInShutdownSequence() |
| 438 { | 419 { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 466 m_shutdownEvent->signal(); | 447 m_shutdownEvent->signal(); |
| 467 | 448 |
| 468 if (!m_workerGlobalScope) | 449 if (!m_workerGlobalScope) |
| 469 return; | 450 return; |
| 470 | 451 |
| 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. | 452 // 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(); | 453 terminateV8Execution(); |
| 473 | 454 |
| 474 InspectorInstrumentation::didKillAllExecutionContextTasks(m_workerGlobalScop e.get()); | 455 InspectorInstrumentation::didKillAllExecutionContextTasks(m_workerGlobalScop e.get()); |
| 475 m_debuggerMessageQueue.kill(); | 456 m_debuggerMessageQueue.kill(); |
| 476 postTask(FROM_HERE, WorkerThreadShutdownStartTask::create()); | 457 backingThread().postTask(FROM_HERE, new Task(threadSafeBind(&WorkerThread::c leanup, AllowCrossThreadAccess(this)))); |
| 477 } | 458 } |
| 478 | 459 |
| 479 void WorkerThread::didStartRunLoop() | 460 void WorkerThread::didStartRunLoop() |
| 480 { | 461 { |
| 481 ASSERT(isCurrentThread()); | 462 ASSERT(isCurrentThread()); |
| 482 Platform::current()->didStartWorkerRunLoop(); | 463 Platform::current()->didStartWorkerRunLoop(); |
| 483 } | 464 } |
| 484 | 465 |
| 485 void WorkerThread::didStopRunLoop() | 466 void WorkerThread::didStopRunLoop() |
| 486 { | 467 { |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 606 InspectorInstrumentation::didLeaveNestedRunLoop(m_workerGlobalScope.get()); | 587 InspectorInstrumentation::didLeaveNestedRunLoop(m_workerGlobalScope.get()); |
| 607 } | 588 } |
| 608 | 589 |
| 609 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke rInspectorController) | 590 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke rInspectorController) |
| 610 { | 591 { |
| 611 MutexLocker locker(m_workerInspectorControllerMutex); | 592 MutexLocker locker(m_workerInspectorControllerMutex); |
| 612 m_workerInspectorController = workerInspectorController; | 593 m_workerInspectorController = workerInspectorController; |
| 613 } | 594 } |
| 614 | 595 |
| 615 } // namespace blink | 596 } // namespace blink |
| OLD | NEW |