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 |
21 // This is a singleton class holding the compositor worker thread in this | 22 // This is a singleton class holding the compositor worker thread in this |
22 // renderrer process. BackingThreadHolst::m_thread will never be cleared, | 23 // renderer process. BackingThreadHolder::m_thread is cleared by |
23 // but Oilpan and V8 are detached from the thread when the last compositor | 24 // ModulesInitializer::shutdown. |
24 // worker thread is gone. | |
25 // See WorkerThread::terminateAndWaitForAllWorkers for the process shutdown | 25 // See WorkerThread::terminateAndWaitForAllWorkers for the process shutdown |
26 // case. | 26 // case. |
27 class BackingThreadHolder { | 27 class BackingThreadHolder { |
28 public: | 28 public: |
29 static BackingThreadHolder& instance() | 29 static BackingThreadHolder& instance() |
30 { | 30 { |
31 DEFINE_THREAD_SAFE_STATIC_LOCAL(BackingThreadHolder, holder, new Backing ThreadHolder); | 31 MutexLocker locker(holderInstanceMutex()); |
32 return holder; | 32 return *s_instance; |
33 } | |
34 | |
35 static void ensureInstance() | |
36 { | |
37 MutexLocker locker(holderInstanceMutex()); | |
kinuko
2016/05/23 09:53:55
I'd like to remove this lock if it shouldn't be ne
flackr
2016/05/25 16:42:52
Given s_instance is cleared on main after a synchr
| |
38 if (!s_instance) | |
39 s_instance = new BackingThreadHolder; | |
40 } | |
41 | |
42 static void clear() | |
43 { | |
44 MutexLocker locker(holderInstanceMutex()); | |
45 if (s_instance) { | |
46 s_instance->thread()->isolate()->TerminateExecution(); | |
47 s_instance->shutdownAndWait(); | |
48 delete s_instance; | |
49 s_instance = nullptr; | |
50 } | |
51 } | |
52 | |
53 static void createForTest() | |
54 { | |
55 MutexLocker locker(holderInstanceMutex()); | |
56 DCHECK_EQ(nullptr, s_instance); | |
57 s_instance = new BackingThreadHolder(WorkerBackingThread::createForTest( Platform::current()->compositorThread())); | |
flackr
2016/05/20 15:51:18
I didn't like how resetForTest swapped out the m_t
| |
33 } | 58 } |
34 | 59 |
35 WorkerBackingThread* thread() { return m_thread.get(); } | 60 WorkerBackingThread* thread() { return m_thread.get(); } |
36 void resetForTest() | 61 |
62 private: | |
63 BackingThreadHolder(PassOwnPtr<WorkerBackingThread> useBackingThread = nullp tr) | |
64 : m_thread(useBackingThread ? useBackingThread.release() : WorkerBacking Thread::create(Platform::current()->compositorThread())) | |
37 { | 65 { |
38 ASSERT(!m_thread || (m_thread->workerScriptCount() == 0)); | 66 DCHECK(isMainThread()); |
39 m_thread = nullptr; | 67 Platform::current()->compositorThread()->getWebTaskRunner()->postTask(BL INK_FROM_HERE, threadSafeBind(&BackingThreadHolder::initializeOnThread, AllowCro ssThreadAccess(this))); |
40 m_thread = WorkerBackingThread::createForTest(Platform::current()->compo sitorThread()); | |
41 } | 68 } |
42 | 69 |
43 private: | 70 static Mutex& holderInstanceMutex() |
44 BackingThreadHolder() : m_thread(WorkerBackingThread::create(Platform::curre nt()->compositorThread())) {} | 71 { |
72 DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, holderMutex, new Mutex); | |
73 return holderMutex; | |
74 } | |
75 | |
76 void initializeOnThread() | |
77 { | |
78 DCHECK_EQ(0u, m_thread->workerScriptCount()) << "BackingThreadHolder sho uld be the first to attach to WorkerBackingThread"; | |
79 m_thread->attach(); | |
80 } | |
81 | |
82 void shutdownAndWait() | |
83 { | |
84 DCHECK(isMainThread()); | |
85 WaitableEvent doneEvent; | |
86 Platform::current()->compositorThread()->getWebTaskRunner()->postTask(BL INK_FROM_HERE, threadSafeBind(&BackingThreadHolder::shutdownOnThread, AllowCross ThreadAccess(this), AllowCrossThreadAccess(&doneEvent))); | |
87 doneEvent.wait(); | |
88 } | |
89 | |
90 void shutdownOnThread(WaitableEvent* doneEvent) | |
91 { | |
92 DCHECK_EQ(1u, m_thread->workerScriptCount()) << "BackingThreadHolder sho uld be the last to detach from WorkerBackingThread"; | |
93 m_thread->detach(); | |
94 doneEvent->signal(); | |
95 } | |
45 | 96 |
46 OwnPtr<WorkerBackingThread> m_thread; | 97 OwnPtr<WorkerBackingThread> m_thread; |
98 | |
99 static BackingThreadHolder* s_instance; | |
47 }; | 100 }; |
48 | 101 |
102 BackingThreadHolder* BackingThreadHolder::s_instance = nullptr; | |
103 | |
49 } // namespace | 104 } // namespace |
50 | 105 |
51 PassOwnPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor kerLoaderProxy> workerLoaderProxy, InProcessWorkerObjectProxy& workerObjectProxy , double timeOrigin) | 106 PassOwnPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor kerLoaderProxy> workerLoaderProxy, InProcessWorkerObjectProxy& workerObjectProxy , double timeOrigin) |
52 { | 107 { |
53 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::create"); | 108 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::create"); |
54 ASSERT(isMainThread()); | 109 ASSERT(isMainThread()); |
55 return adoptPtr(new CompositorWorkerThread(workerLoaderProxy, workerObjectPr oxy, timeOrigin)); | 110 return adoptPtr(new CompositorWorkerThread(workerLoaderProxy, workerObjectPr oxy, timeOrigin)); |
56 } | 111 } |
57 | 112 |
58 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor kerLoaderProxy, InProcessWorkerObjectProxy& workerObjectProxy, double timeOrigin ) | 113 CompositorWorkerThread::CompositorWorkerThread(PassRefPtr<WorkerLoaderProxy> wor kerLoaderProxy, InProcessWorkerObjectProxy& workerObjectProxy, double timeOrigin ) |
(...skipping 11 matching lines...) Expand all Loading... | |
70 { | 125 { |
71 return *BackingThreadHolder::instance().thread(); | 126 return *BackingThreadHolder::instance().thread(); |
72 } | 127 } |
73 | 128 |
74 WorkerGlobalScope*CompositorWorkerThread::createWorkerGlobalScope(PassOwnPtr<Wor kerThreadStartupData> startupData) | 129 WorkerGlobalScope*CompositorWorkerThread::createWorkerGlobalScope(PassOwnPtr<Wor kerThreadStartupData> startupData) |
75 { | 130 { |
76 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::createWorkerGlobalScope"); | 131 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::createWorkerGlobalScope"); |
77 return CompositorWorkerGlobalScope::create(this, std::move(startupData), m_t imeOrigin); | 132 return CompositorWorkerGlobalScope::create(this, std::move(startupData), m_t imeOrigin); |
78 } | 133 } |
79 | 134 |
80 void CompositorWorkerThread::resetSharedBackingThreadForTest() | 135 void CompositorWorkerThread::ensureSharedBackingThread() |
81 { | 136 { |
82 BackingThreadHolder::instance().resetForTest(); | 137 DCHECK(isMainThread()); |
138 BackingThreadHolder::ensureInstance(); | |
139 } | |
140 | |
141 void CompositorWorkerThread::clearSharedBackingThread() | |
142 { | |
143 DCHECK(isMainThread()); | |
144 BackingThreadHolder::clear(); | |
145 } | |
146 | |
147 void CompositorWorkerThread::createSharedBackingThreadForTest() | |
148 { | |
149 BackingThreadHolder::createForTest(); | |
83 } | 150 } |
84 | 151 |
85 } // namespace blink | 152 } // namespace blink |
OLD | NEW |