1/*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "ScrollingThread.h"
28
29#if ENABLE(THREADED_SCROLLING)
30
31#include <wtf/MainThread.h>
32
33namespace WebCore {
34
35ScrollingThread::ScrollingThread()
36    : m_threadIdentifier(0)
37{
38}
39
40bool ScrollingThread::isCurrentThread()
41{
42    if (!shared().m_threadIdentifier)
43        return false;
44
45    return currentThread() == shared().m_threadIdentifier;
46}
47
48void ScrollingThread::dispatch(const Function<void()>& function)
49{
50    shared().createThreadIfNeeded();
51
52    {
53        MutexLocker locker(shared().m_functionsMutex);
54        shared().m_functions.append(function);
55    }
56
57    shared().wakeUpRunLoop();
58}
59
60static void callFunctionOnMainThread(const Function<void()>* function)
61{
62    callOnMainThread(*function);
63    delete function;
64}
65
66void ScrollingThread::dispatchBarrier(const Function<void()>& function)
67{
68    dispatch(bind(callFunctionOnMainThread, new Function<void()>(function)));
69}
70
71ScrollingThread& ScrollingThread::shared()
72{
73    DEFINE_STATIC_LOCAL(ScrollingThread, scrollingThread, ());
74    return scrollingThread;
75}
76
77void ScrollingThread::createThreadIfNeeded()
78{
79    if (m_threadIdentifier)
80        return;
81
82    // Wait for the thread to initialize the run loop.
83    {
84        MutexLocker locker(m_initializeRunLoopConditionMutex);
85
86        m_threadIdentifier = createThread(threadCallback, this, "WebCore: Scrolling");
87
88#if PLATFORM(MAC)
89        while (!m_threadRunLoop)
90            m_initializeRunLoopCondition.wait(m_initializeRunLoopConditionMutex);
91#endif
92    }
93}
94
95void ScrollingThread::threadCallback(void* scrollingThread)
96{
97    static_cast<ScrollingThread*>(scrollingThread)->threadBody();
98}
99
100void ScrollingThread::threadBody()
101{
102    initializeRunLoop();
103}
104
105void ScrollingThread::dispatchFunctionsFromScrollingThread()
106{
107    ASSERT(isCurrentThread());
108
109    Vector<Function<void()> > functions;
110
111    {
112        MutexLocker locker(m_functionsMutex);
113        m_functions.swap(functions);
114    }
115
116    for (size_t i = 0; i < functions.size(); ++i)
117        functions[i]();
118}
119
120} // namespace WebCore
121
122#endif // ENABLE(THREADED_SCROLLING)
123