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 "modules/compositorworker/CompositorWorkerThread.h" | 5 #include "modules/compositorworker/CompositorWorkerThread.h" |
6 | 6 |
7 #include "bindings/core/v8/V8GCController.h" | 7 #include "bindings/core/v8/V8GCController.h" |
8 #include "bindings/core/v8/V8Initializer.h" | 8 #include "bindings/core/v8/V8Initializer.h" |
9 #include "core/workers/WorkerBackingThread.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 "platform/ThreadSafeFunctional.h" | 13 #include "platform/ThreadSafeFunctional.h" |
13 #include "platform/TraceEvent.h" | 14 #include "platform/TraceEvent.h" |
14 #include "public/platform/Platform.h" | 15 #include "public/platform/Platform.h" |
15 | 16 |
16 namespace blink { | 17 namespace blink { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 void destroyThread(WebThreadSupportingGC* thread) | 21 class BackingThreadHolder { |
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: | 22 public: |
30 static CompositorWorkerSharedState& instance() | 23 static BackingThreadHolder& instance() |
31 { | 24 { |
32 DEFINE_THREAD_SAFE_STATIC_LOCAL(CompositorWorkerSharedState, compositorW orkerSharedState, (new CompositorWorkerSharedState())); | 25 DEFINE_THREAD_SAFE_STATIC_LOCAL(BackingThreadHolder, holder, new Backing ThreadHolder); |
33 return compositorWorkerSharedState; | 26 return holder; |
34 } | 27 } |
35 | 28 |
36 WebThreadSupportingGC* compositorWorkerThread() | 29 PassRefPtr<WorkerBackingThread> thread() { return m_thread; } |
30 void clear() { m_thread = nullptr; } | |
kinuko
2016/02/29 09:32:54
Who calls this?
yhirano
2016/02/29 23:47:32
I wanted it to be called in the global shutdown se
| |
31 void reset() | |
37 { | 32 { |
38 MutexLocker lock(m_mutex); | 33 ASSERT(!m_thread || (m_thread->workerScriptCount() == 0)); |
39 if (!m_thread && isMainThread()) { | 34 m_thread = WorkerBackingThread::create(Platform::current()->compositorTh read()); |
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 } | 35 } |
127 | 36 |
128 private: | 37 private: |
129 CompositorWorkerSharedState() {} | 38 BackingThreadHolder() |
130 ~CompositorWorkerSharedState() {} | 39 { |
40 reset(); | |
41 } | |
131 | 42 |
132 Mutex m_mutex; | 43 RefPtr<WorkerBackingThread> m_thread; |
133 OwnPtr<WebThreadSupportingGC> m_thread; | |
134 int m_workerCount = 0; | |
135 v8::Isolate* m_isolate = nullptr; | |
136 }; | 44 }; |
137 | 45 |
138 } // namespace | 46 } // namespace |
139 | 47 |
140 PassRefPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor kerLoaderProxy> workerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) | 48 PassRefPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor kerLoaderProxy> workerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) |
141 { | 49 { |
142 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::create"); | 50 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::create"); |
143 ASSERT(isMainThread()); | 51 ASSERT(isMainThread()); |
144 return adoptRef(new CompositorWorkerThread(workerLoaderProxy, workerObjectPr oxy, timeOrigin)); | 52 return adoptRef(new CompositorWorkerThread(workerLoaderProxy, workerObjectPr oxy, timeOrigin)); |
145 } | 53 } |
146 | 54 |
147 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor kerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) | 55 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor kerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) |
148 : WorkerThread(workerLoaderProxy, workerObjectProxy) | 56 : WorkerThread(workerLoaderProxy, workerObjectProxy, BackingThreadHolder::in stance().thread()) |
149 , m_workerObjectProxy(workerObjectProxy) | 57 , m_workerObjectProxy(workerObjectProxy) |
150 , m_timeOrigin(timeOrigin) | 58 , m_timeOrigin(timeOrigin) |
151 { | 59 { |
152 } | 60 } |
153 | 61 |
154 CompositorWorkerThread::~CompositorWorkerThread() | 62 CompositorWorkerThread::~CompositorWorkerThread() |
155 { | 63 { |
156 } | 64 } |
157 | 65 |
158 WebThreadSupportingGC* CompositorWorkerThread::sharedBackingThread() | |
159 { | |
160 return CompositorWorkerSharedState::instance().compositorWorkerThread(); | |
161 } | |
162 | |
163 PassRefPtrWillBeRawPtr<WorkerGlobalScope> CompositorWorkerThread::createWorkerGl obalScope(PassOwnPtr<WorkerThreadStartupData> startupData) | 66 PassRefPtrWillBeRawPtr<WorkerGlobalScope> CompositorWorkerThread::createWorkerGl obalScope(PassOwnPtr<WorkerThreadStartupData> startupData) |
164 { | 67 { |
165 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::createWorkerGlobalScope"); | 68 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::createWorkerGlobalScope"); |
166 return CompositorWorkerGlobalScope::create(this, startupData, m_timeOrigin); | 69 return CompositorWorkerGlobalScope::create(this, startupData, m_timeOrigin); |
167 } | 70 } |
168 | 71 |
169 WebThreadSupportingGC& CompositorWorkerThread::backingThread() | 72 void CompositorWorkerThread::resetSharedBackingThreadForTest() |
170 { | 73 { |
171 return *CompositorWorkerSharedState::instance().compositorWorkerThread(); | 74 BackingThreadHolder::instance().reset(); |
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 } | 75 } |
218 | 76 |
219 } // namespace blink | 77 } // namespace blink |
OLD | NEW |