Chromium Code Reviews| Index: third_party/WebKit/Source/core/workers/WorkletThreadHolder.h |
| diff --git a/third_party/WebKit/Source/core/workers/WorkletThreadHolder.h b/third_party/WebKit/Source/core/workers/WorkletThreadHolder.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..044b00eca61e44ceb8b37b0d25dace8b92bf397a |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/workers/WorkletThreadHolder.h |
| @@ -0,0 +1,131 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef WorkletThreadHolder_h |
| +#define WorkletThreadHolder_h |
| + |
| +#include "core/CoreExport.h" |
| +#include "core/workers/WorkerBackingThread.h" |
| +#include "platform/WaitableEvent.h" |
| +#include "platform/WebThreadSupportingGC.h" |
| +#include "wtf/PtrUtil.h" |
| + |
| +namespace blink { |
| + |
| +class WorkerBackingThread; |
| +class WaitableEvent; |
|
nhiroki
2016/10/27 00:03:35
There are header inclusions for these classes.
hongchan
2016/10/27 17:31:06
Done.
|
| + |
| +// WorkletThreadHolder is a template class which is designed for singleton |
| +// instance of DerivedWorkletThread (i.e. AnimationWorkletThread, |
| +// AudioWorkletThread). |
| +template <class DerivedWorkletThread> |
| +class WorkletThreadHolder { |
| + public: |
| + static WorkletThreadHolder<DerivedWorkletThread>* threadHolderInstance() { |
| + MutexLocker locker(holderInstanceMutex()); |
| + return s_threadHolderInstance; |
| + } |
| + |
| + static void ensureInstance(const char* threadName, |
| + BlinkGC::ThreadHeapMode ThreadHeapMode) { |
|
nhiroki
2016/10/27 00:03:35
s/ThreadHeapMode/threadHeapMode/
hongchan
2016/10/27 17:31:06
Done.
|
| + DCHECK(isMainThread()); |
| + MutexLocker locker(holderInstanceMutex()); |
| + if (s_threadHolderInstance) |
| + return; |
| + s_threadHolderInstance = new WorkletThreadHolder<DerivedWorkletThread>; |
| + s_threadHolderInstance->initialize( |
| + WorkerBackingThread::create(threadName, ThreadHeapMode)); |
| + } |
| + |
| + static void ensureInstance(WebThread* thread) { |
| + DCHECK(isMainThread()); |
| + MutexLocker locker(holderInstanceMutex()); |
| + if (s_threadHolderInstance) |
| + return; |
| + s_threadHolderInstance = new WorkletThreadHolder<DerivedWorkletThread>; |
| + s_threadHolderInstance->initialize(WorkerBackingThread::create(thread)); |
| + } |
| + |
| + static void createForTest(const char* threadName, |
| + BlinkGC::ThreadHeapMode ThreadHeapMode) { |
|
nhiroki
2016/10/27 00:03:35
s/ThreadHeapMode/threadHeapMode/
hongchan
2016/10/27 17:31:06
Done.
|
| + MutexLocker locker(holderInstanceMutex()); |
| + DCHECK_EQ(nullptr, s_threadHolderInstance); |
|
nhiroki
2016/10/27 00:03:35
DCHECK(s_threadHolderInstace) would be simpler
nhiroki
2016/10/27 00:04:20
DCHECK(!s_threadHolderInstace)
hongchan
2016/10/27 17:31:06
Done.
|
| + s_threadHolderInstance = new WorkletThreadHolder<DerivedWorkletThread>; |
| + s_threadHolderInstance->initialize( |
| + WorkerBackingThread::createForTest(threadName, ThreadHeapMode)); |
| + } |
| + |
| + static void createForTest(WebThread* thread) { |
| + MutexLocker locker(holderInstanceMutex()); |
| + DCHECK_EQ(nullptr, s_threadHolderInstance); |
| + s_threadHolderInstance = new WorkletThreadHolder<DerivedWorkletThread>; |
| + s_threadHolderInstance->initialize( |
| + WorkerBackingThread::createForTest(thread)); |
| + } |
| + |
| + static void clearInstance() { |
| + DCHECK(isMainThread()); |
| + MutexLocker locker(holderInstanceMutex()); |
| + if (s_threadHolderInstance) { |
| + s_threadHolderInstance->shutdownAndWait(); |
| + delete s_threadHolderInstance; |
| + s_threadHolderInstance = nullptr; |
| + } |
| + } |
| + |
| + WorkerBackingThread* thread() { return m_thread.get(); } |
| + |
| + private: |
| + WorkletThreadHolder() {} |
| + ~WorkletThreadHolder() {} |
| + |
| + static Mutex& holderInstanceMutex() { |
| + DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, holderMutex, new Mutex); |
| + return holderMutex; |
| + } |
| + |
| + void initialize(std::unique_ptr<WorkerBackingThread> backingThread) { |
| + m_thread = std::move(backingThread); |
| + m_thread->backingThread().postTask( |
| + BLINK_FROM_HERE, |
| + crossThreadBind(&WorkletThreadHolder::initializeOnWorkletThread, |
| + crossThreadUnretained(this))); |
| + } |
| + |
| + void initializeOnWorkletThread() { |
| + 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::shutdownOnWorlketThread, |
| + crossThreadUnretained(this), |
| + crossThreadUnretained(&waitableEvent))); |
| + waitableEvent.wait(); |
| + } |
| + |
| + void shutdownOnWorlketThread(WaitableEvent* waitableEvent) { |
| + m_thread->shutdown(); |
| + waitableEvent->signal(); |
| + } |
| + |
| + std::unique_ptr<WorkerBackingThread> m_thread; |
| + bool m_initialized = false; |
| + |
| + static WorkletThreadHolder<DerivedWorkletThread>* s_threadHolderInstance; |
| +}; |
| + |
| +template <class DerivedWorkletThread> |
| +WorkletThreadHolder<DerivedWorkletThread>* |
| + WorkletThreadHolder<DerivedWorkletThread>::s_threadHolderInstance = nullptr; |
| + |
| +} // namespace blink |
| + |
| +#endif // WorkletBackingThreadHolder_h |