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