1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "config.h"
6#include "platform/WebThreadSupportingGC.h"
7
8namespace blink {
9
10PassOwnPtr<WebThreadSupportingGC> WebThreadSupportingGC::create(const char* name)
11{
12    return adoptPtr(new WebThreadSupportingGC(name));
13}
14
15WebThreadSupportingGC::WebThreadSupportingGC(const char* name)
16    : m_thread(adoptPtr(blink::Platform::current()->createThread(name)))
17{
18}
19
20WebThreadSupportingGC::~WebThreadSupportingGC()
21{
22    if (ThreadState::current()) {
23        // WebThread's destructor blocks until all the tasks are processed.
24        ThreadState::SafePointScope scope(ThreadState::HeapPointersOnStack);
25        m_thread.clear();
26    }
27}
28
29void WebThreadSupportingGC::attachGC()
30{
31    m_pendingGCRunner = adoptPtr(new PendingGCRunner);
32    m_messageLoopInterruptor = adoptPtr(new MessageLoopInterruptor(&platformThread()));
33    platformThread().addTaskObserver(m_pendingGCRunner.get());
34    ThreadState::attach();
35    ThreadState::current()->addInterruptor(m_messageLoopInterruptor.get());
36}
37
38void WebThreadSupportingGC::detachGC()
39{
40    ThreadState::current()->removeInterruptor(m_messageLoopInterruptor.get());
41    ThreadState::detach();
42    platformThread().removeTaskObserver(m_pendingGCRunner.get());
43    m_pendingGCRunner = nullptr;
44    m_messageLoopInterruptor = nullptr;
45}
46
47} // namespace blink
48