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..12e5b1ad3145f96ea1113aa1d56d6ec91a6001fa |
--- /dev/null |
+++ b/Source/modules/compositorworker/CompositorWorkerManager.cpp |
@@ -0,0 +1,158 @@ |
+// 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 WorkerV8Isolate { |
+ WTF_MAKE_NONCOPYABLE(SharedV8Isolate); |
+public: |
+ explicit SharedV8Isolate(v8::Isolate* isolate) |
+ : m_isolate(isolate) { } |
+ ~SharedV8Isolate() override |
+ { |
+ CompositorWorkerManager::instance()->destroyIsolate(); |
+ } |
+ |
+private: |
+ // WorkerV8Isolate: |
+ v8::Isolate* isolate() const override { return m_isolate; } |
+ void willDestroy() override |
+ { |
+ CompositorWorkerManager::instance()->willDestroyIsolate(); |
+ } |
+ |
+ void terminateExecution() override |
+ { |
+ CompositorWorkerManager::instance()->terminateIsolate(); |
+ } |
+ |
+ v8::Isolate* m_isolate; |
+}; |
+ |
+} // namespace |
+ |
+static CompositorWorkerManager* s_instance = nullptr; |
kinuko
2015/05/26 14:41:25
Hmm, so we want to have single manager per process
sadrul
2015/05/27 01:28:00
That is indeed the intention, at least for now.
T
|
+ |
+static Mutex& singletonMutex() |
+{ |
+ AtomicallyInitializedStaticReference(Mutex, mutex, new Mutex); |
+ return mutex; |
+} |
+ |
+void CompositorWorkerManager::initialize() |
+{ |
+ MutexLocker lock(singletonMutex()); |
+ 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() |
+{ |
+ if (m_isolate) |
+ ASSERT(m_isolate.leakPtr()); |
+} |
+ |
+WebThreadSupportingGC& CompositorWorkerManager::compositorWorkerThread() |
+{ |
+ MutexLocker lock(m_mutex); |
+ if (!m_thread) { |
+ ASSERT(isMainThread()); |
+ 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(); |
+} |
+ |
+static void destroyThread(WebThreadSupportingGC* thread) |
+{ |
+ delete thread; |
+} |
kinuko
2015/05/26 14:41:25
nit: moving these static local methods in the anon
sadrul
2015/05/27 01:28:00
Done.
|
+ |
+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<WorkerV8Isolate> CompositorWorkerManager::createIsolate() |
+{ |
+ MutexLocker lock(m_mutex); |
+ ASSERT(m_thread->isCurrentThread()); |
+ if (!m_isolate) |
+ m_isolate = WorkerV8Isolate::createDefault(); |
+ ++m_isolateCount; |
+ return adoptPtr(new SharedV8Isolate(m_isolate->isolate())); |
+} |
+ |
+void CompositorWorkerManager::willDestroyIsolate() |
+{ |
+ MutexLocker lock(m_mutex); |
+ ASSERT(m_thread->isCurrentThread()); |
+ --m_isolateCount; |
+ if (m_isolateCount == 0) |
+ m_isolate->willDestroy(); |
+} |
+ |
+void CompositorWorkerManager::destroyIsolate() |
+{ |
+ MutexLocker lock(m_mutex); |
+ if (m_isolateCount == 0) |
+ m_isolate = nullptr; |
+} |
+ |
+void CompositorWorkerManager::terminateIsolate() |
+{ |
+ MutexLocker lock(m_mutex); |
+ ASSERT(isMainThread()); |
+ if (m_isolateCount > 1) |
+ return; |
+ |
+ m_isolate->terminateExecution(); |
+ // TODO(sad): Reset |m_isolate| here, so that if another script is created before this one is destroyed, |
+ // we would end up trying to reuse this terminated isolate. |
+} |
+ |
+} // namespace blink |