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.
6 #include "platform/WebThreadSupportingGC.h"
8 #include "platform/heap/SafePoint.h"
9 #include "public/platform/WebScheduler.h"
10 #include "wtf/Threading.h"
14 PassOwnPtr
<WebThreadSupportingGC
> WebThreadSupportingGC::create(const char* name
)
16 return adoptPtr(new WebThreadSupportingGC(name
, nullptr));
19 PassOwnPtr
<WebThreadSupportingGC
> WebThreadSupportingGC::createForThread(WebThread
* thread
)
21 return adoptPtr(new WebThreadSupportingGC(nullptr, thread
));
24 WebThreadSupportingGC::WebThreadSupportingGC(const char* name
, WebThread
* thread
)
28 ASSERT(!name
|| !thread
);
29 // We call this regardless of whether an existing thread is given or not,
30 // as it means that blink is going to run with more than one thread.
31 WTF::willCreateThread();
34 // If |thread| is not given, create a new one and own it.
35 m_owningThread
= adoptPtr(Platform::current()->createThread(name
));
36 m_thread
= m_owningThread
.get();
40 WebThreadSupportingGC::~WebThreadSupportingGC()
42 if (ThreadState::current() && m_owningThread
) {
43 // WebThread's destructor blocks until all the tasks are processed.
44 SafePointScope
scope(ThreadState::HeapPointersOnStack
);
45 m_owningThread
.clear();
49 void WebThreadSupportingGC::initialize()
51 m_pendingGCRunner
= adoptPtr(new PendingGCRunner
);
52 m_thread
->addTaskObserver(m_pendingGCRunner
.get());
53 ThreadState::attach();
54 OwnPtr
<MessageLoopInterruptor
> interruptor
= adoptPtr(new MessageLoopInterruptor(m_thread
->taskRunner()));
55 ThreadState::current()->addInterruptor(interruptor
.release());
58 void WebThreadSupportingGC::shutdown()
60 // Ensure no posted tasks will run from this point on.
61 m_thread
->removeTaskObserver(m_pendingGCRunner
.get());
63 // Shutdown the thread (via its scheduler) only when the thread is created
64 // and is owned by this instance.
66 m_owningThread
->scheduler()->shutdown();
68 ThreadState::detach();
69 m_pendingGCRunner
= nullptr;