Chromium Code Reviews| Index: Source/core/workers/WorkerThread.cpp |
| diff --git a/Source/core/workers/WorkerThread.cpp b/Source/core/workers/WorkerThread.cpp |
| index d8f75cc105c1285c6f3df3d80327ff396782a07a..3c6069347b7edc609eb8952607ae040a3527aefd 100644 |
| --- a/Source/core/workers/WorkerThread.cpp |
| +++ b/Source/core/workers/WorkerThread.cpp |
| @@ -46,6 +46,7 @@ |
| #include "platform/heap/ThreadState.h" |
| #include "platform/weborigin/KURL.h" |
| #include "public/platform/Platform.h" |
| +#include "public/platform/WebScheduler.h" |
| #include "public/platform/WebThread.h" |
| #include "public/platform/WebWaitableEvent.h" |
| #include "wtf/Noncopyable.h" |
| @@ -58,20 +59,33 @@ namespace { |
| const int64_t kShortIdleHandlerDelayMs = 1000; |
| const int64_t kLongIdleHandlerDelayMs = 10*1000; |
| -class MicrotaskRunner : public WebThread::TaskObserver { |
| +} // namespace |
| + |
| +class WorkerMicrotaskRunner : public WebThread::TaskObserver { |
| public: |
| - explicit MicrotaskRunner(WorkerThread* workerThread) |
| + explicit WorkerMicrotaskRunner(WorkerThread* workerThread) |
| : m_workerThread(workerThread) |
| + , m_wasClosed(false) |
| + { |
| + } |
| + |
| + virtual void willProcessTask() override |
| { |
| + // No tasks should get executed after we have closed. |
| + 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
|
| } |
| - virtual void willProcessTask() override { } |
| virtual void didProcessTask() override |
| { |
| Microtask::performCheckpoint(); |
| if (WorkerGlobalScope* globalScope = m_workerThread->workerGlobalScope()) { |
| if (WorkerScriptController* scriptController = globalScope->script()) |
| scriptController->rejectedPromises()->processQueue(); |
| + if (globalScope->isClosing()) { |
| + m_wasClosed = true; |
| + m_workerThread->workerReportingProxy().workerGlobalScopeClosed(); |
| + m_workerThread->cleanup(); |
| + } |
| } |
| } |
| @@ -79,9 +93,9 @@ private: |
| // Thread owns the microtask runner; reference remains |
| // valid for the lifetime of this object. |
| WorkerThread* m_workerThread; |
| -}; |
| -} // namespace |
| + bool m_wasClosed; |
| +}; |
| static Mutex& threadSetMutex() |
| { |
| @@ -208,15 +222,11 @@ public: |
| virtual void run() override |
| { |
| WorkerGlobalScope* workerGlobalScope = m_workerThread.workerGlobalScope(); |
| - // Tasks could be put on the message loop after the cleanup task, |
| - // ensure none of those are ran. |
| - if (!workerGlobalScope) |
| - return; |
| + ASSERT(workerGlobalScope); |
| if (m_isInstrumented) |
| InspectorInstrumentation::willPerformExecutionContextTask(workerGlobalScope, m_task.get()); |
| - if ((!workerGlobalScope->isClosing() && !m_workerThread.terminated()) || m_task->isCleanupTask()) |
| - m_task->performTask(workerGlobalScope); |
| + m_task->performTask(workerGlobalScope); |
| if (m_isInstrumented) |
| InspectorInstrumentation::didPerformExecutionContextTask(workerGlobalScope); |
| } |
| @@ -319,7 +329,7 @@ void WorkerThread::initialize() |
| return; |
| } |
| - m_microtaskRunner = adoptPtr(new MicrotaskRunner(this)); |
| + m_microtaskRunner = adoptPtr(new WorkerMicrotaskRunner(this)); |
| backingThread().addTaskObserver(m_microtaskRunner.get()); |
| backingThread().attachGC(); |
| @@ -354,6 +364,18 @@ void WorkerThread::initialize() |
| void WorkerThread::cleanup() |
| { |
| + MutexLocker lock(m_threadCreationMutex); |
| + ASSERT(isCurrentThread()); |
| + workerGlobalScope()->stopActiveDOMObjects(); |
| + PlatformThreadData::current().threadTimers().setSharedTimer(nullptr); |
| + |
| + // Event listeners would keep DOMWrapperWorld objects alive for too long. Also, they have references to JS objects, |
| + // which become dangling once Heap is destroyed. |
| + workerGlobalScope()->removeAllEventListeners(); |
| + workerGlobalScope()->dispose(); |
|
sadrul
2015/05/06 18:04:50
Can stopActiveDOMObjects() and removeAllEventListe
Sami
2015/05/07 14:20:14
Good idea, done.
|
| + |
| + willDestroyIsolate(); |
| + |
| // This should be called before we start the shutdown procedure. |
| workerReportingProxy().willDestroyWorkerGlobalScope(); |
| @@ -372,6 +394,9 @@ void WorkerThread::cleanup() |
| backingThread().removeTaskObserver(m_microtaskRunner.get()); |
| m_microtaskRunner = nullptr; |
| + // Ensure no posted tasks will run from this point on. |
| + 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
|
| + |
| // Notify the proxy that the WorkerGlobalScope has been disposed of. |
| // This can free this thread object, hence it must not be touched afterwards. |
| workerReportingProxy().workerThreadTerminated(); |
| @@ -382,50 +407,6 @@ void WorkerThread::cleanup() |
| PlatformThreadData::current().destroy(); |
| } |
| -class WorkerThreadShutdownFinishTask : public ExecutionContextTask { |
| -public: |
| - static PassOwnPtr<WorkerThreadShutdownFinishTask> create() |
| - { |
| - return adoptPtr(new WorkerThreadShutdownFinishTask()); |
| - } |
| - |
| - virtual void performTask(ExecutionContext *context) |
| - { |
| - WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); |
| - workerGlobalScope->dispose(); |
| - |
| - WorkerThread* workerThread = workerGlobalScope->thread(); |
| - workerThread->willDestroyIsolate(); |
| - workerThread->backingThread().postTask(FROM_HERE, new Task(WTF::bind(&WorkerThread::cleanup, workerThread))); |
| - } |
| - |
| - virtual bool isCleanupTask() const { return true; } |
| -}; |
| - |
| -class WorkerThreadShutdownStartTask : public ExecutionContextTask { |
| -public: |
| - static PassOwnPtr<WorkerThreadShutdownStartTask> create() |
| - { |
| - return adoptPtr(new WorkerThreadShutdownStartTask()); |
| - } |
| - |
| - virtual void performTask(ExecutionContext *context) |
| - { |
| - WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); |
| - workerGlobalScope->stopActiveDOMObjects(); |
| - PlatformThreadData::current().threadTimers().setSharedTimer(nullptr); |
| - |
| - // Event listeners would keep DOMWrapperWorld objects alive for too long. Also, they have references to JS objects, |
| - // which become dangling once Heap is destroyed. |
| - workerGlobalScope->removeAllEventListeners(); |
| - |
| - // Stick a shutdown command at the end of the queue, so that we deal |
| - // with all the cleanup tasks the databases post first. |
| - workerGlobalScope->postTask(FROM_HERE, WorkerThreadShutdownFinishTask::create()); |
| - } |
| - |
| - virtual bool isCleanupTask() const { return true; } |
| -}; |
| void WorkerThread::stop() |
| { |
| @@ -473,7 +454,7 @@ void WorkerThread::stopInternal() |
| InspectorInstrumentation::didKillAllExecutionContextTasks(m_workerGlobalScope.get()); |
| m_debuggerMessageQueue.kill(); |
| - postTask(FROM_HERE, WorkerThreadShutdownStartTask::create()); |
| + backingThread().postTask(FROM_HERE, new Task(threadSafeBind(&WorkerThread::cleanup, AllowCrossThreadAccess(this)))); |
| } |
| void WorkerThread::didStartRunLoop() |