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/InProcessWorkerObjectProxy.h" | 9 #include "core/workers/InProcessWorkerObjectProxy.h" |
10 #include "core/workers/WorkerBackingThread.h" | 10 #include "core/workers/WorkerBackingThread.h" |
11 #include "core/workers/WorkerThreadStartupData.h" | 11 #include "core/workers/WorkerThreadStartupData.h" |
12 #include "modules/compositorworker/CompositorWorkerGlobalScope.h" | 12 #include "modules/compositorworker/CompositorWorkerGlobalScope.h" |
13 #include "platform/ThreadSafeFunctional.h" | 13 #include "platform/ThreadSafeFunctional.h" |
14 #include "platform/TraceEvent.h" | 14 #include "platform/TraceEvent.h" |
15 #include "platform/WaitableEvent.h" | 15 #include "platform/WaitableEvent.h" |
16 #include "platform/WebThreadSupportingGC.h" | 16 #include "platform/WebThreadSupportingGC.h" |
17 #include "public/platform/Platform.h" | 17 #include "public/platform/Platform.h" |
| 18 #include "wtf/Assertions.h" |
18 | 19 |
19 namespace blink { | 20 namespace blink { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 // This is a singleton class holding the compositor worker thread in this | 24 // This is a singleton class holding the compositor worker thread in this |
24 // renderer process. BackingThreadHolder::m_thread is cleared by | 25 // renderer process. BackingThreadHolder::m_thread is cleared by |
25 // ModulesInitializer::shutdown. | 26 // ModulesInitializer::shutdown. |
26 // See WorkerThread::terminateAndWaitForAllWorkers for the process shutdown | 27 // See WorkerThread::terminateAndWaitForAllWorkers for the process shutdown |
27 // case. | 28 // case. |
28 class BackingThreadHolder { | 29 class BackingThreadHolder { |
29 public: | 30 public: |
30 static BackingThreadHolder& instance() | 31 static BackingThreadHolder& instance() |
31 { | 32 { |
32 MutexLocker locker(holderInstanceMutex()); | 33 MutexLocker locker(holderInstanceMutex()); |
33 return *s_instance; | 34 return *s_instance; |
34 } | 35 } |
35 | 36 |
36 static void ensureInstance() | 37 static void ensureInstance() |
37 { | 38 { |
38 if (!s_instance) | 39 if (!s_instance) |
39 s_instance = new BackingThreadHolder; | 40 s_instance = new BackingThreadHolder; |
40 } | 41 } |
41 | 42 |
42 static void terminateExecution() | |
43 { | |
44 MutexLocker locker(holderInstanceMutex()); | |
45 if (s_instance && s_instance->m_initialized) { | |
46 s_instance->thread()->isolate()->TerminateExecution(); | |
47 s_instance->m_terminatingExecution = true; | |
48 } | |
49 } | |
50 | |
51 static void clear() | 43 static void clear() |
52 { | 44 { |
53 MutexLocker locker(holderInstanceMutex()); | 45 MutexLocker locker(holderInstanceMutex()); |
54 if (s_instance) { | 46 if (s_instance) { |
55 DCHECK(!s_instance->m_initialized || s_instance->m_terminatingExecut
ion); | |
56 s_instance->shutdownAndWait(); | 47 s_instance->shutdownAndWait(); |
57 delete s_instance; | 48 delete s_instance; |
58 s_instance = nullptr; | 49 s_instance = nullptr; |
59 } | 50 } |
60 } | 51 } |
61 | 52 |
62 static void createForTest() | 53 static void createForTest() |
63 { | 54 { |
64 MutexLocker locker(holderInstanceMutex()); | 55 MutexLocker locker(holderInstanceMutex()); |
65 DCHECK_EQ(nullptr, s_instance); | 56 DCHECK_EQ(nullptr, s_instance); |
(...skipping 12 matching lines...) Expand all Loading... |
78 | 69 |
79 static Mutex& holderInstanceMutex() | 70 static Mutex& holderInstanceMutex() |
80 { | 71 { |
81 DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, holderMutex, new Mutex); | 72 DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, holderMutex, new Mutex); |
82 return holderMutex; | 73 return holderMutex; |
83 } | 74 } |
84 | 75 |
85 void initializeOnThread() | 76 void initializeOnThread() |
86 { | 77 { |
87 MutexLocker locker(holderInstanceMutex()); | 78 MutexLocker locker(holderInstanceMutex()); |
88 DCHECK_EQ(0u, m_thread->workerScriptCount()) << "BackingThreadHolder sho
uld be the first to attach to WorkerBackingThread"; | 79 DCHECK(!m_initialized); |
89 m_thread->attach(); | 80 m_thread->initialize(); |
90 m_initialized = true; | 81 m_initialized = true; |
91 } | 82 } |
92 | 83 |
93 void shutdownAndWait() | 84 void shutdownAndWait() |
94 { | 85 { |
95 DCHECK(isMainThread()); | 86 DCHECK(isMainThread()); |
96 WaitableEvent doneEvent; | 87 WaitableEvent doneEvent; |
97 m_thread->backingThread().postTask(BLINK_FROM_HERE, threadSafeBind(&Back
ingThreadHolder::shutdownOnThread, AllowCrossThreadAccess(this), AllowCrossThrea
dAccess(&doneEvent))); | 88 m_thread->backingThread().postTask(BLINK_FROM_HERE, threadSafeBind(&Back
ingThreadHolder::shutdownOnThread, AllowCrossThreadAccess(this), AllowCrossThrea
dAccess(&doneEvent))); |
98 doneEvent.wait(); | 89 doneEvent.wait(); |
99 } | 90 } |
100 | 91 |
101 void shutdownOnThread(WaitableEvent* doneEvent) | 92 void shutdownOnThread(WaitableEvent* doneEvent) |
102 { | 93 { |
103 DCHECK_EQ(1u, m_thread->workerScriptCount()) << "BackingThreadHolder sho
uld be the last to detach from WorkerBackingThread"; | 94 m_thread->shutdown(); |
104 m_thread->detach(); | |
105 doneEvent->signal(); | 95 doneEvent->signal(); |
106 } | 96 } |
107 | 97 |
108 OwnPtr<WorkerBackingThread> m_thread; | 98 OwnPtr<WorkerBackingThread> m_thread; |
109 bool m_terminatingExecution = false; | |
110 bool m_initialized = false; | 99 bool m_initialized = false; |
111 | 100 |
112 static BackingThreadHolder* s_instance; | 101 static BackingThreadHolder* s_instance; |
113 }; | 102 }; |
114 | 103 |
115 BackingThreadHolder* BackingThreadHolder::s_instance = nullptr; | 104 BackingThreadHolder* BackingThreadHolder::s_instance = nullptr; |
116 | 105 |
117 } // namespace | 106 } // namespace |
118 | 107 |
119 PassOwnPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor
kerLoaderProxy> workerLoaderProxy, InProcessWorkerObjectProxy& workerObjectProxy
, double timeOrigin) | 108 PassOwnPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor
kerLoaderProxy> workerLoaderProxy, InProcessWorkerObjectProxy& workerObjectProxy
, double timeOrigin) |
(...skipping 24 matching lines...) Expand all Loading... |
144 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::createWorkerGlobalScope"); | 133 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork
erThread::createWorkerGlobalScope"); |
145 return CompositorWorkerGlobalScope::create(this, std::move(startupData), m_t
imeOrigin); | 134 return CompositorWorkerGlobalScope::create(this, std::move(startupData), m_t
imeOrigin); |
146 } | 135 } |
147 | 136 |
148 void CompositorWorkerThread::ensureSharedBackingThread() | 137 void CompositorWorkerThread::ensureSharedBackingThread() |
149 { | 138 { |
150 DCHECK(isMainThread()); | 139 DCHECK(isMainThread()); |
151 BackingThreadHolder::ensureInstance(); | 140 BackingThreadHolder::ensureInstance(); |
152 } | 141 } |
153 | 142 |
154 void CompositorWorkerThread::terminateExecution() | |
155 { | |
156 DCHECK(isMainThread()); | |
157 BackingThreadHolder::terminateExecution(); | |
158 } | |
159 | |
160 void CompositorWorkerThread::clearSharedBackingThread() | 143 void CompositorWorkerThread::clearSharedBackingThread() |
161 { | 144 { |
162 DCHECK(isMainThread()); | 145 DCHECK(isMainThread()); |
163 BackingThreadHolder::clear(); | 146 BackingThreadHolder::clear(); |
164 } | 147 } |
165 | 148 |
166 void CompositorWorkerThread::createSharedBackingThreadForTest() | 149 void CompositorWorkerThread::createSharedBackingThreadForTest() |
167 { | 150 { |
168 BackingThreadHolder::createForTest(); | 151 BackingThreadHolder::createForTest(); |
169 } | 152 } |
170 | 153 |
171 } // namespace blink | 154 } // namespace blink |
OLD | NEW |