OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "modules/compositorworker/CompositorWorkerManager.h" |
| 7 |
| 8 #include "bindings/core/v8/V8Binding.h" |
| 9 #include "bindings/core/v8/V8GCController.h" |
| 10 #include "bindings/core/v8/V8Initializer.h" |
| 11 #include "bindings/core/v8/V8PerIsolateData.h" |
| 12 #include "platform/ThreadSafeFunctional.h" |
| 13 #include "platform/WebThreadSupportingGC.h" |
| 14 #include "wtf/MainThread.h" |
| 15 #include "wtf/ThreadingPrimitives.h" |
| 16 |
| 17 namespace blink { |
| 18 |
| 19 namespace { |
| 20 |
| 21 static CompositorWorkerManager* s_instance = nullptr; |
| 22 |
| 23 Mutex& singletonMutex() |
| 24 { |
| 25 AtomicallyInitializedStaticReference(Mutex, mutex, new Mutex); |
| 26 return mutex; |
| 27 } |
| 28 |
| 29 void destroyThread(WebThread* thread) |
| 30 { |
| 31 delete thread; |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
| 36 void CompositorWorkerManager::initialize() |
| 37 { |
| 38 MutexLocker lock(singletonMutex()); |
| 39 ASSERT(!s_instance); |
| 40 s_instance = new CompositorWorkerManager(); |
| 41 } |
| 42 |
| 43 void CompositorWorkerManager::shutdown() |
| 44 { |
| 45 MutexLocker lock(singletonMutex()); |
| 46 ASSERT(s_instance); |
| 47 delete s_instance; |
| 48 s_instance = nullptr; |
| 49 } |
| 50 |
| 51 CompositorWorkerManager* CompositorWorkerManager::instance() |
| 52 { |
| 53 MutexLocker lock(singletonMutex()); |
| 54 ASSERT(s_instance); |
| 55 return s_instance; |
| 56 } |
| 57 |
| 58 CompositorWorkerManager::CompositorWorkerManager() |
| 59 { |
| 60 } |
| 61 |
| 62 CompositorWorkerManager::~CompositorWorkerManager() |
| 63 { |
| 64 } |
| 65 |
| 66 WebThreadSupportingGC& CompositorWorkerManager::compositorWorkerThread() |
| 67 { |
| 68 MutexLocker lock(m_mutex); |
| 69 if (!m_thread) { |
| 70 ASSERT(isMainThread()); |
| 71 ASSERT(!m_workerCount); |
| 72 // TODO(sadrul): Instead of creating a new thread, retrieve the thread f
rom |
| 73 // Platform using a more specialized function |
| 74 // (e.g. Platform::compositorWorkerThread()). |
| 75 m_platformThread = adoptPtr(Platform::current()->createThread("Composito
rWorker Thread")); |
| 76 m_thread = WebThreadSupportingGC::createForThread(m_platformThread.get()
); |
| 77 } |
| 78 return *m_thread.get(); |
| 79 } |
| 80 |
| 81 void CompositorWorkerManager::initializeBackingThread() |
| 82 { |
| 83 ASSERT(m_thread->isCurrentThread()); |
| 84 MutexLocker lock(m_mutex); |
| 85 ++m_workerCount; |
| 86 if (m_workerCount > 1) |
| 87 return; |
| 88 |
| 89 m_thread->initialize(); |
| 90 |
| 91 // Initialize the isolate at the same time. |
| 92 ASSERT(!m_isolate); |
| 93 m_isolate = V8PerIsolateData::initialize(); |
| 94 V8Initializer::initializeWorker(m_isolate); |
| 95 |
| 96 OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterruptor
(m_isolate)); |
| 97 ThreadState::current()->addInterruptor(interruptor.release()); |
| 98 ThreadState::current()->registerTraceDOMWrappers(m_isolate, V8GCController::
traceDOMWrappers); |
| 99 } |
| 100 |
| 101 void CompositorWorkerManager::shutdownBackingThread() |
| 102 { |
| 103 MutexLocker lock(m_mutex); |
| 104 ASSERT(m_thread->isCurrentThread()); |
| 105 ASSERT(m_workerCount > 0); |
| 106 --m_workerCount; |
| 107 if (m_workerCount == 0) { |
| 108 m_thread->shutdown(); |
| 109 m_thread = nullptr; |
| 110 Platform::current()->mainThread()->taskRunner()->postTask(BLINK_FROM_HER
E, threadSafeBind(destroyThread, AllowCrossThreadAccess(m_platformThread.leakPtr
()))); |
| 111 } |
| 112 } |
| 113 |
| 114 v8::Isolate* CompositorWorkerManager::initializeIsolate() |
| 115 { |
| 116 MutexLocker lock(m_mutex); |
| 117 ASSERT(m_thread->isCurrentThread()); |
| 118 ASSERT(m_isolate); |
| 119 // It is safe to use the existing isolate even if TerminateExecution() has b
een |
| 120 // called on it, without calling CancelTerminateExecution(). |
| 121 return m_isolate; |
| 122 } |
| 123 |
| 124 void CompositorWorkerManager::willDestroyIsolate() |
| 125 { |
| 126 MutexLocker lock(m_mutex); |
| 127 ASSERT(m_thread->isCurrentThread()); |
| 128 if (m_workerCount == 1) |
| 129 V8PerIsolateData::willBeDestroyed(m_isolate); |
| 130 } |
| 131 |
| 132 void CompositorWorkerManager::destroyIsolate() |
| 133 { |
| 134 MutexLocker lock(m_mutex); |
| 135 if (!m_thread) { |
| 136 ASSERT(m_workerCount == 0); |
| 137 V8PerIsolateData::destroy(m_isolate); |
| 138 m_isolate = nullptr; |
| 139 } |
| 140 } |
| 141 |
| 142 void CompositorWorkerManager::terminateV8Execution() |
| 143 { |
| 144 MutexLocker lock(m_mutex); |
| 145 ASSERT(isMainThread()); |
| 146 if (m_workerCount > 1) |
| 147 return; |
| 148 |
| 149 v8::V8::TerminateExecution(m_isolate); |
| 150 } |
| 151 |
| 152 } // namespace blink |
OLD | NEW |