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

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

Issue 2432543002: Refactoring WorkletThreadBackingHolder with template pattern (Closed)
Patch Set: Fixing typos Created 4 years, 1 month 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/AbstractAnimationWorkletThread.h" 5 #include "modules/compositorworker/AbstractAnimationWorkletThread.h"
6 6
7 #include "core/workers/WorkerBackingThread.h" 7 #include "core/workers/WorkerBackingThread.h"
8 #include "core/workers/WorkletBackingThreadHolder.h" 8 #include "core/workers/WorkletThreadHolder.h"
9 #include "platform/CrossThreadFunctional.h" 9 #include "platform/CrossThreadFunctional.h"
10 #include "platform/WebThreadSupportingGC.h" 10 #include "platform/WebThreadSupportingGC.h"
11 #include "public/platform/Platform.h" 11 #include "public/platform/Platform.h"
12 #include "wtf/Assertions.h" 12 #include "wtf/Assertions.h"
13 #include "wtf/PtrUtil.h" 13 #include "wtf/PtrUtil.h"
14 #include <memory> 14 #include <memory>
15 15
16 namespace blink { 16 namespace blink {
17 17
18 namespace { 18 template class WorkletThreadHolder<AbstractAnimationWorkletThread>;
19
20 // This is a singleton class holding the animation worklet thread in this
21 // renderer process. WorkletBackingThreadHolder::m_thread is cleared by
22 // ModulesInitializer::shutdown.
23 //
24 // See WorkerThread::terminateAndWaitForAllWorkers for the process shutdown
25 // case.
26 //
27 // TODO(hongchan): consider refactoring static methods in this class into
28 // a template class.
29 class AnimationWorkletBackingThreadHolder final
30 : public WorkletBackingThreadHolder {
31 public:
32 static AnimationWorkletBackingThreadHolder* instance() {
33 MutexLocker locker(holderInstanceMutex());
34 return s_instance;
35 }
36
37 static void ensureInstance() {
38 if (!s_instance)
39 s_instance = new AnimationWorkletBackingThreadHolder;
40 }
41
42 static void clear() {
43 MutexLocker locker(holderInstanceMutex());
44 if (s_instance) {
45 s_instance->shutdownAndWait();
46 delete s_instance;
47 s_instance = nullptr;
48 }
49 }
50
51 static void createForTest() {
52 MutexLocker locker(holderInstanceMutex());
53 DCHECK_EQ(nullptr, s_instance);
54 s_instance = new AnimationWorkletBackingThreadHolder(
55 WorkerBackingThread::createForTest(
56 Platform::current()->compositorThread()));
57 }
58
59 private:
60 AnimationWorkletBackingThreadHolder(
61 std::unique_ptr<WorkerBackingThread> backingThread = nullptr)
62 : WorkletBackingThreadHolder(
63 backingThread ? std::move(backingThread)
64 : WorkerBackingThread::create(
65 Platform::current()->compositorThread())) {}
66
67 static Mutex& holderInstanceMutex() {
68 DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, holderMutex, new Mutex);
69 return holderMutex;
70 }
71
72 void initializeOnThread() final {
73 MutexLocker locker(holderInstanceMutex());
74 DCHECK(!m_initialized);
75 m_thread->initialize();
76 m_initialized = true;
77 }
78
79 static AnimationWorkletBackingThreadHolder* s_instance;
80 };
81
82 AnimationWorkletBackingThreadHolder*
83 AnimationWorkletBackingThreadHolder::s_instance = nullptr;
84
85 } // namespace
86 19
87 AbstractAnimationWorkletThread::AbstractAnimationWorkletThread( 20 AbstractAnimationWorkletThread::AbstractAnimationWorkletThread(
88 PassRefPtr<WorkerLoaderProxy> workerLoaderProxy, 21 PassRefPtr<WorkerLoaderProxy> workerLoaderProxy,
89 WorkerReportingProxy& workerReportingProxy) 22 WorkerReportingProxy& workerReportingProxy)
90 : WorkerThread(std::move(workerLoaderProxy), workerReportingProxy) {} 23 : WorkerThread(std::move(workerLoaderProxy), workerReportingProxy) {}
91 24
92 AbstractAnimationWorkletThread::~AbstractAnimationWorkletThread() {} 25 AbstractAnimationWorkletThread::~AbstractAnimationWorkletThread() {}
93 26
94 WorkerBackingThread& AbstractAnimationWorkletThread::workerBackingThread() { 27 WorkerBackingThread& AbstractAnimationWorkletThread::workerBackingThread() {
95 return *AnimationWorkletBackingThreadHolder::instance()->thread(); 28 return *WorkletThreadHolder<
29 AbstractAnimationWorkletThread>::threadHolderInstance()
30 ->thread();
96 } 31 }
97 32
98 void collectAllGarbageOnThread(WaitableEvent* doneEvent) { 33 void collectAllGarbageOnThread(WaitableEvent* doneEvent) {
99 blink::ThreadState::current()->collectAllGarbage(); 34 blink::ThreadState::current()->collectAllGarbage();
100 doneEvent->signal(); 35 doneEvent->signal();
101 } 36 }
102 37
103 void AbstractAnimationWorkletThread::collectAllGarbage() { 38 void AbstractAnimationWorkletThread::collectAllGarbage() {
104 DCHECK(isMainThread()); 39 DCHECK(isMainThread());
105 WaitableEvent doneEvent; 40 WaitableEvent doneEvent;
106 AnimationWorkletBackingThreadHolder* instance = 41 WorkletThreadHolder<AbstractAnimationWorkletThread>* threadHolderInstance =
107 AnimationWorkletBackingThreadHolder::instance(); 42 WorkletThreadHolder<
108 if (!instance) 43 AbstractAnimationWorkletThread>::threadHolderInstance();
44 if (!threadHolderInstance)
109 return; 45 return;
110 instance->thread()->backingThread().postTask( 46 threadHolderInstance->thread()->backingThread().postTask(
111 BLINK_FROM_HERE, crossThreadBind(&collectAllGarbageOnThread, 47 BLINK_FROM_HERE, crossThreadBind(&collectAllGarbageOnThread,
112 crossThreadUnretained(&doneEvent))); 48 crossThreadUnretained(&doneEvent)));
113 doneEvent.wait(); 49 doneEvent.wait();
114 } 50 }
115 51
116 void AbstractAnimationWorkletThread::ensureSharedBackingThread() { 52 void AbstractAnimationWorkletThread::ensureSharedBackingThread() {
117 DCHECK(isMainThread()); 53 DCHECK(isMainThread());
118 AnimationWorkletBackingThreadHolder::ensureInstance(); 54 WorkletThreadHolder<AbstractAnimationWorkletThread>::ensureInstance(
55 Platform::current()->compositorThread());
119 } 56 }
120 57
121 void AbstractAnimationWorkletThread::clearSharedBackingThread() { 58 void AbstractAnimationWorkletThread::clearSharedBackingThread() {
122 DCHECK(isMainThread()); 59 DCHECK(isMainThread());
123 AnimationWorkletBackingThreadHolder::clear(); 60 WorkletThreadHolder<AbstractAnimationWorkletThread>::clearInstance();
124 } 61 }
125 62
126 void AbstractAnimationWorkletThread::createSharedBackingThreadForTest() { 63 void AbstractAnimationWorkletThread::createSharedBackingThreadForTest() {
127 AnimationWorkletBackingThreadHolder::createForTest(); 64 WorkletThreadHolder<AbstractAnimationWorkletThread>::createForTest(
65 Platform::current()->compositorThread());
128 } 66 }
129 67
130 } // namespace blink 68 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698