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