2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/html/parser/HTMLParserThread.h"
34 #include "platform/Task.h"
35 #include "platform/TaskSynchronizer.h"
36 #include "public/platform/Platform.h"
37 #include "wtf/PassOwnPtr.h"
41 static HTMLParserThread* s_sharedThread = 0;
43 HTMLParserThread::HTMLParserThread()
47 HTMLParserThread::~HTMLParserThread()
51 void HTMLParserThread::init()
53 ASSERT(!s_sharedThread);
54 s_sharedThread = new HTMLParserThread;
57 void HTMLParserThread::setupHTMLParserThread()
59 m_pendingGCRunner = adoptPtr(new PendingGCRunner);
60 m_messageLoopInterruptor = adoptPtr(new MessageLoopInterruptor(&platformThread()));
61 platformThread().addTaskObserver(m_pendingGCRunner.get());
62 ThreadState::attach();
63 ThreadState::current()->addInterruptor(m_messageLoopInterruptor.get());
66 void HTMLParserThread::shutdown()
68 ASSERT(s_sharedThread);
69 // currentThread will always be non-null in production, but can be null in Chromium unit tests.
70 if (blink::Platform::current()->currentThread() && s_sharedThread->isRunning()) {
71 TaskSynchronizer taskSynchronizer;
72 s_sharedThread->postTask(WTF::bind(&HTMLParserThread::cleanupHTMLParserThread, s_sharedThread, &taskSynchronizer));
73 taskSynchronizer.waitForTaskCompletion();
75 delete s_sharedThread;
79 void HTMLParserThread::cleanupHTMLParserThread(TaskSynchronizer* taskSynchronizer)
81 ThreadState::current()->removeInterruptor(m_messageLoopInterruptor.get());
82 ThreadState::detach();
83 platformThread().removeTaskObserver(m_pendingGCRunner.get());
84 m_pendingGCRunner = nullptr;
85 m_messageLoopInterruptor = nullptr;
86 taskSynchronizer->taskCompleted();
89 HTMLParserThread* HTMLParserThread::shared()
91 return s_sharedThread;
94 blink::WebThread& HTMLParserThread::platformThread()
97 m_thread = adoptPtr(blink::Platform::current()->createThread("HTMLParserThread"));
98 postTask(WTF::bind(&HTMLParserThread::setupHTMLParserThread, this));
103 bool HTMLParserThread::isRunning()
108 void HTMLParserThread::postTask(const Closure& closure)
110 platformThread().postTask(new Task(closure));
113 } // namespace WebCore