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 static Mutex& singletonMutex() |
| 24 { |
| 25 AtomicallyInitializedStaticReference(Mutex, mutex, new Mutex); |
| 26 return mutex; |
| 27 } |
| 28 |
| 29 static void destroyThread(WebThreadSupportingGC* 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 m_thread = WebThreadSupportingGC::create("CompositorWorker Thread"); |
| 73 } |
| 74 return *m_thread.get(); |
| 75 } |
| 76 |
| 77 void CompositorWorkerManager::initializeBackingThread() |
| 78 { |
| 79 ASSERT(m_thread->isCurrentThread()); |
| 80 MutexLocker lock(m_mutex); |
| 81 ++m_workerCount; |
| 82 if (m_workerCount > 1) |
| 83 return; |
| 84 |
| 85 m_thread->initialize(); |
| 86 |
| 87 // Initialize the isolate at the same time. |
| 88 ASSERT(!m_isolate); |
| 89 m_isolate = V8PerIsolateData::initialize(); |
| 90 V8Initializer::initializeWorker(m_isolate); |
| 91 |
| 92 m_interruptor = adoptPtr(new V8IsolateInterruptor(m_isolate)); |
| 93 ThreadState::current()->addInterruptor(m_interruptor.get()); |
| 94 ThreadState::current()->registerTraceDOMWrappers(m_isolate, V8GCController::
traceDOMWrappers); |
| 95 } |
| 96 |
| 97 void CompositorWorkerManager::shutdownBackingThread() |
| 98 { |
| 99 MutexLocker lock(m_mutex); |
| 100 ASSERT(m_thread->isCurrentThread()); |
| 101 ASSERT(m_workerCount > 0); |
| 102 --m_workerCount; |
| 103 if (m_workerCount == 0) { |
| 104 m_thread->shutdown(); |
| 105 Platform::current()->mainThread()->postTask(FROM_HERE, threadSafeBind(de
stroyThread, AllowCrossThreadAccess(m_thread.leakPtr()))); |
| 106 m_thread = nullptr; |
| 107 } |
| 108 } |
| 109 |
| 110 v8::Isolate* CompositorWorkerManager::initializeIsolate() |
| 111 { |
| 112 MutexLocker lock(m_mutex); |
| 113 ASSERT(m_thread->isCurrentThread()); |
| 114 ASSERT(m_isolate); |
| 115 // It is safe to use the existing isolate even if TerminateExecution() has b
een |
| 116 // called on it, without calling CancelTerminateExecution(). |
| 117 return m_isolate; |
| 118 } |
| 119 |
| 120 void CompositorWorkerManager::willDestroyIsolate() |
| 121 { |
| 122 MutexLocker lock(m_mutex); |
| 123 ASSERT(m_thread->isCurrentThread()); |
| 124 if (m_workerCount == 1) { |
| 125 V8PerIsolateData::willBeDestroyed(m_isolate); |
| 126 ThreadState::current()->removeInterruptor(m_interruptor.get()); |
| 127 } |
| 128 } |
| 129 |
| 130 void CompositorWorkerManager::destroyIsolate() |
| 131 { |
| 132 MutexLocker lock(m_mutex); |
| 133 if (!m_thread) { |
| 134 ASSERT(m_workerCount == 0); |
| 135 V8PerIsolateData::destroy(m_isolate); |
| 136 m_isolate = nullptr; |
| 137 } |
| 138 } |
| 139 |
| 140 void CompositorWorkerManager::terminateV8Execution() |
| 141 { |
| 142 MutexLocker lock(m_mutex); |
| 143 ASSERT(isMainThread()); |
| 144 if (m_workerCount > 1) |
| 145 return; |
| 146 |
| 147 v8::V8::TerminateExecution(m_isolate); |
| 148 } |
| 149 |
| 150 } // namespace blink |
OLD | NEW |