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 m_platformThread = Platform::current()->compositorThread(); | |
sadrul
2015/11/27 19:03:08
It doesn't look like we actually need to remember
Ian Vollick
2015/11/27 19:56:11
Done.
| |
35 m_thread = WebThreadSupportingGC::createForThread(m_platformThread); | |
36 } | |
37 return m_thread.get(); | |
38 } | |
39 | |
40 void initializeBackingThread() | |
41 { | |
42 ASSERT(m_thread->isCurrentThread()); | |
43 MutexLocker lock(m_mutex); | |
sadrul
2015/11/27 19:03:08
Lock first?
Ian Vollick
2015/11/27 19:56:11
Done.
| |
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 // We don't own the compositor thread. | |
127 WebThread* m_platformThread; | |
128 int m_workerCount = 0; | |
129 v8::Isolate* m_isolate = nullptr; | |
130 }; | |
131 | |
132 } // namespace | |
133 | |
17 PassRefPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor kerLoaderProxy> workerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) | 134 PassRefPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor kerLoaderProxy> workerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) |
18 { | 135 { |
136 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::create"); | |
19 ASSERT(isMainThread()); | 137 ASSERT(isMainThread()); |
20 return adoptRef(new CompositorWorkerThread(workerLoaderProxy, workerObjectPr oxy, timeOrigin)); | 138 return adoptRef(new CompositorWorkerThread(workerLoaderProxy, workerObjectPr oxy, timeOrigin)); |
21 } | 139 } |
22 | 140 |
23 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor kerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) | 141 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor kerLoaderProxy, WorkerObjectProxy& workerObjectProxy, double timeOrigin) |
24 : WorkerThread(workerLoaderProxy, workerObjectProxy) | 142 : WorkerThread(workerLoaderProxy, workerObjectProxy) |
25 , m_workerObjectProxy(workerObjectProxy) | 143 , m_workerObjectProxy(workerObjectProxy) |
26 , m_timeOrigin(timeOrigin) | 144 , m_timeOrigin(timeOrigin) |
27 { | 145 { |
28 } | 146 } |
29 | 147 |
30 CompositorWorkerThread::~CompositorWorkerThread() | 148 CompositorWorkerThread::~CompositorWorkerThread() |
31 { | 149 { |
32 } | 150 } |
33 | 151 |
152 WebThreadSupportingGC* CompositorWorkerThread::sharedBackingThread() | |
153 { | |
154 return CompositorWorkerSharedState::instance().compositorWorkerThread(); | |
155 } | |
156 | |
34 PassRefPtrWillBeRawPtr<WorkerGlobalScope> CompositorWorkerThread::createWorkerGl obalScope(PassOwnPtr<WorkerThreadStartupData> startupData) | 157 PassRefPtrWillBeRawPtr<WorkerGlobalScope> CompositorWorkerThread::createWorkerGl obalScope(PassOwnPtr<WorkerThreadStartupData> startupData) |
35 { | 158 { |
159 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::createWorkerGlobalScope"); | |
36 return CompositorWorkerGlobalScope::create(this, startupData, m_timeOrigin); | 160 return CompositorWorkerGlobalScope::create(this, startupData, m_timeOrigin); |
37 } | 161 } |
38 | 162 |
39 WebThreadSupportingGC& CompositorWorkerThread::backingThread() | 163 WebThreadSupportingGC& CompositorWorkerThread::backingThread() |
40 { | 164 { |
41 return CompositorWorkerManager::instance()->compositorWorkerThread(); | 165 return *CompositorWorkerSharedState::instance().compositorWorkerThread(); |
42 } | 166 } |
43 | 167 |
44 void CompositorWorkerThread::initializeBackingThread() | 168 void CompositorWorkerThread::initializeBackingThread() |
45 { | 169 { |
46 CompositorWorkerManager::instance()->initializeBackingThread(); | 170 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::initializeBackingThread"); |
171 CompositorWorkerSharedState::instance().initializeBackingThread(); | |
47 } | 172 } |
48 | 173 |
49 void CompositorWorkerThread::shutdownBackingThread() | 174 void CompositorWorkerThread::shutdownBackingThread() |
50 { | 175 { |
51 CompositorWorkerManager::instance()->shutdownBackingThread(); | 176 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::shutdownBackingThread"); |
177 CompositorWorkerSharedState::instance().shutdownBackingThread(); | |
52 } | 178 } |
53 | 179 |
54 v8::Isolate* CompositorWorkerThread::initializeIsolate() | 180 v8::Isolate* CompositorWorkerThread::initializeIsolate() |
55 { | 181 { |
56 return CompositorWorkerManager::instance()->initializeIsolate(); | 182 return CompositorWorkerSharedState::instance().initializeIsolate(); |
57 } | 183 } |
58 | 184 |
59 void CompositorWorkerThread::willDestroyIsolate() | 185 void CompositorWorkerThread::willDestroyIsolate() |
60 { | 186 { |
61 CompositorWorkerManager::instance()->willDestroyIsolate(); | 187 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::willDestroyIsolate"); |
188 CompositorWorkerSharedState::instance().willDestroyIsolate(); | |
62 } | 189 } |
63 | 190 |
64 void CompositorWorkerThread::destroyIsolate() | 191 void CompositorWorkerThread::destroyIsolate() |
65 { | 192 { |
66 CompositorWorkerManager::instance()->destroyIsolate(); | 193 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::destroyIsolate"); |
194 CompositorWorkerSharedState::instance().destroyIsolate(); | |
67 } | 195 } |
68 | 196 |
69 void CompositorWorkerThread::terminateV8Execution() | 197 void CompositorWorkerThread::terminateV8Execution() |
70 { | 198 { |
71 CompositorWorkerManager::instance()->terminateV8Execution(); | 199 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::terminateV8Execution"); |
200 CompositorWorkerSharedState::instance().terminateV8Execution(); | |
201 } | |
202 | |
203 bool CompositorWorkerThread::hasThreadForTest() | |
204 { | |
205 return CompositorWorkerSharedState::instance().hasThreadForTest(); | |
206 } | |
207 | |
208 bool CompositorWorkerThread::hasIsolateForTest() | |
209 { | |
210 return CompositorWorkerSharedState::instance().hasIsolateForTest(); | |
72 } | 211 } |
73 | 212 |
74 } // namespace blink | 213 } // namespace blink |
OLD | NEW |