Index: third_party/WebKit/Source/core/workers/WorkerThread.cpp |
diff --git a/third_party/WebKit/Source/core/workers/WorkerThread.cpp b/third_party/WebKit/Source/core/workers/WorkerThread.cpp |
index bb5bb46c7708830080177d81d4d618f2c919daf0..f4aa897af0dab13d80b1eb9f82de4e4c4452dc01 100644 |
--- a/third_party/WebKit/Source/core/workers/WorkerThread.cpp |
+++ b/third_party/WebKit/Source/core/workers/WorkerThread.cpp |
@@ -27,13 +27,11 @@ |
#include "core/workers/WorkerThread.h" |
#include "bindings/core/v8/ScriptSourceCode.h" |
-#include "bindings/core/v8/V8GCController.h" |
-#include "bindings/core/v8/V8IdleTaskRunner.h" |
-#include "bindings/core/v8/V8Initializer.h" |
#include "core/dom/Microtask.h" |
#include "core/inspector/InspectorInstrumentation.h" |
#include "core/inspector/WorkerInspectorController.h" |
#include "core/workers/DedicatedWorkerGlobalScope.h" |
+#include "core/workers/WorkerBackingThread.h" |
#include "core/workers/WorkerClients.h" |
#include "core/workers/WorkerReportingProxy.h" |
#include "core/workers/WorkerThreadStartupData.h" |
@@ -42,7 +40,6 @@ |
#include "platform/heap/SafePoint.h" |
#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 "wtf/Functional.h" |
@@ -194,7 +191,7 @@ private: |
bool m_killed = false; |
}; |
-WorkerThread::WorkerThread(PassRefPtr<WorkerLoaderProxy> workerLoaderProxy, WorkerReportingProxy& workerReportingProxy) |
+WorkerThread::WorkerThread(PassRefPtr<WorkerLoaderProxy> workerLoaderProxy, WorkerReportingProxy& workerReportingProxy, PassRefPtr<WorkerBackingThread> thread) |
: m_started(false) |
, m_terminated(false) |
, m_shutdown(false) |
@@ -202,7 +199,7 @@ WorkerThread::WorkerThread(PassRefPtr<WorkerLoaderProxy> workerLoaderProxy, Work |
, m_workerLoaderProxy(workerLoaderProxy) |
, m_workerReportingProxy(workerReportingProxy) |
, m_webScheduler(nullptr) |
- , m_isolate(nullptr) |
+ , m_workerBackingThread(thread) |
, m_shutdownEvent(adoptPtr(new WaitableEvent( |
WaitableEvent::ResetPolicy::Manual, |
WaitableEvent::InitialState::NonSignaled))) |
@@ -268,18 +265,16 @@ void WorkerThread::initialize(PassOwnPtr<WorkerThreadStartupData> startupData) |
return; |
} |
+ m_workerBackingThread->attach(); |
+ |
m_microtaskRunner = adoptPtr(new WorkerMicrotaskRunner(this)); |
- initializeBackingThread(); |
backingThread().addTaskObserver(m_microtaskRunner.get()); |
- m_isolate = initializeIsolate(); |
// Optimize for memory usage instead of latency for the worker isolate. |
- m_isolate->IsolateInBackgroundNotification(); |
+ isolate()->IsolateInBackgroundNotification(); |
m_workerGlobalScope = createWorkerGlobalScope(startupData); |
m_workerGlobalScope->scriptLoaded(sourceCode.length(), cachedMetaData.get() ? cachedMetaData->size() : 0); |
- didStartWorkerThread(); |
- |
// Notify proxy that a new WorkerGlobalScope has been created and started. |
m_workerReportingProxy.workerGlobalScopeStarted(m_workerGlobalScope.get()); |
@@ -316,10 +311,6 @@ void WorkerThread::shutdown() |
workerGlobalScope()->dispose(); |
- // This should be called after the WorkerGlobalScope's disposed (which may |
- // trigger some last-minutes cleanups) and before the thread actually stops. |
- willStopWorkerThread(); |
- |
backingThread().removeTaskObserver(m_microtaskRunner.get()); |
postTask(BLINK_FROM_HERE, createSameThreadTask(&WorkerThread::performShutdownTask, this)); |
} |
@@ -335,11 +326,8 @@ void WorkerThread::performShutdownTask() |
m_workerGlobalScope->notifyContextDestroyed(); |
m_workerGlobalScope = nullptr; |
- willDestroyIsolate(); |
- shutdownBackingThread(); |
- destroyIsolate(); |
- m_isolate = nullptr; |
- |
+ m_workerBackingThread->detach(); |
+ m_workerBackingThread = nullptr; |
m_microtaskRunner = nullptr; |
// Notify the proxy that the WorkerGlobalScope has been disposed of. |
@@ -402,23 +390,36 @@ void WorkerThread::terminateInternal() |
// Ensure that tasks are being handled by thread event loop. If script execution weren't forbidden, a while(1) loop in JS could keep the thread alive forever. |
m_workerGlobalScope->scriptController()->willScheduleExecutionTermination(); |
- terminateV8Execution(); |
+ |
+ if (1 == m_workerBackingThread->workerScriptCount()) { |
+ // This condition is not entirely correct because other scripts |
+ // can be being initialized or terminated simuletaneously. Though this |
+ // function itself is protected by a mutex, it is possible that |
+ // |workerScriptCount()| here is not consistent with that in |
+ // |initialize| and |shutdown|. |
+ isolate()->TerminateExecution(); |
+ } |
kinuko
2016/02/29 09:32:53
The comment just leaves readers unsure if this cod
yhirano
2016/02/29 23:47:32
Imagine there are two WorkerThread x and y attache
yhirano
2016/03/01 00:04:52
Correction:
before x->shutdown() on the worker th
|
InspectorInstrumentation::didKillAllExecutionContextTasks(m_workerGlobalScope.get()); |
m_debuggerTaskQueue->kill(); |
backingThread().postTask(BLINK_FROM_HERE, threadSafeBind(&WorkerThread::shutdown, AllowCrossThreadAccess(this))); |
} |
-void WorkerThread::didStartWorkerThread() |
+WorkerBackingThread& WorkerThread::workerBackingThread() |
{ |
- ASSERT(isCurrentThread()); |
- Platform::current()->didStartWorkerThread(); |
+ ASSERT(m_workerBackingThread); |
+ return *m_workerBackingThread; |
} |
-void WorkerThread::willStopWorkerThread() |
+WebThreadSupportingGC& WorkerThread::backingThread() |
{ |
- ASSERT(isCurrentThread()); |
- Platform::current()->willStopWorkerThread(); |
+ ASSERT(m_workerBackingThread); |
+ return m_workerBackingThread->backingThread(); |
+} |
+ |
+v8::Isolate* WorkerThread::isolate() const |
+{ |
+ return m_workerBackingThread->isolate(); |
} |
void WorkerThread::terminateAndWaitForAllWorkers() |
@@ -448,52 +449,6 @@ void WorkerThread::postDelayedTask(const WebTraceLocation& location, PassOwnPtr< |
backingThread().postDelayedTask(location, createWorkerThreadTask(task, true), delayMs); |
} |
-void WorkerThread::initializeBackingThread() |
-{ |
- ASSERT(isCurrentThread()); |
- backingThread().initialize(); |
-} |
- |
-void WorkerThread::shutdownBackingThread() |
-{ |
- ASSERT(isCurrentThread()); |
- backingThread().shutdown(); |
-} |
- |
-v8::Isolate* WorkerThread::initializeIsolate() |
-{ |
- ASSERT(isCurrentThread()); |
- ASSERT(!m_isolate); |
- v8::Isolate* isolate = V8PerIsolateData::initialize(); |
- V8Initializer::initializeWorker(isolate); |
- |
- OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterruptor(isolate)); |
- ThreadState::current()->addInterruptor(interruptor.release()); |
- ThreadState::current()->registerTraceDOMWrappers(isolate, V8GCController::traceDOMWrappers); |
- if (RuntimeEnabledFeatures::v8IdleTasksEnabled()) |
- V8PerIsolateData::enableIdleTasks(isolate, adoptPtr(new V8IdleTaskRunner(m_webScheduler))); |
- return isolate; |
-} |
- |
-void WorkerThread::willDestroyIsolate() |
-{ |
- ASSERT(isCurrentThread()); |
- ASSERT(m_isolate); |
- V8PerIsolateData::willBeDestroyed(m_isolate); |
-} |
- |
-void WorkerThread::destroyIsolate() |
-{ |
- ASSERT(isCurrentThread()); |
- V8PerIsolateData::destroy(m_isolate); |
-} |
- |
-void WorkerThread::terminateV8Execution() |
-{ |
- ASSERT(isMainThread()); |
- m_isolate->TerminateExecution(); |
-} |
- |
void WorkerThread::appendDebuggerTask(PassOwnPtr<Closure> task) |
{ |
{ |