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() |
| 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_thread = nullptr; |
| 69 } |
| 70 } |
| 71 |
| 72 v8::Isolate* initializeIsolate() |
| 73 { |
| 74 MutexLocker lock(m_mutex); |
| 75 ASSERT(m_thread->isCurrentThread()); |
| 76 ASSERT(m_isolate); |
| 77 // It is safe to use the existing isolate even if TerminateExecution() h
as been |
| 78 // called on it, without calling CancelTerminateExecution(). |
| 79 return m_isolate; |
| 80 } |
| 81 |
| 82 void willDestroyIsolate() |
| 83 { |
| 84 MutexLocker lock(m_mutex); |
| 85 ASSERT(m_thread->isCurrentThread()); |
| 86 if (m_workerCount == 1) |
| 87 V8PerIsolateData::willBeDestroyed(m_isolate); |
| 88 } |
| 89 |
| 90 void destroyIsolate() |
| 91 { |
| 92 MutexLocker lock(m_mutex); |
| 93 if (!m_thread) { |
| 94 ASSERT(m_workerCount == 0); |
| 95 V8PerIsolateData::destroy(m_isolate); |
| 96 m_isolate = nullptr; |
| 97 } |
| 98 } |
| 99 |
| 100 void terminateV8Execution() |
| 101 { |
| 102 MutexLocker lock(m_mutex); |
| 103 ASSERT(isMainThread()); |
| 104 if (m_workerCount > 1) |
| 105 return; |
| 106 |
| 107 v8::V8::TerminateExecution(m_isolate); |
| 108 } |
| 109 |
| 110 bool hasThreadForTest() |
| 111 { |
| 112 return m_thread; |
| 113 } |
| 114 |
| 115 bool hasIsolateForTest() |
| 116 { |
| 117 return m_isolate; |
| 118 } |
| 119 |
| 120 private: |
| 121 CompositorWorkerSharedState() {} |
| 122 ~CompositorWorkerSharedState() {} |
| 123 |
| 124 Mutex m_mutex; |
| 125 OwnPtr<WebThreadSupportingGC> m_thread; |
| 126 int m_workerCount = 0; |
| 127 v8::Isolate* m_isolate = nullptr; |
| 128 }; |
| 129 |
| 130 } // namespace |
| 131 |
17 PassRefPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor
kerLoaderProxy> workerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double
timeOrigin) | 132 PassRefPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor
kerLoaderProxy> workerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double
timeOrigin) |
18 { | 133 { |
| 134 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::create"); |
19 ASSERT(isMainThread()); | 135 ASSERT(isMainThread()); |
20 return adoptRef(new CompositorWorkerThread(workerLoaderProxy, workerObjectPr
oxy, timeOrigin)); | 136 return adoptRef(new CompositorWorkerThread(workerLoaderProxy, workerObjectPr
oxy, timeOrigin)); |
21 } | 137 } |
22 | 138 |
23 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor
kerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) | 139 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor
kerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) |
24 : WorkerThread(workerLoaderProxy, workerObjectProxy) | 140 : WorkerThread(workerLoaderProxy, workerObjectProxy) |
25 , m_workerObjectProxy(workerObjectProxy) | 141 , m_workerObjectProxy(workerObjectProxy) |
26 , m_timeOrigin(timeOrigin) | 142 , m_timeOrigin(timeOrigin) |
27 { | 143 { |
28 } | 144 } |
29 | 145 |
30 CompositorWorkerThread::~CompositorWorkerThread() | 146 CompositorWorkerThread::~CompositorWorkerThread() |
31 { | 147 { |
32 } | 148 } |
33 | 149 |
| 150 WebThreadSupportingGC* CompositorWorkerThread::sharedBackingThread() |
| 151 { |
| 152 return CompositorWorkerSharedState::instance().compositorWorkerThread(); |
| 153 } |
| 154 |
34 PassRefPtrWillBeRawPtr<WorkerGlobalScope> CompositorWorkerThread::createWorkerGl
obalScope(PassOwnPtr<WorkerThreadStartupData> startupData) | 155 PassRefPtrWillBeRawPtr<WorkerGlobalScope> CompositorWorkerThread::createWorkerGl
obalScope(PassOwnPtr<WorkerThreadStartupData> startupData) |
35 { | 156 { |
| 157 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::createWorkerGlobalScope"); |
36 return CompositorWorkerGlobalScope::create(this, startupData, m_timeOrigin); | 158 return CompositorWorkerGlobalScope::create(this, startupData, m_timeOrigin); |
37 } | 159 } |
38 | 160 |
39 WebThreadSupportingGC& CompositorWorkerThread::backingThread() | 161 WebThreadSupportingGC& CompositorWorkerThread::backingThread() |
40 { | 162 { |
41 return CompositorWorkerManager::instance()->compositorWorkerThread(); | 163 return *CompositorWorkerSharedState::instance().compositorWorkerThread(); |
42 } | 164 } |
43 | 165 |
44 void CompositorWorkerThread::initializeBackingThread() | 166 void CompositorWorkerThread::initializeBackingThread() |
45 { | 167 { |
46 CompositorWorkerManager::instance()->initializeBackingThread(); | 168 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::initializeBackingThread"); |
| 169 CompositorWorkerSharedState::instance().initializeBackingThread(); |
47 } | 170 } |
48 | 171 |
49 void CompositorWorkerThread::shutdownBackingThread() | 172 void CompositorWorkerThread::shutdownBackingThread() |
50 { | 173 { |
51 CompositorWorkerManager::instance()->shutdownBackingThread(); | 174 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::shutdownBackingThread"); |
| 175 CompositorWorkerSharedState::instance().shutdownBackingThread(); |
52 } | 176 } |
53 | 177 |
54 v8::Isolate* CompositorWorkerThread::initializeIsolate() | 178 v8::Isolate* CompositorWorkerThread::initializeIsolate() |
55 { | 179 { |
56 return CompositorWorkerManager::instance()->initializeIsolate(); | 180 return CompositorWorkerSharedState::instance().initializeIsolate(); |
57 } | 181 } |
58 | 182 |
59 void CompositorWorkerThread::willDestroyIsolate() | 183 void CompositorWorkerThread::willDestroyIsolate() |
60 { | 184 { |
61 CompositorWorkerManager::instance()->willDestroyIsolate(); | 185 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::willDestroyIsolate"); |
| 186 CompositorWorkerSharedState::instance().willDestroyIsolate(); |
62 } | 187 } |
63 | 188 |
64 void CompositorWorkerThread::destroyIsolate() | 189 void CompositorWorkerThread::destroyIsolate() |
65 { | 190 { |
66 CompositorWorkerManager::instance()->destroyIsolate(); | 191 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::destroyIsolate"); |
| 192 CompositorWorkerSharedState::instance().destroyIsolate(); |
67 } | 193 } |
68 | 194 |
69 void CompositorWorkerThread::terminateV8Execution() | 195 void CompositorWorkerThread::terminateV8Execution() |
70 { | 196 { |
71 CompositorWorkerManager::instance()->terminateV8Execution(); | 197 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::terminateV8Execution"); |
| 198 CompositorWorkerSharedState::instance().terminateV8Execution(); |
| 199 } |
| 200 |
| 201 bool CompositorWorkerThread::hasThreadForTest() |
| 202 { |
| 203 return CompositorWorkerSharedState::instance().hasThreadForTest(); |
| 204 } |
| 205 |
| 206 bool CompositorWorkerThread::hasIsolateForTest() |
| 207 { |
| 208 return CompositorWorkerSharedState::instance().hasIsolateForTest(); |
72 } | 209 } |
73 | 210 |
74 } // namespace blink | 211 } // namespace blink |
OLD | NEW |