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..dc7fd62952dbcae3837c6ab1c628aa83409e4ddd |
--- /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 WorkerIsolateWrapper { |
haraken
2015/06/02 02:14:49
I think this is mis-designed and complicated more
sadrul
2015/06/02 02:37:53
We need SharedV8Isolate because each WorkerThread
haraken
2015/06/02 02:42:23
Just to confirm:
Compositor workers : CompositorW
sadrul
2015/06/02 02:48:28
There can be multiple WorkerThread instances. So:
haraken
2015/06/02 03:34:26
Thanks for the clarification!
It seems that the c
sadrul
2015/06/02 04:23:56
The WorkerThread doesn't necessarily own the Isola
|
+ 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) |
haraken
2015/06/02 02:14:49
Where is this called?
sadrul
2015/06/02 02:37:53
In a posted task (below in line 116).
|
+{ |
+ delete thread; |
haraken
2015/06/02 02:14:49
We should avoid calling manual delete...
sadrul
2015/06/02 02:37:53
I tried using something like DeleteSoon() in chrom
|
+} |
+ |
+} // 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()))); |
+ } |
+} |
+ |
+PassOwnPtr<WorkerIsolateWrapper> CompositorWorkerManager::createIsolate() |
+{ |
+ MutexLocker lock(m_mutex); |
+ ASSERT(m_thread->isCurrentThread()); |
+ if (!m_isolate) |
+ m_isolate = WorkerIsolateWrapper::createDefault(); |
+ // It is safe to use the existing isolate even if TerminateExecution() has been |
+ // called on it, without calling CancelTerminateExecution(). |
+ 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) { |
+ ASSERT(m_workerCount == 0); |
+ m_isolate.clear(); |
+ } |
+} |
+ |
+void CompositorWorkerManager::terminateIsolate() |
+{ |
+ MutexLocker lock(m_mutex); |
+ ASSERT(isMainThread()); |
+ if (m_workerCount > 1) |
+ return; |
+ |
+ m_isolate->terminateExecution(); |
+} |
+ |
+} // namespace blink |