Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(527)

Side by Side Diff: third_party/WebKit/Source/modules/compositorworker/CompositorWorkerThread.cpp

Issue 1955693003: compositor-worker: Keep worker backing thread alive for the lifetime of the compositor thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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;
haraken 2016/05/20 00:24:45 Can we make this a static member variable of Backi
flackr 2016/05/20 15:51:18 Done.
24
25 static Mutex& holderInstanceMutex()
26 {
27 DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, holderMutex, new Mutex);
28 return holderMutex;
29 }
30
21 // This is a singleton class holding the compositor worker thread in this 31 // This is a singleton class holding the compositor worker thread in this
22 // renderrer process. BackingThreadHolst::m_thread will never be cleared, 32 // renderer process. BackingThreadHolder::m_thread is cleared by
23 // but Oilpan and V8 are detached from the thread when the last compositor 33 // ModulesInitializer::shutdown.
24 // worker thread is gone.
25 // See WorkerThread::terminateAndWaitForAllWorkers for the process shutdown 34 // See WorkerThread::terminateAndWaitForAllWorkers for the process shutdown
26 // case. 35 // case.
27 class BackingThreadHolder { 36 class BackingThreadHolder {
28 public: 37 public:
29 static BackingThreadHolder& instance() 38 static BackingThreadHolder& instance()
30 { 39 {
31 DEFINE_THREAD_SAFE_STATIC_LOCAL(BackingThreadHolder, holder, new Backing ThreadHolder); 40 MutexLocker locker(holderInstanceMutex());
32 return holder; 41 if (!s_holderInstance)
yhirano 2016/05/20 02:24:55 Can you create another function for ensureBackingT
flackr 2016/05/20 15:51:18 Done.
42 s_holderInstance = new BackingThreadHolder;
43 return *s_holderInstance;
44 }
45
46 static void clear()
47 {
48 MutexLocker locker(holderInstanceMutex());
49 if (s_holderInstance) {
50 s_holderInstance->shutdownAndWait();
51 delete s_holderInstance;
52 s_holderInstance = nullptr;
53 }
33 } 54 }
34 55
35 WorkerBackingThread* thread() { return m_thread.get(); } 56 WorkerBackingThread* thread() { return m_thread.get(); }
57
36 void resetForTest() 58 void resetForTest()
37 { 59 {
38 ASSERT(!m_thread || (m_thread->workerScriptCount() == 0)); 60 ASSERT(!m_thread || (m_thread->workerScriptCount() == 0));
39 m_thread = nullptr;
40 m_thread = WorkerBackingThread::createForTest(Platform::current()->compo sitorThread()); 61 m_thread = WorkerBackingThread::createForTest(Platform::current()->compo sitorThread());
41 } 62 }
42 63
43 private: 64 private:
44 BackingThreadHolder() : m_thread(WorkerBackingThread::create(Platform::curre nt()->compositorThread())) {} 65 BackingThreadHolder() : m_thread(WorkerBackingThread::create(Platform::curre nt()->compositorThread()))
66 {
67 Platform::current()->compositorThread()->getWebTaskRunner()->postTask(BL INK_FROM_HERE, threadSafeBind(&BackingThreadHolder::initializeOnThread, AllowCro ssThreadAccess(this)));
haraken 2016/05/20 00:24:45 Add DCHECK(isMainThread()) (for documentation purp
flackr 2016/05/20 15:51:18 Done.
68 }
69
70 void initializeOnThread()
71 {
72 DCHECK_EQ(0u, m_thread->workerScriptCount()) << "BackingThreadHolder sho uld be the first to attach to WorkerBackingThread";
haraken 2016/05/20 00:24:44 Once we land this CL, it should be guaranteed that
yhirano 2016/05/20 02:24:54 It's now used to see if we need call TerminateExec
haraken 2016/05/20 02:35:24 But this CL makes the TerminateExecution useless,
yhirano 2016/05/20 03:10:15 It depends on whether we want to keep the "current
73 m_thread->attach();
74 }
75
76 void shutdownAndWait()
77 {
78 WaitableEvent doneEvent;
haraken 2016/05/20 00:24:45 Add DCHECK(isMainThread()).
flackr 2016/05/20 15:51:18 Done.
79 Platform::current()->compositorThread()->getWebTaskRunner()->postTask(BL INK_FROM_HERE, threadSafeBind(&BackingThreadHolder::shutdownOnThread, AllowCross ThreadAccess(this), AllowCrossThreadAccess(&doneEvent)));
80 doneEvent.wait();
81 }
82
83 void shutdownOnThread(WaitableEvent* doneEvent)
84 {
85 DCHECK_EQ(1u, m_thread->workerScriptCount()) << "BackingThreadHolder sho uld be the last to detach from WorkerBackingThread";
86 m_thread->detach();
87 doneEvent->signal();
88 }
45 89
46 OwnPtr<WorkerBackingThread> m_thread; 90 OwnPtr<WorkerBackingThread> m_thread;
47 }; 91 };
48 92
49 } // namespace 93 } // namespace
50 94
51 PassOwnPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor kerLoaderProxy> workerLoaderProxy, InProcessWorkerObjectProxy& workerObjectProxy , double timeOrigin) 95 PassOwnPtr<CompositorWorkerThread> CompositorWorkerThread::create(PassRefPtr<Wor kerLoaderProxy> workerLoaderProxy, InProcessWorkerObjectProxy& workerObjectProxy , double timeOrigin)
52 { 96 {
53 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::create"); 97 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::create");
54 ASSERT(isMainThread()); 98 ASSERT(isMainThread());
(...skipping 15 matching lines...) Expand all
70 { 114 {
71 return *BackingThreadHolder::instance().thread(); 115 return *BackingThreadHolder::instance().thread();
72 } 116 }
73 117
74 WorkerGlobalScope*CompositorWorkerThread::createWorkerGlobalScope(PassOwnPtr<Wor kerThreadStartupData> startupData) 118 WorkerGlobalScope*CompositorWorkerThread::createWorkerGlobalScope(PassOwnPtr<Wor kerThreadStartupData> startupData)
75 { 119 {
76 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::createWorkerGlobalScope"); 120 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("compositor-worker"), "CompositorWork erThread::createWorkerGlobalScope");
77 return CompositorWorkerGlobalScope::create(this, std::move(startupData), m_t imeOrigin); 121 return CompositorWorkerGlobalScope::create(this, std::move(startupData), m_t imeOrigin);
78 } 122 }
79 123
124 void CompositorWorkerThread::ensureSharedBackingThread()
125 {
126 BackingThreadHolder::instance();
haraken 2016/05/20 00:24:45 Add DCHECK(isMainThread()).
flackr 2016/05/20 15:51:18 Done.
127 }
128
129 void CompositorWorkerThread::clearSharedBackingThread()
130 {
131 BackingThreadHolder::clear();
haraken 2016/05/20 00:24:45 Add DCHECK(isMainThread()).
flackr 2016/05/20 15:51:18 Done.
132 }
133
80 void CompositorWorkerThread::resetSharedBackingThreadForTest() 134 void CompositorWorkerThread::resetSharedBackingThreadForTest()
81 { 135 {
82 BackingThreadHolder::instance().resetForTest(); 136 BackingThreadHolder::instance().resetForTest();
83 } 137 }
84 138
85 } // namespace blink 139 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698