1/*
2 * Copyright (C) 2010 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 "RunLoop.h"
28
29#include <wtf/StdLibExtras.h>
30#include <wtf/ThreadSpecific.h>
31
32namespace WTF {
33
34static RunLoop* s_mainRunLoop;
35
36// Helper class for ThreadSpecificData.
37class RunLoop::Holder {
38public:
39    Holder()
40        : m_runLoop(adoptRef(*new RunLoop))
41    {
42    }
43
44    RunLoop& runLoop() { return m_runLoop.get(); }
45
46private:
47    Ref<RunLoop> m_runLoop;
48};
49
50void RunLoop::initializeMainRunLoop()
51{
52    if (s_mainRunLoop)
53        return;
54    s_mainRunLoop = &RunLoop::current();
55}
56
57RunLoop& RunLoop::current()
58{
59    DEPRECATED_DEFINE_STATIC_LOCAL(WTF::ThreadSpecific<RunLoop::Holder>, runLoopHolder, ());
60    return runLoopHolder->runLoop();
61}
62
63RunLoop& RunLoop::main()
64{
65    ASSERT(s_mainRunLoop);
66    return *s_mainRunLoop;
67}
68
69bool RunLoop::isMain()
70{
71    ASSERT(s_mainRunLoop);
72    return s_mainRunLoop == &RunLoop::current();
73}
74
75void RunLoop::performWork()
76{
77    // It is important to handle the functions in the queue one at a time because while inside one of these
78    // functions we might re-enter RunLoop::performWork() and we need to be able to pick up where we left off.
79    // See http://webkit.org/b/89590 for more discussion.
80
81    // One possible scenario when handling the function queue is as follows:
82    // - RunLoop::performWork() is invoked with 1 function on the queue
83    // - Handling that function results in 1 more function being enqueued
84    // - Handling that one results in yet another being enqueued
85    // - And so on
86    //
87    // In this situation one invocation of performWork() never returns so all other event sources are blocked.
88    // By only handling up to the number of functions that were in the queue when performWork() is called
89    // we guarantee to occasionally return from the run loop so other event sources will be allowed to spin.
90
91    std::function<void()> function;
92    size_t functionsToHandle = 0;
93
94    {
95        MutexLocker locker(m_functionQueueLock);
96        functionsToHandle = m_functionQueue.size();
97
98        if (m_functionQueue.isEmpty())
99            return;
100
101        function = m_functionQueue.takeFirst();
102    }
103
104    function();
105
106    for (size_t functionsHandled = 1; functionsHandled < functionsToHandle; ++functionsHandled) {
107        {
108            MutexLocker locker(m_functionQueueLock);
109
110            // Even if we start off with N functions to handle and we've only handled less than N functions, the queue
111            // still might be empty because those functions might have been handled in an inner RunLoop::performWork().
112            // In that case we should bail here.
113            if (m_functionQueue.isEmpty())
114                break;
115
116            function = m_functionQueue.takeFirst();
117        }
118
119        function();
120    }
121}
122
123void RunLoop::dispatch(std::function<void ()> function)
124{
125    MutexLocker locker(m_functionQueueLock);
126    m_functionQueue.append(WTF::move(function));
127
128    wakeUp();
129}
130
131} // namespace WTF
132