Chromium Code Reviews| Index: third_party/WebKit/Source/core/workers/WorkerBackingThread.cpp |
| diff --git a/third_party/WebKit/Source/core/workers/WorkerBackingThread.cpp b/third_party/WebKit/Source/core/workers/WorkerBackingThread.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cfa7f87181463921a7841b7a2ab661c7c9121284 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/workers/WorkerBackingThread.cpp |
| @@ -0,0 +1,107 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "core/workers/WorkerBackingThread.h" |
| + |
| +#include "bindings/core/v8/V8Binding.h" |
| +#include "bindings/core/v8/V8GCController.h" |
| +#include "bindings/core/v8/V8IdleTaskRunner.h" |
| +#include "bindings/core/v8/V8Initializer.h" |
| +#include "bindings/core/v8/V8PerIsolateData.h" |
| +#include "platform/RuntimeEnabledFeatures.h" |
| +#include "platform/ThreadSafeFunctional.h" |
| +#include "platform/WaitableEvent.h" |
| +#include "platform/WebThreadSupportingGC.h" |
| +#include "public/platform/Platform.h" |
| +#include "public/platform/WebTraceLocation.h" |
| + |
| +namespace blink { |
| + |
| +namespace { |
| + |
| +void noop(PassOwnPtr<WebThreadSupportingGC> thread) {} |
| + |
| +} // namespace |
| + |
| +WorkerBackingThread::WorkerBackingThread(const char* name) |
| + : m_backingThread(WebThreadSupportingGC::create(name)) |
| + , m_isOwningThread(true) |
| +{ |
| +} |
| + |
| +WorkerBackingThread::WorkerBackingThread(WebThread* thread) |
| + : m_backingThread(WebThreadSupportingGC::createForThread(thread)) |
| + , m_isOwningThread(false) |
| +{ |
| +} |
| + |
| +WorkerBackingThread::~WorkerBackingThread() |
| +{ |
| + ASSERT(!workerScriptCount()); |
| + if (!isMainThread()) { |
| + // We need to destroy the backing thread in the main thread. |
| + Platform::current()->mainThread()->taskRunner()->postTask(BLINK_FROM_HERE, threadSafeBind(&noop, m_backingThread.release())); |
|
kinuko
2016/02/29 09:32:53
So we make this class thread-safe-refcounted and p
yhirano
2016/02/29 23:47:32
When the WebThreadSuppotingGC changes are landed w
|
| + } |
| +} |
| + |
| +void WorkerBackingThread::attach() |
| +{ |
| + ASSERT(m_backingThread->isCurrentThread()); |
| + { |
| + MutexLocker locker(m_mutex); |
| + if (++m_workerScriptCount > 1) |
| + return; |
| + } |
| + initialize(); |
| +} |
| + |
| +void WorkerBackingThread::detach() |
| +{ |
| + ASSERT(m_backingThread->isCurrentThread()); |
| + { |
| + MutexLocker locker(m_mutex); |
| + if (--m_workerScriptCount > 0) |
| + return; |
| + } |
| + shutdown(); |
| +} |
| + |
| +void WorkerBackingThread::setInitializationEventForTest() |
| +{ |
| + m_initializationEventForTest = adoptPtr(new WaitableEvent); |
| +} |
| + |
| +void WorkerBackingThread::initialize() |
| +{ |
| + ASSERT(m_backingThread->isCurrentThread()); |
| + ASSERT(!m_isolate); |
| + m_backingThread->initialize(); |
| + m_isolate = V8PerIsolateData::initialize(); |
| + V8Initializer::initializeWorker(m_isolate); |
| + |
| + OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterruptor(m_isolate)); |
| + ThreadState::current()->addInterruptor(interruptor.release()); |
| + ThreadState::current()->registerTraceDOMWrappers(m_isolate, V8GCController::traceDOMWrappers); |
| + if (RuntimeEnabledFeatures::v8IdleTasksEnabled()) |
| + V8PerIsolateData::enableIdleTasks(m_isolate, adoptPtr(new V8IdleTaskRunner(backingThread().platformThread().scheduler()))); |
| + if (m_isOwningThread) |
| + Platform::current()->didStartWorkerThread(); |
| + |
| + if (m_initializationEventForTest) |
| + m_initializationEventForTest->signal(); |
| +} |
| + |
| +void WorkerBackingThread::shutdown() |
| +{ |
| + ASSERT(m_backingThread->isCurrentThread()); |
| + if (m_isOwningThread) |
| + Platform::current()->willStopWorkerThread(); |
| + |
| + V8PerIsolateData::willBeDestroyed(m_isolate); |
| + m_backingThread->shutdown(); |
| + V8PerIsolateData::destroy(m_isolate); |
| + m_isolate = nullptr; |
| +} |
| + |
| +} // namespace blink |