Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "modules/compositorworker/CompositorWorkerThread.h" | 6 #include "modules/compositorworker/CompositorWorkerThread.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/V8GCController.h" | |
| 8 #include "bindings/core/v8/V8Initializer.h" | 9 #include "bindings/core/v8/V8Initializer.h" |
| 9 #include "core/workers/WorkerObjectProxy.h" | 10 #include "core/workers/WorkerObjectProxy.h" |
| 10 #include "core/workers/WorkerThreadStartupData.h" | 11 #include "core/workers/WorkerThreadStartupData.h" |
| 11 #include "modules/compositorworker/CompositorWorkerGlobalScope.h" | 12 #include "modules/compositorworker/CompositorWorkerGlobalScope.h" |
| 12 #include "modules/compositorworker/CompositorWorkerManager.h" | 13 #include "platform/ThreadSafeFunctional.h" |
| 14 #include "platform/TraceEvent.h" | |
| 13 #include "public/platform/Platform.h" | 15 #include "public/platform/Platform.h" |
| 14 | 16 |
| 15 namespace blink { | 17 namespace blink { |
| 16 | 18 |
| 19 namespace { | |
| 20 | |
| 21 class CompositorWorkerSharedState { | |
| 22 public: | |
| 23 static CompositorWorkerSharedState& instance() | |
| 24 { | |
| 25 AtomicallyInitializedStaticReference(CompositorWorkerSharedState, compos itorWorkerSharedState, (new CompositorWorkerSharedState())); | |
| 26 return compositorWorkerSharedState; | |
| 27 } | |
| 28 | |
| 29 WebThreadSupportingGC* compositorWorkerThread() | |
| 30 { | |
| 31 MutexLocker lock(m_mutex); | |
| 32 if (!m_thread && isMainThread()) { | |
| 33 ASSERT(!m_workerCount); | |
| 34 WebThread* platformThread = Platform::current()->compositorThread(); | |
| 35 m_thread = WebThreadSupportingGC::createForThread(platformThread); | |
| 36 } | |
| 37 return m_thread.get(); | |
| 38 } | |
| 39 | |
| 40 void initializeBackingThread() | |
| 41 { | |
| 42 MutexLocker lock(m_mutex); | |
| 43 ASSERT(m_thread->isCurrentThread()); | |
| 44 ++m_workerCount; | |
| 45 if (m_workerCount > 1) | |
| 46 return; | |
| 47 | |
| 48 m_thread->initialize(); | |
| 49 | |
| 50 // Initialize the isolate at the same time. | |
| 51 ASSERT(!m_isolate); | |
| 52 m_isolate = V8PerIsolateData::initialize(); | |
| 53 V8Initializer::initializeWorker(m_isolate); | |
| 54 | |
| 55 OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterru ptor(m_isolate)); | |
| 56 ThreadState::current()->addInterruptor(interruptor.release()); | |
| 57 ThreadState::current()->registerTraceDOMWrappers(m_isolate, V8GCControll er::traceDOMWrappers); | |
| 58 } | |
| 59 | |
| 60 void shutdownBackingThread(const CompositorWorkerThread* worker) | |
| 61 { | |
| 62 MutexLocker lock(m_mutex); | |
| 63 ASSERT(m_thread->isCurrentThread()); | |
| 64 ASSERT(m_workerCount > 0); | |
| 65 --m_workerCount; | |
| 66 if (m_workerCount == 0) { | |
| 67 m_thread->shutdown(); | |
| 68 m_markedForDeletion.set(worker, m_thread.release()); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void willDestroyWorkerThread(const CompositorWorkerThread* worker) | |
| 73 { | |
| 74 MutexLocker lock(m_mutex); | |
| 75 m_markedForDeletion.remove(worker); | |
| 76 } | |
| 77 | |
| 78 v8::Isolate* initializeIsolate() | |
| 79 { | |
| 80 MutexLocker lock(m_mutex); | |
| 81 ASSERT(m_thread->isCurrentThread()); | |
| 82 ASSERT(m_isolate); | |
| 83 // It is safe to use the existing isolate even if TerminateExecution() h as been | |
| 84 // called on it, without calling CancelTerminateExecution(). | |
| 85 return m_isolate; | |
| 86 } | |
| 87 | |
| 88 void willDestroyIsolate() | |
| 89 { | |
| 90 MutexLocker lock(m_mutex); | |
| 91 ASSERT(m_thread->isCurrentThread()); | |
| 92 if (m_workerCount == 1) | |
| 93 V8PerIsolateData::willBeDestroyed(m_isolate); | |
| 94 } | |
| 95 | |
| 96 void destroyIsolate() | |
| 97 { | |
| 98 MutexLocker lock(m_mutex); | |
| 99 if (!m_thread) { | |
| 100 ASSERT(m_workerCount == 0); | |
| 101 V8PerIsolateData::destroy(m_isolate); | |
| 102 m_isolate = nullptr; | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 void terminateV8Execution() | |
| 107 { | |
| 108 MutexLocker lock(m_mutex); | |
| 109 ASSERT(isMainThread()); | |
| 110 if (m_workerCount > 1) | |
| 111 return; | |
| 112 | |
| 113 v8::V8::TerminateExecution(m_isolate); | |
| 114 } | |
| 115 | |
| 116 bool hasThreadForTest() | |
| 117 { | |
| 118 return m_thread; | |
| 119 } | |
| 120 | |
| 121 bool hasIsolateForTest() | |
| 122 { | |
| 123 return m_isolate; | |
| 124 } | |
| 125 | |
| 126 private: | |
| 127 CompositorWorkerSharedState() {} | |
| 128 ~CompositorWorkerSharedState() {} | |
| 129 | |
| 130 Mutex m_mutex; | |
| 131 OwnPtr<WebThreadSupportingGC> m_thread; | |
| 132 int m_workerCount = 0; | |
| 133 v8::Isolate* m_isolate = nullptr; | |
| 134 | |
| 135 // You may only use this collection while holding the mutex. | |
| 136 HashMap<const CompositorWorkerThread*, OwnPtr<WebThreadSupportingGC>> m_mark edForDeletion; | |
|
haraken
2015/12/03 04:04:21
Why do we need the m_markedForDeletion? Wouldn't i
| |
| 137 }; | |
| 138 | |
| 139 } // namespace | |
| 140 | |
| 17 PassRefPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor kerLoaderProxy> workerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) | 141 PassRefPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor kerLoaderProxy> workerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) |
| 18 { | 142 { |
| 143 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::create"); | |
| 19 ASSERT(isMainThread()); | 144 ASSERT(isMainThread()); |
| 20 return adoptRef(new CompositorWorkerThread(workerLoaderProxy, workerObjectPr oxy, timeOrigin)); | 145 return adoptRef(new CompositorWorkerThread(workerLoaderProxy, workerObjectPr oxy, timeOrigin)); |
| 21 } | 146 } |
| 22 | 147 |
| 23 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor kerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) | 148 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor kerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) |
| 24 : WorkerThread(workerLoaderProxy, workerObjectProxy) | 149 : WorkerThread(workerLoaderProxy, workerObjectProxy) |
| 25 , m_workerObjectProxy(workerObjectProxy) | 150 , m_workerObjectProxy(workerObjectProxy) |
| 26 , m_timeOrigin(timeOrigin) | 151 , m_timeOrigin(timeOrigin) |
| 27 { | 152 { |
| 28 } | 153 } |
| 29 | 154 |
| 30 CompositorWorkerThread::~CompositorWorkerThread() | 155 CompositorWorkerThread::~CompositorWorkerThread() |
| 31 { | 156 { |
| 157 CompositorWorkerSharedState::instance().willDestroyWorkerThread(this); | |
| 158 } | |
| 159 | |
| 160 WebThreadSupportingGC* CompositorWorkerThread::sharedBackingThread() | |
| 161 { | |
| 162 return CompositorWorkerSharedState::instance().compositorWorkerThread(); | |
| 32 } | 163 } |
| 33 | 164 |
| 34 PassRefPtrWillBeRawPtr<WorkerGlobalScope> CompositorWorkerThread::createWorkerGl obalScope(PassOwnPtr<WorkerThreadStartupData> startupData) | 165 PassRefPtrWillBeRawPtr<WorkerGlobalScope> CompositorWorkerThread::createWorkerGl obalScope(PassOwnPtr<WorkerThreadStartupData> startupData) |
| 35 { | 166 { |
| 167 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::createWorkerGlobalScope"); | |
| 36 return CompositorWorkerGlobalScope::create(this, startupData, m_timeOrigin); | 168 return CompositorWorkerGlobalScope::create(this, startupData, m_timeOrigin); |
| 37 } | 169 } |
| 38 | 170 |
| 39 WebThreadSupportingGC& CompositorWorkerThread::backingThread() | 171 WebThreadSupportingGC& CompositorWorkerThread::backingThread() |
| 40 { | 172 { |
| 41 return CompositorWorkerManager::instance()->compositorWorkerThread(); | 173 return *CompositorWorkerSharedState::instance().compositorWorkerThread(); |
| 42 } | 174 } |
| 43 | 175 |
| 44 void CompositorWorkerThread::initializeBackingThread() | 176 void CompositorWorkerThread::initializeBackingThread() |
| 45 { | 177 { |
| 46 CompositorWorkerManager::instance()->initializeBackingThread(); | 178 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::initializeBackingThread"); |
| 179 CompositorWorkerSharedState::instance().initializeBackingThread(); | |
| 47 } | 180 } |
| 48 | 181 |
| 49 void CompositorWorkerThread::shutdownBackingThread() | 182 void CompositorWorkerThread::shutdownBackingThread() |
| 50 { | 183 { |
| 51 CompositorWorkerManager::instance()->shutdownBackingThread(); | 184 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::shutdownBackingThread"); |
| 185 CompositorWorkerSharedState::instance().shutdownBackingThread(this); | |
| 52 } | 186 } |
| 53 | 187 |
| 54 v8::Isolate* CompositorWorkerThread::initializeIsolate() | 188 v8::Isolate* CompositorWorkerThread::initializeIsolate() |
| 55 { | 189 { |
| 56 return CompositorWorkerManager::instance()->initializeIsolate(); | 190 return CompositorWorkerSharedState::instance().initializeIsolate(); |
| 57 } | 191 } |
| 58 | 192 |
| 59 void CompositorWorkerThread::willDestroyIsolate() | 193 void CompositorWorkerThread::willDestroyIsolate() |
| 60 { | 194 { |
| 61 CompositorWorkerManager::instance()->willDestroyIsolate(); | 195 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::willDestroyIsolate"); |
| 196 CompositorWorkerSharedState::instance().willDestroyIsolate(); | |
| 62 } | 197 } |
| 63 | 198 |
| 64 void CompositorWorkerThread::destroyIsolate() | 199 void CompositorWorkerThread::destroyIsolate() |
| 65 { | 200 { |
| 66 CompositorWorkerManager::instance()->destroyIsolate(); | 201 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::destroyIsolate"); |
| 202 CompositorWorkerSharedState::instance().destroyIsolate(); | |
| 67 } | 203 } |
| 68 | 204 |
| 69 void CompositorWorkerThread::terminateV8Execution() | 205 void CompositorWorkerThread::terminateV8Execution() |
| 70 { | 206 { |
| 71 CompositorWorkerManager::instance()->terminateV8Execution(); | 207 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::terminateV8Execution"); |
| 208 CompositorWorkerSharedState::instance().terminateV8Execution(); | |
| 209 } | |
| 210 | |
| 211 bool CompositorWorkerThread::hasThreadForTest() | |
| 212 { | |
| 213 return CompositorWorkerSharedState::instance().hasThreadForTest(); | |
| 214 } | |
| 215 | |
| 216 bool CompositorWorkerThread::hasIsolateForTest() | |
| 217 { | |
| 218 return CompositorWorkerSharedState::instance().hasIsolateForTest(); | |
| 72 } | 219 } |
| 73 | 220 |
| 74 } // namespace blink | 221 } // namespace blink |
| OLD | NEW |