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#ifndef ScrollingThread_h
27#define ScrollingThread_h
28
29#if ENABLE(ASYNC_SCROLLING)
30
31#include <condition_variable>
32#include <functional>
33#include <wtf/Forward.h>
34#include <wtf/Noncopyable.h>
35#include <wtf/Threading.h>
36#include <wtf/Vector.h>
37
38#if PLATFORM(COCOA)
39#include <wtf/RetainPtr.h>
40#endif
41
42namespace WebCore {
43
44class ScrollingThread {
45    WTF_MAKE_NONCOPYABLE(ScrollingThread);
46
47public:
48    static bool isCurrentThread();
49    static void dispatch(std::function<void ()>);
50
51    // Will dispatch the given function on the main thread once all pending functions
52    // on the scrolling thread have finished executing. Used for synchronization purposes.
53    static void dispatchBarrier(std::function<void ()>);
54
55private:
56    friend NeverDestroyed<ScrollingThread>;
57
58    ScrollingThread();
59
60    static ScrollingThread& shared();
61
62    void createThreadIfNeeded();
63    static void threadCallback(void* scrollingThread);
64    void threadBody();
65    void dispatchFunctionsFromScrollingThread();
66
67    void initializeRunLoop();
68    void wakeUpRunLoop();
69
70#if PLATFORM(COCOA)
71    static void threadRunLoopSourceCallback(void* scrollingThread);
72    void threadRunLoopSourceCallback();
73#endif
74
75    ThreadIdentifier m_threadIdentifier;
76
77    std::condition_variable m_initializeRunLoopConditionVariable;
78    std::mutex m_initializeRunLoopMutex;
79
80    std::mutex m_functionsMutex;
81    Vector<std::function<void ()>> m_functions;
82
83#if PLATFORM(COCOA)
84    // FIXME: We should use WebCore::RunLoop here.
85    RetainPtr<CFRunLoopRef> m_threadRunLoop;
86    RetainPtr<CFRunLoopSourceRef> m_threadRunLoopSource;
87#endif
88};
89
90} // namespace WebCore
91
92#endif // ENABLE(ASYNC_SCROLLING)
93
94#endif // ScrollingThread_h
95