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