Chromium Code Reviews| Index: Source/modules/compositorworker/CompositorWorkerManager.cpp |
| diff --git a/Source/modules/compositorworker/CompositorWorkerManager.cpp b/Source/modules/compositorworker/CompositorWorkerManager.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bf77e265f9d4069814e4b619f7da8465daa9785f |
| --- /dev/null |
| +++ b/Source/modules/compositorworker/CompositorWorkerManager.cpp |
| @@ -0,0 +1,145 @@ |
| +// Copyright 2015 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 "config.h" |
| +#include "modules/compositorworker/CompositorWorkerManager.h" |
| + |
| +#include "bindings/core/v8/V8Binding.h" |
| +#include "bindings/core/v8/V8GCController.h" |
| +#include "bindings/core/v8/V8Initializer.h" |
| +#include "bindings/core/v8/V8PerIsolateData.h" |
| +#include "platform/ThreadSafeFunctional.h" |
| +#include "platform/WebThreadSupportingGC.h" |
| +#include "wtf/MainThread.h" |
| +#include "wtf/ThreadingPrimitives.h" |
| + |
| +namespace blink { |
| + |
| +namespace { |
| + |
| +static CompositorWorkerManager* s_instance = nullptr; |
| + |
| +static Mutex& singletonMutex() |
| +{ |
| + AtomicallyInitializedStaticReference(Mutex, mutex, new Mutex); |
| + return mutex; |
| +} |
| + |
| +static void destroyThread(WebThreadSupportingGC* thread) |
| +{ |
| + delete thread; |
| +} |
| + |
| +} // namespace |
| + |
| +void CompositorWorkerManager::initialize() |
| +{ |
| + MutexLocker lock(singletonMutex()); |
| + ASSERT(!s_instance); |
| + s_instance = new CompositorWorkerManager(); |
| +} |
| + |
| +void CompositorWorkerManager::shutdown() |
| +{ |
| + MutexLocker lock(singletonMutex()); |
| + ASSERT(s_instance); |
| + delete s_instance; |
| + s_instance = nullptr; |
| +} |
| + |
| +CompositorWorkerManager* CompositorWorkerManager::instance() |
| +{ |
| + MutexLocker lock(singletonMutex()); |
| + ASSERT(s_instance); |
| + return s_instance; |
| +} |
| + |
| +CompositorWorkerManager::CompositorWorkerManager() |
| +{ |
| +} |
| + |
| +CompositorWorkerManager::~CompositorWorkerManager() |
| +{ |
| +} |
| + |
| +WebThreadSupportingGC& CompositorWorkerManager::compositorWorkerThread() |
| +{ |
| + MutexLocker lock(m_mutex); |
| + if (!m_thread) { |
| + ASSERT(isMainThread()); |
| + ASSERT(!m_workerCount); |
| + m_thread = WebThreadSupportingGC::create("CompositorWorker Thread"); |
| + } |
| + return *m_thread.get(); |
| +} |
| + |
| +void CompositorWorkerManager::initializeBackingThread() |
| +{ |
| + ASSERT(m_thread->isCurrentThread()); |
| + MutexLocker lock(m_mutex); |
| + ++m_workerCount; |
| + if (m_workerCount == 1) |
| + m_thread->initialize(); |
| +} |
| + |
| +void CompositorWorkerManager::shutdownBackingThread() |
| +{ |
| + MutexLocker lock(m_mutex); |
| + ASSERT(m_thread->isCurrentThread()); |
| + ASSERT(m_workerCount > 0); |
| + --m_workerCount; |
| + if (m_workerCount == 0) { |
| + m_thread->shutdown(); |
| + Platform::current()->mainThread()->postTask(FROM_HERE, threadSafeBind(destroyThread, AllowCrossThreadAccess(m_thread.leakPtr()))); |
| + } |
| +} |
| + |
| +v8::Isolate* CompositorWorkerManager::initializedIsolate() |
| +{ |
| + MutexLocker lock(m_mutex); |
| + ASSERT(m_thread->isCurrentThread()); |
| + if (!m_isolate) { |
| + m_isolate = V8PerIsolateData::initialize(); |
|
haraken
2015/06/03 02:44:11
Can we do the isolate initialization in initialize
sadrul
2015/06/03 03:16:00
Done.
|
| + V8Initializer::initializeWorker(m_isolate); |
| + |
| + m_interruptor = adoptPtr(new V8IsolateInterruptor(m_isolate)); |
| + ThreadState::current()->addInterruptor(m_interruptor.get()); |
| + ThreadState::current()->registerTraceDOMWrappers(m_isolate, V8GCController::traceDOMWrappers); |
| + } |
| + // It is safe to use the existing isolate even if TerminateExecution() has been |
| + // called on it, without calling CancelTerminateExecution(). |
| + return m_isolate; |
| +} |
| + |
| +void CompositorWorkerManager::willDestroyIsolate() |
| +{ |
| + MutexLocker lock(m_mutex); |
| + ASSERT(m_thread->isCurrentThread()); |
| + if (m_workerCount == 1) { |
| + V8PerIsolateData::willBeDestroyed(m_isolate); |
| + ThreadState::current()->removeInterruptor(m_interruptor.get()); |
| + } |
| +} |
| + |
| +void CompositorWorkerManager::destroyIsolate() |
| +{ |
| + MutexLocker lock(m_mutex); |
| + if (!m_thread) { |
|
haraken
2015/06/03 02:44:11
What is this check for? Is it possible that m_thre
sadrul
2015/06/03 03:16:00
After calling m_thread->shutdown(), we schedule th
|
| + ASSERT(m_workerCount == 0); |
| + V8PerIsolateData::destroy(m_isolate); |
| + m_isolate = nullptr; |
| + } |
| +} |
| + |
| +void CompositorWorkerManager::terminateIsolate() |
| +{ |
| + MutexLocker lock(m_mutex); |
| + ASSERT(isMainThread()); |
| + if (m_workerCount > 1) |
| + return; |
| + |
| + v8::V8::TerminateExecution(m_isolate); |
| +} |
| + |
| +} // namespace blink |