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

Unified Diff: third_party/WebKit/Source/core/workers/WorkletBackingThreadHolder.h

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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/workers/WorkletBackingThreadHolder.h
diff --git a/third_party/WebKit/Source/core/workers/WorkletBackingThreadHolder.h b/third_party/WebKit/Source/core/workers/WorkletBackingThreadHolder.h
index 2f47c61734dda64b74139cabff3bdf08246560a3..de9c4a52ff7680c72ab9fa0b97792f613e230a05 100644
--- a/third_party/WebKit/Source/core/workers/WorkletBackingThreadHolder.h
+++ b/third_party/WebKit/Source/core/workers/WorkletBackingThreadHolder.h
@@ -6,6 +6,9 @@
#define WorkletBackingThreadHolder_h
#include "core/CoreExport.h"
+#include "core/workers/WorkerBackingThread.h"
+#include "platform/WaitableEvent.h"
+#include "platform/WebThreadSupportingGC.h"
#include "wtf/PtrUtil.h"
namespace blink {
@@ -27,6 +30,89 @@ class CORE_EXPORT WorkletBackingThreadHolder {
bool m_initialized;
};
+// WorkletThreadHolder is a template class which is designed for singleton
+// instance of DereivedWorkletThreadHolder (i.e. AnimationWorkletThreadHolder,
nhiroki 2016/10/19 01:36:49 s/Dereived/Derived/
hongchan 2016/10/24 18:39:21 Done.
+// AudioWorkletThreadHolder).
+template <class DerivedWorkletThreadHolder>
+class CORE_EXPORT WorkletThreadHolder {
+ public:
+ static DerivedWorkletThreadHolder* instance() {
+ MutexLocker locker(holderInstanceMutex());
+ return s_holderInstance;
+ }
+
+ static void ensureInstance() {
+ if (!s_holderInstance)
+ s_holderInstance = new DerivedWorkletThreadHolder;
+ }
+
+ static void clear() {
+ MutexLocker locker(holderInstanceMutex());
+ if (s_holderInstance) {
+ s_holderInstance->shutdownAndWait();
+ delete s_holderInstance;
+ s_holderInstance = nullptr;
+ }
+ }
+
+ static void createForTest(
+ std::unique_ptr<WorkerBackingThread> backingThread) {
+ MutexLocker locker(holderInstanceMutex());
+ DCHECK_EQ(nullptr, s_holderInstance);
nhiroki 2016/10/19 01:36:49 DCHECK(!s_holderInstance)?
hongchan 2016/10/24 18:39:21 Done.
+ s_holderInstance = new DerivedWorkletThreadHolder(std::move(backingThread));
+ }
+
+ WorkerBackingThread* thread() { return m_thread.get(); }
+
+ protected:
+ WorkletThreadHolder(std::unique_ptr<WorkerBackingThread> backingThread)
+ : m_thread(std::move(backingThread)), m_initialized(false) {
+ DCHECK(isMainThread());
+ m_thread->backingThread().postTask(
+ BLINK_FROM_HERE,
+ crossThreadBind(&WorkletThreadHolder::initializeOnThread,
+ crossThreadUnretained(this)));
+ }
+
+ static Mutex& holderInstanceMutex() {
+ DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, holderMutex, new Mutex);
nhiroki 2016/10/19 01:36:49 Just to confirm: Each template class instance's ho
hongchan 2016/10/24 18:39:21 Yes. That is the intention. The same type/class wi
+ return holderMutex;
+ }
+
+ void initializeOnThread() {
+ MutexLocker locker(holderInstanceMutex());
+ DCHECK(!m_initialized);
+ m_thread->initialize();
+ m_initialized = true;
+ }
+
+ void shutdownAndWait() {
+ DCHECK(isMainThread());
+ WaitableEvent waitableEvent;
+ m_thread->backingThread().postTask(
+ BLINK_FROM_HERE,
+ crossThreadBind(&WorkletThreadHolder::shutdownOnThread,
+ crossThreadUnretained(this),
+ crossThreadUnretained(&waitableEvent)));
+ waitableEvent.wait();
+ }
+
+ void shutdownOnThread(WaitableEvent* waitableEvent) {
+ m_thread->shutdown();
+ waitableEvent->signal();
+ }
+
+ private:
+ std::unique_ptr<WorkerBackingThread> m_thread;
+ bool m_initialized;
+
+ static DerivedWorkletThreadHolder* s_holderInstance;
nhiroki 2016/10/19 01:36:49 Just to confirm: Each template class instance has
hongchan 2016/10/24 18:39:21 Yes. As discussed above.
+};
+
+template <class DerivedWorkletThreadHolder>
+DerivedWorkletThreadHolder*
+ WorkletThreadHolder<DerivedWorkletThreadHolder>::s_holderInstance = nullptr;
+
} // namespace blink
#endif // WorkletBackingThreadHolder_h

Powered by Google App Engine
This is Rietveld 408576698