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

Side by Side Diff: third_party/WebKit/Source/modules/webaudio/AudioWorkletThread.cpp

Issue 2432543002: Refactoring WorkletThreadBackingHolder with template pattern (Closed)
Patch Set: Using Template Created 4 years, 2 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 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/webaudio/AudioWorkletThread.h" 5 #include "modules/webaudio/AudioWorkletThread.h"
6 6
7 #include "core/workers/WorkerBackingThread.h" 7 #include "core/workers/WorkerBackingThread.h"
8 #include "core/workers/WorkerThreadStartupData.h" 8 #include "core/workers/WorkerThreadStartupData.h"
9 #include "core/workers/WorkletBackingThreadHolder.h" 9 #include "core/workers/WorkletBackingThreadHolder.h"
10 #include "modules/webaudio/AudioWorkletGlobalScope.h" 10 #include "modules/webaudio/AudioWorkletGlobalScope.h"
11 #include "platform/CrossThreadFunctional.h" 11 #include "platform/CrossThreadFunctional.h"
12 #include "platform/WaitableEvent.h" 12 #include "platform/WaitableEvent.h"
13 #include "platform/WebThreadSupportingGC.h" 13 #include "platform/WebThreadSupportingGC.h"
14 #include "platform/tracing/TraceEvent.h" 14 #include "platform/tracing/TraceEvent.h"
15 #include "platform/weborigin/SecurityOrigin.h" 15 #include "platform/weborigin/SecurityOrigin.h"
16 #include "public/platform/Platform.h" 16 #include "public/platform/Platform.h"
17 #include "wtf/Assertions.h" 17 #include "wtf/Assertions.h"
18 #include "wtf/PtrUtil.h" 18 #include "wtf/PtrUtil.h"
19 #include <memory> 19 #include <memory>
20 20
21 namespace blink { 21 namespace blink {
22 22
23 namespace { 23 namespace {
24 24
25 // TODO(hongchan): consider refactoring static methods in this class into 25 // AudioWorkletThreadHolder is a singletone container/mananger of a dedicated
26 // a template class. 26 // thread for processing the AudioWorklet.
27 class AudioWorkletThreadHolder final : public WorkletBackingThreadHolder { 27 class AudioWorkletThreadHolder final
28 : public WorkletThreadHolder<AudioWorkletThreadHolder> {
nhiroki 2016/10/19 01:36:49 I'd prefer to avoid class inheritance as much as p
29 friend class WorkletThreadHolder<AudioWorkletThreadHolder>;
30
28 public: 31 public:
29 static AudioWorkletThreadHolder* instance() {
30 MutexLocker locker(holderInstanceMutex());
31 return s_instance;
32 }
33
34 static void ensureInstance() {
35 if (!s_instance)
36 s_instance = new AudioWorkletThreadHolder;
37 }
38
39 static void clear() {
40 MutexLocker locker(holderInstanceMutex());
41 if (s_instance) {
42 s_instance->shutdownAndWait();
43 delete s_instance;
44 s_instance = nullptr;
45 }
46 }
47
48 static void createForTest() { 32 static void createForTest() {
49 MutexLocker locker(holderInstanceMutex()); 33 WorkletThreadHolder<AudioWorkletThreadHolder>::createForTest(
50 DCHECK_EQ(nullptr, s_instance); 34 WorkerBackingThread::createForTest("AudioWorkletThread",
51 s_instance = 35 BlinkGC::PerThreadHeapMode));
52 new AudioWorkletThreadHolder(WorkerBackingThread::createForTest(
53 "AudioWorkletThread", BlinkGC::PerThreadHeapMode));
54 } 36 }
55 37
56 private: 38 private:
57 AudioWorkletThreadHolder( 39 AudioWorkletThreadHolder(
58 std::unique_ptr<WorkerBackingThread> backingThread = nullptr) 40 std::unique_ptr<WorkerBackingThread> backingThread = nullptr)
59 : WorkletBackingThreadHolder( 41 : WorkletThreadHolder(backingThread ? std::move(backingThread)
60 backingThread 42 : WorkerBackingThread::create(
61 ? std::move(backingThread) 43 "AudioWorkletThread",
62 : WorkerBackingThread::create("AudioWorkletThread", 44 BlinkGC::PerThreadHeapMode)) {}
63 BlinkGC::PerThreadHeapMode)) {}
64
65 static Mutex& holderInstanceMutex() {
66 DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, holderMutex, new Mutex);
67 return holderMutex;
68 }
69
70 void initializeOnThread() {
71 MutexLocker locker(holderInstanceMutex());
72 DCHECK(!m_initialized);
73 m_thread->initialize();
74 m_initialized = true;
75 }
76
77 static AudioWorkletThreadHolder* s_instance;
78 }; 45 };
79 46
80 AudioWorkletThreadHolder* AudioWorkletThreadHolder::s_instance = nullptr;
81
82 } // namespace 47 } // namespace
83 48
84 std::unique_ptr<AudioWorkletThread> AudioWorkletThread::create( 49 std::unique_ptr<AudioWorkletThread> AudioWorkletThread::create(
85 PassRefPtr<WorkerLoaderProxy> workerLoaderProxy, 50 PassRefPtr<WorkerLoaderProxy> workerLoaderProxy,
86 WorkerReportingProxy& workerReportingProxy) { 51 WorkerReportingProxy& workerReportingProxy) {
87 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("audio-worklet"), 52 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("audio-worklet"),
88 "AudioWorkletThread::create"); 53 "AudioWorkletThread::create");
89 DCHECK(isMainThread()); 54 DCHECK(isMainThread());
90 return wrapUnique(new AudioWorkletThread(std::move(workerLoaderProxy), 55 return wrapUnique(new AudioWorkletThread(std::move(workerLoaderProxy),
91 workerReportingProxy)); 56 workerReportingProxy));
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 securityOrigin->transferPrivilegesFrom( 109 securityOrigin->transferPrivilegesFrom(
145 std::move(startupData->m_starterOriginPrivilegeData)); 110 std::move(startupData->m_starterOriginPrivilegeData));
146 } 111 }
147 112
148 return AudioWorkletGlobalScope::create( 113 return AudioWorkletGlobalScope::create(
149 startupData->m_scriptURL, startupData->m_userAgent, 114 startupData->m_scriptURL, startupData->m_userAgent,
150 securityOrigin.release(), this->isolate(), this); 115 securityOrigin.release(), this->isolate(), this);
151 } 116 }
152 117
153 } // namespace blink 118 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698