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" | |
9 #include "bindings/core/v8/V8Initializer.h" | 8 #include "bindings/core/v8/V8Initializer.h" |
10 #include "core/workers/WorkerObjectProxy.h" | 9 #include "core/workers/WorkerObjectProxy.h" |
11 #include "core/workers/WorkerThreadStartupData.h" | 10 #include "core/workers/WorkerThreadStartupData.h" |
12 #include "modules/compositorworker/CompositorWorkerGlobalScope.h" | 11 #include "modules/compositorworker/CompositorWorkerGlobalScope.h" |
13 #include "platform/ThreadSafeFunctional.h" | 12 #include "modules/compositorworker/CompositorWorkerManager.h" |
14 #include "platform/TraceEvent.h" | |
15 #include "public/platform/Platform.h" | 13 #include "public/platform/Platform.h" |
16 | 14 |
17 namespace blink { | 15 namespace blink { |
18 | 16 |
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 | |
132 PassRefPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor
kerLoaderProxy> workerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double
timeOrigin) | 17 PassRefPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor
kerLoaderProxy> workerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double
timeOrigin) |
133 { | 18 { |
134 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::create"); | |
135 ASSERT(isMainThread()); | 19 ASSERT(isMainThread()); |
136 return adoptRef(new CompositorWorkerThread(workerLoaderProxy, workerObjectPr
oxy, timeOrigin)); | 20 return adoptRef(new CompositorWorkerThread(workerLoaderProxy, workerObjectPr
oxy, timeOrigin)); |
137 } | 21 } |
138 | 22 |
139 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor
kerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) | 23 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor
kerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) |
140 : WorkerThread(workerLoaderProxy, workerObjectProxy) | 24 : WorkerThread(workerLoaderProxy, workerObjectProxy) |
141 , m_workerObjectProxy(workerObjectProxy) | 25 , m_workerObjectProxy(workerObjectProxy) |
142 , m_timeOrigin(timeOrigin) | 26 , m_timeOrigin(timeOrigin) |
143 { | 27 { |
144 } | 28 } |
145 | 29 |
146 CompositorWorkerThread::~CompositorWorkerThread() | 30 CompositorWorkerThread::~CompositorWorkerThread() |
147 { | 31 { |
148 } | 32 } |
149 | 33 |
150 WebThreadSupportingGC* CompositorWorkerThread::sharedBackingThread() | |
151 { | |
152 return CompositorWorkerSharedState::instance().compositorWorkerThread(); | |
153 } | |
154 | |
155 PassRefPtrWillBeRawPtr<WorkerGlobalScope> CompositorWorkerThread::createWorkerGl
obalScope(PassOwnPtr<WorkerThreadStartupData> startupData) | 34 PassRefPtrWillBeRawPtr<WorkerGlobalScope> CompositorWorkerThread::createWorkerGl
obalScope(PassOwnPtr<WorkerThreadStartupData> startupData) |
156 { | 35 { |
157 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::createWorkerGlobalScope"); | |
158 return CompositorWorkerGlobalScope::create(this, startupData, m_timeOrigin); | 36 return CompositorWorkerGlobalScope::create(this, startupData, m_timeOrigin); |
159 } | 37 } |
160 | 38 |
161 WebThreadSupportingGC& CompositorWorkerThread::backingThread() | 39 WebThreadSupportingGC& CompositorWorkerThread::backingThread() |
162 { | 40 { |
163 return *CompositorWorkerSharedState::instance().compositorWorkerThread(); | 41 return CompositorWorkerManager::instance()->compositorWorkerThread(); |
164 } | 42 } |
165 | 43 |
166 void CompositorWorkerThread::initializeBackingThread() | 44 void CompositorWorkerThread::initializeBackingThread() |
167 { | 45 { |
168 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::initializeBackingThread"); | 46 CompositorWorkerManager::instance()->initializeBackingThread(); |
169 CompositorWorkerSharedState::instance().initializeBackingThread(); | |
170 } | 47 } |
171 | 48 |
172 void CompositorWorkerThread::shutdownBackingThread() | 49 void CompositorWorkerThread::shutdownBackingThread() |
173 { | 50 { |
174 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::shutdownBackingThread"); | 51 CompositorWorkerManager::instance()->shutdownBackingThread(); |
175 CompositorWorkerSharedState::instance().shutdownBackingThread(); | |
176 } | 52 } |
177 | 53 |
178 v8::Isolate* CompositorWorkerThread::initializeIsolate() | 54 v8::Isolate* CompositorWorkerThread::initializeIsolate() |
179 { | 55 { |
180 return CompositorWorkerSharedState::instance().initializeIsolate(); | 56 return CompositorWorkerManager::instance()->initializeIsolate(); |
181 } | 57 } |
182 | 58 |
183 void CompositorWorkerThread::willDestroyIsolate() | 59 void CompositorWorkerThread::willDestroyIsolate() |
184 { | 60 { |
185 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::willDestroyIsolate"); | 61 CompositorWorkerManager::instance()->willDestroyIsolate(); |
186 CompositorWorkerSharedState::instance().willDestroyIsolate(); | |
187 } | 62 } |
188 | 63 |
189 void CompositorWorkerThread::destroyIsolate() | 64 void CompositorWorkerThread::destroyIsolate() |
190 { | 65 { |
191 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::destroyIsolate"); | 66 CompositorWorkerManager::instance()->destroyIsolate(); |
192 CompositorWorkerSharedState::instance().destroyIsolate(); | |
193 } | 67 } |
194 | 68 |
195 void CompositorWorkerThread::terminateV8Execution() | 69 void CompositorWorkerThread::terminateV8Execution() |
196 { | 70 { |
197 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::terminateV8Execution"); | 71 CompositorWorkerManager::instance()->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(); | |
209 } | 72 } |
210 | 73 |
211 } // namespace blink | 74 } // namespace blink |
OLD | NEW |