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 { |
| 23 ASSERT(isMainThread()); |
| 24 // The destructor for |thread| will block until all tasks have completed. |
| 25 // This guarantees that shutdown will finish before the thread is destroyed. |
| 26 delete thread; |
| 27 } |
| 28 |
| 29 class CompositorWorkerSharedState { |
| 30 public: |
| 31 static CompositorWorkerSharedState& instance() |
| 32 { |
| 33 AtomicallyInitializedStaticReference(CompositorWorkerSharedState, compos
itorWorkerSharedState, (new CompositorWorkerSharedState())); |
| 34 return compositorWorkerSharedState; |
| 35 } |
| 36 |
| 37 WebThreadSupportingGC* compositorWorkerThread() |
| 38 { |
| 39 MutexLocker lock(m_mutex); |
| 40 if (!m_thread && isMainThread()) { |
| 41 ASSERT(!m_workerCount); |
| 42 WebThread* platformThread = Platform::current()->compositorThread(); |
| 43 m_thread = WebThreadSupportingGC::createForThread(platformThread); |
| 44 } |
| 45 return m_thread.get(); |
| 46 } |
| 47 |
| 48 void initializeBackingThread() |
| 49 { |
| 50 MutexLocker lock(m_mutex); |
| 51 ASSERT(m_thread->isCurrentThread()); |
| 52 ++m_workerCount; |
| 53 if (m_workerCount > 1) |
| 54 return; |
| 55 |
| 56 m_thread->initialize(); |
| 57 |
| 58 // Initialize the isolate at the same time. |
| 59 ASSERT(!m_isolate); |
| 60 m_isolate = V8PerIsolateData::initialize(); |
| 61 V8Initializer::initializeWorker(m_isolate); |
| 62 |
| 63 OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterru
ptor(m_isolate)); |
| 64 ThreadState::current()->addInterruptor(interruptor.release()); |
| 65 ThreadState::current()->registerTraceDOMWrappers(m_isolate, V8GCControll
er::traceDOMWrappers); |
| 66 } |
| 67 |
| 68 void shutdownBackingThread() |
| 69 { |
| 70 MutexLocker lock(m_mutex); |
| 71 ASSERT(m_thread->isCurrentThread()); |
| 72 ASSERT(m_workerCount > 0); |
| 73 --m_workerCount; |
| 74 if (m_workerCount == 0) { |
| 75 m_thread->shutdown(); |
| 76 Platform::current()->mainThread()->taskRunner()->postTask( |
| 77 BLINK_FROM_HERE, threadSafeBind(destroyThread, AllowCrossThreadA
ccess(m_thread.leakPtr()))); |
| 78 } |
| 79 } |
| 80 |
| 81 v8::Isolate* initializeIsolate() |
| 82 { |
| 83 MutexLocker lock(m_mutex); |
| 84 ASSERT(m_thread->isCurrentThread()); |
| 85 ASSERT(m_isolate); |
| 86 // It is safe to use the existing isolate even if TerminateExecution() h
as been |
| 87 // called on it, without calling CancelTerminateExecution(). |
| 88 return m_isolate; |
| 89 } |
| 90 |
| 91 void willDestroyIsolate() |
| 92 { |
| 93 MutexLocker lock(m_mutex); |
| 94 ASSERT(m_thread->isCurrentThread()); |
| 95 if (m_workerCount == 1) |
| 96 V8PerIsolateData::willBeDestroyed(m_isolate); |
| 97 } |
| 98 |
| 99 void destroyIsolate() |
| 100 { |
| 101 MutexLocker lock(m_mutex); |
| 102 if (!m_thread) { |
| 103 ASSERT(m_workerCount == 0); |
| 104 V8PerIsolateData::destroy(m_isolate); |
| 105 m_isolate = nullptr; |
| 106 } |
| 107 } |
| 108 |
| 109 void terminateV8Execution() |
| 110 { |
| 111 MutexLocker lock(m_mutex); |
| 112 ASSERT(isMainThread()); |
| 113 if (m_workerCount > 1) |
| 114 return; |
| 115 |
| 116 v8::V8::TerminateExecution(m_isolate); |
| 117 } |
| 118 |
| 119 bool hasThreadForTest() |
| 120 { |
| 121 return m_thread; |
| 122 } |
| 123 |
| 124 bool hasIsolateForTest() |
| 125 { |
| 126 return m_isolate; |
| 127 } |
| 128 |
| 129 private: |
| 130 CompositorWorkerSharedState() {} |
| 131 ~CompositorWorkerSharedState() {} |
| 132 |
| 133 Mutex m_mutex; |
| 134 OwnPtr<WebThreadSupportingGC> m_thread; |
| 135 int m_workerCount = 0; |
| 136 v8::Isolate* m_isolate = nullptr; |
| 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 { |
32 } | 157 } |
33 | 158 |
| 159 WebThreadSupportingGC* CompositorWorkerThread::sharedBackingThread() |
| 160 { |
| 161 return CompositorWorkerSharedState::instance().compositorWorkerThread(); |
| 162 } |
| 163 |
34 PassRefPtrWillBeRawPtr<WorkerGlobalScope> CompositorWorkerThread::createWorkerGl
obalScope(PassOwnPtr<WorkerThreadStartupData> startupData) | 164 PassRefPtrWillBeRawPtr<WorkerGlobalScope> CompositorWorkerThread::createWorkerGl
obalScope(PassOwnPtr<WorkerThreadStartupData> startupData) |
35 { | 165 { |
| 166 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::createWorkerGlobalScope"); |
36 return CompositorWorkerGlobalScope::create(this, startupData, m_timeOrigin); | 167 return CompositorWorkerGlobalScope::create(this, startupData, m_timeOrigin); |
37 } | 168 } |
38 | 169 |
39 WebThreadSupportingGC& CompositorWorkerThread::backingThread() | 170 WebThreadSupportingGC& CompositorWorkerThread::backingThread() |
40 { | 171 { |
41 return CompositorWorkerManager::instance()->compositorWorkerThread(); | 172 return *CompositorWorkerSharedState::instance().compositorWorkerThread(); |
42 } | 173 } |
43 | 174 |
44 void CompositorWorkerThread::initializeBackingThread() | 175 void CompositorWorkerThread::initializeBackingThread() |
45 { | 176 { |
46 CompositorWorkerManager::instance()->initializeBackingThread(); | 177 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::initializeBackingThread"); |
| 178 CompositorWorkerSharedState::instance().initializeBackingThread(); |
47 } | 179 } |
48 | 180 |
49 void CompositorWorkerThread::shutdownBackingThread() | 181 void CompositorWorkerThread::shutdownBackingThread() |
50 { | 182 { |
51 CompositorWorkerManager::instance()->shutdownBackingThread(); | 183 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::shutdownBackingThread"); |
| 184 CompositorWorkerSharedState::instance().shutdownBackingThread(); |
52 } | 185 } |
53 | 186 |
54 v8::Isolate* CompositorWorkerThread::initializeIsolate() | 187 v8::Isolate* CompositorWorkerThread::initializeIsolate() |
55 { | 188 { |
56 return CompositorWorkerManager::instance()->initializeIsolate(); | 189 return CompositorWorkerSharedState::instance().initializeIsolate(); |
57 } | 190 } |
58 | 191 |
59 void CompositorWorkerThread::willDestroyIsolate() | 192 void CompositorWorkerThread::willDestroyIsolate() |
60 { | 193 { |
61 CompositorWorkerManager::instance()->willDestroyIsolate(); | 194 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::willDestroyIsolate"); |
| 195 CompositorWorkerSharedState::instance().willDestroyIsolate(); |
62 } | 196 } |
63 | 197 |
64 void CompositorWorkerThread::destroyIsolate() | 198 void CompositorWorkerThread::destroyIsolate() |
65 { | 199 { |
66 CompositorWorkerManager::instance()->destroyIsolate(); | 200 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::destroyIsolate"); |
| 201 CompositorWorkerSharedState::instance().destroyIsolate(); |
67 } | 202 } |
68 | 203 |
69 void CompositorWorkerThread::terminateV8Execution() | 204 void CompositorWorkerThread::terminateV8Execution() |
70 { | 205 { |
71 CompositorWorkerManager::instance()->terminateV8Execution(); | 206 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::terminateV8Execution"); |
| 207 CompositorWorkerSharedState::instance().terminateV8Execution(); |
| 208 } |
| 209 |
| 210 bool CompositorWorkerThread::hasThreadForTest() |
| 211 { |
| 212 return CompositorWorkerSharedState::instance().hasThreadForTest(); |
| 213 } |
| 214 |
| 215 bool CompositorWorkerThread::hasIsolateForTest() |
| 216 { |
| 217 return CompositorWorkerSharedState::instance().hasIsolateForTest(); |
72 } | 218 } |
73 | 219 |
74 } // namespace blink | 220 } // namespace blink |
OLD | NEW |