| 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..2174288638b9987ce314702c6f969d7380948e92 100644
|
| --- a/third_party/WebKit/Source/core/workers/WorkletBackingThreadHolder.h
|
| +++ b/third_party/WebKit/Source/core/workers/WorkletBackingThreadHolder.h
|
| @@ -6,13 +6,13 @@
|
| #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 {
|
|
|
| -class WorkerBackingThread;
|
| -class WaitableEvent;
|
| -
|
| class CORE_EXPORT WorkletBackingThreadHolder {
|
| public:
|
| WorkerBackingThread* thread() { return m_thread.get(); }
|
| @@ -27,6 +27,89 @@ class CORE_EXPORT WorkletBackingThreadHolder {
|
| bool m_initialized;
|
| };
|
|
|
| +// 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) {
|
| + DCHECK(isMainThread());
|
| + MutexLocker locker(holderInstanceMutex());
|
| + if (s_threadHolderInstance)
|
| + return;
|
| + s_threadHolderInstance = new WorkletThreadHolder<DerivedWorkletThread>;
|
| + s_threadHolderInstance->initialize(
|
| + WorkerBackingThread::create(threadName, threadHeapMode));
|
| + }
|
| +
|
| + 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() {}
|
| +
|
| + 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::shutdownOnThread,
|
| + crossThreadUnretained(this),
|
| + crossThreadUnretained(&waitableEvent)));
|
| + waitableEvent.wait();
|
| + }
|
| +
|
| + void shutdownOnThread(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
|
|
|