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 void destroyThread(WebThreadSupportingGC* thread) | |
| 22 { | |
|
haraken
2015/12/03 05:19:31
It would be helpful to add ASSERT(isMainThread())
yhirano
2016/02/02 06:38:20
Hi haraken@, I wanna understand the comment. Is th
haraken
2016/02/02 07:07:56
Good point. A blocking operation happens in the We
| |
| 23 delete thread; | |
| 24 } | |
| 25 | |
| 26 class CompositorWorkerSharedState { | |
| 27 public: | |
| 28 static CompositorWorkerSharedState& instance() | |
| 29 { | |
| 30 AtomicallyInitializedStaticReference(CompositorWorkerSharedState, compos itorWorkerSharedState, (new CompositorWorkerSharedState())); | |
| 31 return compositorWorkerSharedState; | |
| 32 } | |
| 33 | |
| 34 WebThreadSupportingGC* compositorWorkerThread() | |
| 35 { | |
| 36 MutexLocker lock(m_mutex); | |
| 37 if (!m_thread && isMainThread()) { | |
| 38 ASSERT(!m_workerCount); | |
| 39 WebThread* platformThread = Platform::current()->compositorThread(); | |
| 40 m_thread = WebThreadSupportingGC::createForThread(platformThread); | |
| 41 } | |
| 42 return m_thread.get(); | |
| 43 } | |
| 44 | |
| 45 void initializeBackingThread() | |
| 46 { | |
| 47 MutexLocker lock(m_mutex); | |
| 48 ASSERT(m_thread->isCurrentThread()); | |
| 49 ++m_workerCount; | |
| 50 if (m_workerCount > 1) | |
| 51 return; | |
| 52 | |
| 53 m_thread->initialize(); | |
| 54 | |
| 55 // Initialize the isolate at the same time. | |
| 56 ASSERT(!m_isolate); | |
| 57 m_isolate = V8PerIsolateData::initialize(); | |
| 58 V8Initializer::initializeWorker(m_isolate); | |
| 59 | |
| 60 OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterru ptor(m_isolate)); | |
| 61 ThreadState::current()->addInterruptor(interruptor.release()); | |
| 62 ThreadState::current()->registerTraceDOMWrappers(m_isolate, V8GCControll er::traceDOMWrappers); | |
| 63 } | |
| 64 | |
| 65 void shutdownBackingThread() | |
| 66 { | |
| 67 MutexLocker lock(m_mutex); | |
| 68 ASSERT(m_thread->isCurrentThread()); | |
| 69 ASSERT(m_workerCount > 0); | |
| 70 --m_workerCount; | |
| 71 if (m_workerCount == 0) { | |
| 72 m_thread->shutdown(); | |
| 73 Platform::current()->mainThread()->taskRunner()->postTask( | |
| 74 BLINK_FROM_HERE, threadSafeBind(destroyThread, AllowCrossThreadA ccess(m_thread.leakPtr()))); | |
| 75 } | |
| 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 | |
| 136 } // namespace | |
| 137 | |
| 17 PassRefPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor kerLoaderProxy> workerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) | 138 PassRefPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor kerLoaderProxy> workerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) |
| 18 { | 139 { |
| 140 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::create"); | |
| 19 ASSERT(isMainThread()); | 141 ASSERT(isMainThread()); |
| 20 return adoptRef(new CompositorWorkerThread(workerLoaderProxy, workerObjectPr oxy, timeOrigin)); | 142 return adoptRef(new CompositorWorkerThread(workerLoaderProxy, workerObjectPr oxy, timeOrigin)); |
| 21 } | 143 } |
| 22 | 144 |
| 23 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor kerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) | 145 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor kerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) |
| 24 : WorkerThread(workerLoaderProxy, workerObjectProxy) | 146 : WorkerThread(workerLoaderProxy, workerObjectProxy) |
| 25 , m_workerObjectProxy(workerObjectProxy) | 147 , m_workerObjectProxy(workerObjectProxy) |
| 26 , m_timeOrigin(timeOrigin) | 148 , m_timeOrigin(timeOrigin) |
| 27 { | 149 { |
| 28 } | 150 } |
| 29 | 151 |
| 30 CompositorWorkerThread::~CompositorWorkerThread() | 152 CompositorWorkerThread::~CompositorWorkerThread() |
| 31 { | 153 { |
| 32 } | 154 } |
| 33 | 155 |
| 156 WebThreadSupportingGC* CompositorWorkerThread::sharedBackingThread() | |
| 157 { | |
| 158 return CompositorWorkerSharedState::instance().compositorWorkerThread(); | |
| 159 } | |
| 160 | |
| 34 PassRefPtrWillBeRawPtr<WorkerGlobalScope> CompositorWorkerThread::createWorkerGl obalScope(PassOwnPtr<WorkerThreadStartupData> startupData) | 161 PassRefPtrWillBeRawPtr<WorkerGlobalScope> CompositorWorkerThread::createWorkerGl obalScope(PassOwnPtr<WorkerThreadStartupData> startupData) |
| 35 { | 162 { |
| 163 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::createWorkerGlobalScope"); | |
| 36 return CompositorWorkerGlobalScope::create(this, startupData, m_timeOrigin); | 164 return CompositorWorkerGlobalScope::create(this, startupData, m_timeOrigin); |
| 37 } | 165 } |
| 38 | 166 |
| 39 WebThreadSupportingGC& CompositorWorkerThread::backingThread() | 167 WebThreadSupportingGC& CompositorWorkerThread::backingThread() |
| 40 { | 168 { |
| 41 return CompositorWorkerManager::instance()->compositorWorkerThread(); | 169 return *CompositorWorkerSharedState::instance().compositorWorkerThread(); |
| 42 } | 170 } |
| 43 | 171 |
| 44 void CompositorWorkerThread::initializeBackingThread() | 172 void CompositorWorkerThread::initializeBackingThread() |
| 45 { | 173 { |
| 46 CompositorWorkerManager::instance()->initializeBackingThread(); | 174 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::initializeBackingThread"); |
| 175 CompositorWorkerSharedState::instance().initializeBackingThread(); | |
| 47 } | 176 } |
| 48 | 177 |
| 49 void CompositorWorkerThread::shutdownBackingThread() | 178 void CompositorWorkerThread::shutdownBackingThread() |
| 50 { | 179 { |
| 51 CompositorWorkerManager::instance()->shutdownBackingThread(); | 180 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::shutdownBackingThread"); |
| 181 CompositorWorkerSharedState::instance().shutdownBackingThread(); | |
| 52 } | 182 } |
| 53 | 183 |
| 54 v8::Isolate* CompositorWorkerThread::initializeIsolate() | 184 v8::Isolate* CompositorWorkerThread::initializeIsolate() |
| 55 { | 185 { |
| 56 return CompositorWorkerManager::instance()->initializeIsolate(); | 186 return CompositorWorkerSharedState::instance().initializeIsolate(); |
| 57 } | 187 } |
| 58 | 188 |
| 59 void CompositorWorkerThread::willDestroyIsolate() | 189 void CompositorWorkerThread::willDestroyIsolate() |
| 60 { | 190 { |
| 61 CompositorWorkerManager::instance()->willDestroyIsolate(); | 191 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::willDestroyIsolate"); |
| 192 CompositorWorkerSharedState::instance().willDestroyIsolate(); | |
| 62 } | 193 } |
| 63 | 194 |
| 64 void CompositorWorkerThread::destroyIsolate() | 195 void CompositorWorkerThread::destroyIsolate() |
| 65 { | 196 { |
| 66 CompositorWorkerManager::instance()->destroyIsolate(); | 197 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::destroyIsolate"); |
| 198 CompositorWorkerSharedState::instance().destroyIsolate(); | |
| 67 } | 199 } |
| 68 | 200 |
| 69 void CompositorWorkerThread::terminateV8Execution() | 201 void CompositorWorkerThread::terminateV8Execution() |
| 70 { | 202 { |
| 71 CompositorWorkerManager::instance()->terminateV8Execution(); | 203 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::terminateV8Execution"); |
| 204 CompositorWorkerSharedState::instance().terminateV8Execution(); | |
| 205 } | |
| 206 | |
| 207 bool CompositorWorkerThread::hasThreadForTest() | |
| 208 { | |
| 209 return CompositorWorkerSharedState::instance().hasThreadForTest(); | |
| 210 } | |
| 211 | |
| 212 bool CompositorWorkerThread::hasIsolateForTest() | |
| 213 { | |
| 214 return CompositorWorkerSharedState::instance().hasIsolateForTest(); | |
| 72 } | 215 } |
| 73 | 216 |
| 74 } // namespace blink | 217 } // namespace blink |
| OLD | NEW |