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..a088343533fcd46836672bd855a16e6d01c17df0 |
| --- /dev/null |
| +++ b/Source/modules/compositorworker/CompositorWorkerManager.cpp |
| @@ -0,0 +1,156 @@ |
| +// 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 "platform/ThreadSafeFunctional.h" |
| +#include "platform/WebThreadSupportingGC.h" |
| +#include "wtf/MainThread.h" |
| +#include "wtf/ThreadingPrimitives.h" |
| + |
| +namespace blink { |
| + |
| +namespace { |
| + |
| +class SharedV8Isolate final : public WorkerIsolateWrapper { |
| + WTF_MAKE_NONCOPYABLE(SharedV8Isolate); |
| +public: |
| + explicit SharedV8Isolate(v8::Isolate* isolate) |
| + : m_isolate(isolate) { } |
| + ~SharedV8Isolate() override |
| + { |
| + CompositorWorkerManager::instance()->destroyIsolate(); |
| + } |
| + |
| +private: |
| + // WorkerIsolateWrapper: |
| + v8::Isolate* isolate() const override { return m_isolate; } |
| + void willDestroy() override |
| + { |
| + CompositorWorkerManager::instance()->willDestroyIsolate(); |
| + } |
| + |
| + void terminateExecution() override |
| + { |
| + CompositorWorkerManager::instance()->terminateIsolate(); |
| + } |
| + |
| + v8::Isolate* m_isolate; |
| +}; |
| + |
| +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()); |
| + if (s_instance) |
|
kinuko
2015/06/01 13:15:42
nit: could this be null?
sadrul
2015/06/01 14:21:16
We initialize conditionally on whether the feature
|
| + 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()))); |
| + } |
| +} |
| + |
| +PassOwnPtr<WorkerIsolateWrapper> CompositorWorkerManager::createIsolate() |
| +{ |
| + MutexLocker lock(m_mutex); |
| + ASSERT(m_thread->isCurrentThread()); |
| + if (!m_isolate) |
| + m_isolate = WorkerIsolateWrapper::createDefault(); |
| + else if (m_isolate->isolate()->IsExecutionTerminating()) |
| + m_isolate->isolate()->CancelTerminateExecution(); |
|
kinuko
2015/06/01 13:15:42
Was looking a recent bug that explains a bit about
sadrul
2015/06/01 14:21:16
Whoa. Thanks a lot for that link! Very informative
|
| + return adoptPtr(new SharedV8Isolate(m_isolate->isolate())); |
| +} |
| + |
| +void CompositorWorkerManager::willDestroyIsolate() |
| +{ |
| + MutexLocker lock(m_mutex); |
| + ASSERT(m_thread->isCurrentThread()); |
| + if (m_workerCount == 1) |
| + m_isolate->willDestroy(); |
| +} |
| + |
| +void CompositorWorkerManager::destroyIsolate() |
| +{ |
| + MutexLocker lock(m_mutex); |
| + if (!m_thread) |
| + m_isolate.clear(); |
|
kinuko
2015/06/01 13:15:42
Can you also assert m_workerCount == 0 here?
sadrul
2015/06/01 14:21:16
Done.
|
| +} |
| + |
| +void CompositorWorkerManager::terminateIsolate() |
| +{ |
| + MutexLocker lock(m_mutex); |
| + ASSERT(isMainThread()); |
| + if (m_workerCount > 1) |
| + return; |
| + |
| + m_isolate->terminateExecution(); |
| +} |
| + |
| +} // namespace blink |