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