17242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci// Copyright 2014 The Chromium Authors. All rights reserved.
27242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci// Use of this source code is governed by a BSD-style license that can be
37242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci// found in the LICENSE file.
47242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
57242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#ifndef WebThreadSupportingGC_h
67242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#define WebThreadSupportingGC_h
77242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
87242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "platform/heap/glue/MessageLoopInterruptor.h"
97242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "platform/heap/glue/PendingGCRunner.h"
107242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "public/platform/Platform.h"
117242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "public/platform/WebThread.h"
127242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "wtf/Noncopyable.h"
137242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "wtf/OwnPtr.h"
147242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "wtf/PassOwnPtr.h"
157242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
167242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tuccinamespace blink {
177242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
187242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci// WebThreadSupportingGC wraps a WebThread and adds support for attaching
197242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci// to and detaching from the Blink GC infrastructure. The attachGC method
207242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci// must be called during initialization on the WebThread and before the
217242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci// thread allocates any objects managed by the Blink GC. The detach GC
227242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci// method must be called on the WebThread during shutdown when the thread
237242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci// no longer needs to access objects managed by the Blink GC.
247242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucciclass PLATFORM_EXPORT WebThreadSupportingGC FINAL {
257242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    WTF_MAKE_NONCOPYABLE(WebThreadSupportingGC);
267242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tuccipublic:
277242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    static PassOwnPtr<WebThreadSupportingGC> create(const char*);
287242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    ~WebThreadSupportingGC();
297242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
307242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    void postTask(WebThread::Task* task)
317242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    {
327242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        m_thread->postTask(task);
337242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    }
347242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
357242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    void postDelayedTask(WebThread::Task* task, long long delayMs)
367242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    {
377242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        m_thread->postDelayedTask(task, delayMs);
387242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    }
397242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
407242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    bool isCurrentThread() const
417242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    {
427242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        return m_thread->isCurrentThread();
437242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    }
447242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
457242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    void addTaskObserver(WebThread::TaskObserver* observer)
467242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    {
477242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        m_thread->addTaskObserver(observer);
487242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    }
497242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
507242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    void removeTaskObserver(WebThread::TaskObserver* observer)
517242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    {
527242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        m_thread->removeTaskObserver(observer);
537242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    }
547242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
557242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    void enterRunLoop()
567242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    {
577242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        m_thread->enterRunLoop();
587242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    }
597242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
607242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    void exitRunLoop()
617242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    {
627242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        m_thread->exitRunLoop();
637242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    }
647242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
657242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    void attachGC();
667242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    void detachGC();
677242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
687242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    WebThread& platformThread() const
697242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    {
707242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        ASSERT(m_thread);
717242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        return *m_thread;
727242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    }
737242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
747242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucciprivate:
757242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    explicit WebThreadSupportingGC(const char*);
767242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
777242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    OwnPtr<PendingGCRunner> m_pendingGCRunner;
787242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    OwnPtr<MessageLoopInterruptor> m_messageLoopInterruptor;
797242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
807242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    // FIXME: This has to be last because of crbug.com/401397.
817242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    // A WorkerThread might get deleted before it had a chance to properly
827242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    // shut down. By deleting the WebThread first, we can guarantee that
837242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    // no pending tasks on the thread might want to access any of the other
847242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    // members during the WorkerThread's destruction.
857242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    OwnPtr<WebThread> m_thread;
867242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci};
877242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
887242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci}
897242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
907242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#endif
91