Chromium Code Reviews| 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 "public/platform/Platform.h" | 16 #include "public/platform/Platform.h" |
| 16 | 17 |
| 17 namespace blink { | 18 namespace blink { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 22 class BackingThreadHolder; | |
| 23 static BackingThreadHolder* s_holderInstance = nullptr; | |
| 24 | |
| 21 // This is a singleton class holding the compositor worker thread in this | 25 // This is a singleton class holding the compositor worker thread in this |
| 22 // renderrer process. BackingThreadHolst::m_thread will never be cleared, | 26 // renderer process. BackingThreadHolder::m_thread is cleared by |
| 23 // but Oilpan and V8 are detached from the thread when the last compositor | 27 // ModulesInitializer::shutdown. |
| 24 // worker thread is gone. | |
| 25 // See WorkerThread::terminateAndWaitForAllWorkers for the process shutdown | 28 // See WorkerThread::terminateAndWaitForAllWorkers for the process shutdown |
| 26 // case. | 29 // case. |
| 27 class BackingThreadHolder { | 30 class BackingThreadHolder { |
| 28 public: | 31 public: |
| 29 static BackingThreadHolder& instance() | 32 static BackingThreadHolder& instance() |
| 30 { | 33 { |
| 31 DEFINE_THREAD_SAFE_STATIC_LOCAL(BackingThreadHolder, holder, new Backing ThreadHolder); | 34 if (!s_holderInstance) |
|
yhirano
2016/05/19 05:35:37
You need a mutex.
flackr
2016/05/19 22:24:16
Done. But are you sure? It seems to me like instan
haraken
2016/05/20 00:24:44
Yeah, as I commented on https://codereview.chromiu
yhirano
2016/05/20 02:24:54
instance() can be called from worker threads via W
haraken
2016/05/20 02:35:24
If I'm understanding correctly, it *should not* ha
flackr
2016/05/20 15:51:18
Correct, that is how it is supposed to work.
I se
| |
| 32 return holder; | 35 s_holderInstance = new BackingThreadHolder; |
| 36 return *s_holderInstance; | |
| 37 } | |
| 38 | |
| 39 static void clear() | |
| 40 { | |
| 41 if (s_holderInstance) { | |
| 42 s_holderInstance->shutdownAndWait(); | |
| 43 delete s_holderInstance; | |
| 44 s_holderInstance = nullptr; | |
| 45 } | |
| 33 } | 46 } |
| 34 | 47 |
| 35 WorkerBackingThread* thread() { return m_thread.get(); } | 48 WorkerBackingThread* thread() { return m_thread.get(); } |
| 49 | |
| 36 void resetForTest() | 50 void resetForTest() |
| 37 { | 51 { |
| 38 ASSERT(!m_thread || (m_thread->workerScriptCount() == 0)); | 52 ASSERT(!m_thread || (m_thread->workerScriptCount() == 0)); |
| 39 m_thread = nullptr; | |
| 40 m_thread = WorkerBackingThread::createForTest(Platform::current()->compo sitorThread()); | 53 m_thread = WorkerBackingThread::createForTest(Platform::current()->compo sitorThread()); |
| 41 } | 54 } |
| 42 | 55 |
| 43 private: | 56 private: |
| 44 BackingThreadHolder() : m_thread(WorkerBackingThread::create(Platform::curre nt()->compositorThread())) {} | 57 BackingThreadHolder() : m_thread(WorkerBackingThread::create(Platform::curre nt()->compositorThread())) |
| 58 { | |
| 59 Platform::current()->compositorThread()->getWebTaskRunner()->postTask(BL INK_FROM_HERE, threadSafeBind(&BackingThreadHolder::initializeOnThread, AllowCro ssThreadAccess(this))); | |
| 60 } | |
| 61 | |
| 62 void initializeOnThread() | |
|
yhirano
2016/05/19 05:35:37
DCHECK(0, m_thread->workerScriptCount());
flackr
2016/05/19 22:24:16
Done.
| |
| 63 { | |
| 64 m_thread->attach(); | |
| 65 } | |
| 66 | |
| 67 void shutdownAndWait() | |
| 68 { | |
| 69 WaitableEvent doneEvent; | |
| 70 Platform::current()->compositorThread()->getWebTaskRunner()->postTask(BL INK_FROM_HERE, threadSafeBind(&BackingThreadHolder::shutdownOnThread, AllowCross ThreadAccess(&instance()), AllowCrossThreadAccess(&doneEvent))); | |
| 71 doneEvent.wait(); | |
| 72 } | |
| 73 | |
| 74 void shutdownOnThread(WaitableEvent* doneEvent) | |
|
yhirano
2016/05/19 05:35:37
DCHECK(1, m_thread->workerScriptCount());
flackr
2016/05/19 22:24:16
Done.
| |
| 75 { | |
| 76 m_thread->detach(); | |
| 77 doneEvent->signal(); | |
| 78 } | |
| 45 | 79 |
| 46 OwnPtr<WorkerBackingThread> m_thread; | 80 OwnPtr<WorkerBackingThread> m_thread; |
| 47 }; | 81 }; |
| 48 | 82 |
| 49 } // namespace | 83 } // namespace |
| 50 | 84 |
| 51 PassOwnPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor kerLoaderProxy> workerLoaderProxy, InProcessWorkerObjectProxy& workerObjectProxy , double timeOrigin) | 85 PassOwnPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor kerLoaderProxy> workerLoaderProxy, InProcessWorkerObjectProxy& workerObjectProxy , double timeOrigin) |
| 52 { | 86 { |
| 53 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::create"); | 87 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::create"); |
| 54 ASSERT(isMainThread()); | 88 ASSERT(isMainThread()); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 70 { | 104 { |
| 71 return *BackingThreadHolder::instance().thread(); | 105 return *BackingThreadHolder::instance().thread(); |
| 72 } | 106 } |
| 73 | 107 |
| 74 WorkerGlobalScope*CompositorWorkerThread::createWorkerGlobalScope(PassOwnPtr<Wor kerThreadStartupData> startupData) | 108 WorkerGlobalScope*CompositorWorkerThread::createWorkerGlobalScope(PassOwnPtr<Wor kerThreadStartupData> startupData) |
| 75 { | 109 { |
| 76 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::createWorkerGlobalScope"); | 110 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::createWorkerGlobalScope"); |
| 77 return CompositorWorkerGlobalScope::create(this, std::move(startupData), m_t imeOrigin); | 111 return CompositorWorkerGlobalScope::create(this, std::move(startupData), m_t imeOrigin); |
| 78 } | 112 } |
| 79 | 113 |
| 114 void CompositorWorkerThread::ensureSharedBackingThread() | |
| 115 { | |
| 116 BackingThreadHolder::instance(); | |
| 117 } | |
| 118 | |
| 119 void CompositorWorkerThread::clearSharedBackingThread() | |
| 120 { | |
| 121 BackingThreadHolder::clear(); | |
| 122 } | |
| 123 | |
| 80 void CompositorWorkerThread::resetSharedBackingThreadForTest() | 124 void CompositorWorkerThread::resetSharedBackingThreadForTest() |
| 81 { | 125 { |
| 82 BackingThreadHolder::instance().resetForTest(); | 126 BackingThreadHolder::instance().resetForTest(); |
| 83 } | 127 } |
| 84 | 128 |
| 85 } // namespace blink | 129 } // namespace blink |
| OLD | NEW |