OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "platform/WebThreadSupportingGC.h" | 6 #include "platform/WebThreadSupportingGC.h" |
7 | 7 |
8 #include "platform/heap/SafePoint.h" | 8 #include "platform/heap/SafePoint.h" |
9 #include "public/platform/WebScheduler.h" | 9 #include "public/platform/WebScheduler.h" |
10 #include "wtf/Threading.h" | 10 #include "wtf/Threading.h" |
11 | 11 |
12 namespace blink { | 12 namespace blink { |
13 | 13 |
14 PassOwnPtr<WebThreadSupportingGC> WebThreadSupportingGC::create(const char* name
) | 14 PassOwnPtr<WebThreadSupportingGC> WebThreadSupportingGC::create(const char* name
) |
15 { | 15 { |
| 16 return adoptPtr(new WebThreadSupportingGC(name, nullptr)); |
| 17 } |
| 18 |
| 19 PassOwnPtr<WebThreadSupportingGC> WebThreadSupportingGC::createForThread(WebThre
ad* thread) |
| 20 { |
| 21 return adoptPtr(new WebThreadSupportingGC(nullptr, thread)); |
| 22 } |
| 23 |
| 24 WebThreadSupportingGC::WebThreadSupportingGC(const char* name, WebThread* thread
) |
| 25 : m_thread(thread) |
| 26 { |
16 #if ENABLE(ASSERT) | 27 #if ENABLE(ASSERT) |
| 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. |
17 WTF::willCreateThread(); | 31 WTF::willCreateThread(); |
18 #endif | 32 #endif |
19 return adoptPtr(new WebThreadSupportingGC(name)); | 33 if (!m_thread) { |
20 } | 34 // If |thread| is not given, create a new one and own it. |
21 | 35 m_owningThread = adoptPtr(Platform::current()->createThread(name)); |
22 WebThreadSupportingGC::WebThreadSupportingGC(const char* name) | 36 m_thread = m_owningThread.get(); |
23 : m_thread(adoptPtr(Platform::current()->createThread(name))) | 37 } |
24 { | |
25 } | 38 } |
26 | 39 |
27 WebThreadSupportingGC::~WebThreadSupportingGC() | 40 WebThreadSupportingGC::~WebThreadSupportingGC() |
28 { | 41 { |
29 if (ThreadState::current()) { | 42 if (ThreadState::current() && m_owningThread) { |
30 // WebThread's destructor blocks until all the tasks are processed. | 43 // WebThread's destructor blocks until all the tasks are processed. |
31 SafePointScope scope(ThreadState::HeapPointersOnStack); | 44 SafePointScope scope(ThreadState::HeapPointersOnStack); |
32 m_thread.clear(); | 45 m_owningThread.clear(); |
33 } | 46 } |
34 } | 47 } |
35 | 48 |
36 void WebThreadSupportingGC::initialize() | 49 void WebThreadSupportingGC::initialize() |
37 { | 50 { |
38 m_pendingGCRunner = adoptPtr(new PendingGCRunner); | 51 m_pendingGCRunner = adoptPtr(new PendingGCRunner); |
39 platformThread().addTaskObserver(m_pendingGCRunner.get()); | 52 m_thread->addTaskObserver(m_pendingGCRunner.get()); |
40 ThreadState::attach(); | 53 ThreadState::attach(); |
41 OwnPtr<MessageLoopInterruptor> interruptor = adoptPtr(new MessageLoopInterru
ptor(&platformThread())); | 54 OwnPtr<MessageLoopInterruptor> interruptor = adoptPtr(new MessageLoopInterru
ptor(m_thread)); |
42 ThreadState::current()->addInterruptor(interruptor.release()); | 55 ThreadState::current()->addInterruptor(interruptor.release()); |
43 } | 56 } |
44 | 57 |
45 void WebThreadSupportingGC::shutdown() | 58 void WebThreadSupportingGC::shutdown() |
46 { | 59 { |
47 // Ensure no posted tasks will run from this point on. | 60 // Ensure no posted tasks will run from this point on. |
48 platformThread().removeTaskObserver(m_pendingGCRunner.get()); | 61 m_thread->removeTaskObserver(m_pendingGCRunner.get()); |
49 platformThread().scheduler()->shutdown(); | 62 |
| 63 // Shutdown the thread (via its scheduler) only when the thread is created |
| 64 // and is owned by this instance. |
| 65 if (m_owningThread) |
| 66 m_owningThread->scheduler()->shutdown(); |
50 | 67 |
51 ThreadState::detach(); | 68 ThreadState::detach(); |
52 m_pendingGCRunner = nullptr; | 69 m_pendingGCRunner = nullptr; |
53 } | 70 } |
54 | 71 |
55 } // namespace blink | 72 } // namespace blink |
OLD | NEW |