]>
Commit | Line | Data |
---|---|---|
9dae56ea A |
1 | /* |
2 | * Copyright (C) 2007, 2008 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 | * | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | |
14 | * its contributors may be used to endorse or promote products derived | |
15 | * from this software without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include "config.h" | |
30 | #include "MainThread.h" | |
31 | ||
ba379fdc A |
32 | #include "CurrentTime.h" |
33 | #include "Deque.h" | |
4e4e5a6f | 34 | #include "StdLibExtras.h" |
9dae56ea | 35 | #include "Threading.h" |
9dae56ea | 36 | |
4e4e5a6f A |
37 | #if PLATFORM(CHROMIUM) |
38 | #error Chromium uses a different main thread implementation | |
39 | #endif | |
40 | ||
9dae56ea A |
41 | namespace WTF { |
42 | ||
43 | struct FunctionWithContext { | |
44 | MainThreadFunction* function; | |
45 | void* context; | |
f9bf01c6 | 46 | ThreadCondition* syncFlag; |
9dae56ea | 47 | |
f9bf01c6 | 48 | FunctionWithContext(MainThreadFunction* function = 0, void* context = 0, ThreadCondition* syncFlag = 0) |
9dae56ea A |
49 | : function(function) |
50 | , context(context) | |
f9bf01c6 | 51 | , syncFlag(syncFlag) |
9dae56ea A |
52 | { |
53 | } | |
4e4e5a6f A |
54 | bool operator == (const FunctionWithContext& o) |
55 | { | |
56 | return function == o.function | |
57 | && context == o.context | |
58 | && syncFlag == o.syncFlag; | |
59 | } | |
9dae56ea A |
60 | }; |
61 | ||
4e4e5a6f A |
62 | class FunctionWithContextFinder { |
63 | public: | |
64 | FunctionWithContextFinder(const FunctionWithContext& m) : m(m) {} | |
65 | bool operator() (FunctionWithContext& o) { return o == m; } | |
66 | FunctionWithContext m; | |
67 | }; | |
68 | ||
69 | ||
ba379fdc | 70 | typedef Deque<FunctionWithContext> FunctionQueue; |
9dae56ea A |
71 | |
72 | static bool callbacksPaused; // This global variable is only accessed from main thread. | |
4e4e5a6f A |
73 | #if !PLATFORM(MAC) && !PLATFORM(QT) |
74 | static ThreadIdentifier mainThreadIdentifier; | |
75 | #endif | |
9dae56ea | 76 | |
4e4e5a6f | 77 | static Mutex& mainThreadFunctionQueueMutex() |
9dae56ea A |
78 | { |
79 | DEFINE_STATIC_LOCAL(Mutex, staticMutex, ()); | |
80 | return staticMutex; | |
81 | } | |
82 | ||
83 | static FunctionQueue& functionQueue() | |
84 | { | |
85 | DEFINE_STATIC_LOCAL(FunctionQueue, staticFunctionQueue, ()); | |
86 | return staticFunctionQueue; | |
87 | } | |
88 | ||
4e4e5a6f A |
89 | |
90 | #if !PLATFORM(MAC) | |
91 | ||
9dae56ea | 92 | void initializeMainThread() |
4e4e5a6f A |
93 | { |
94 | static bool initializedMainThread; | |
95 | if (initializedMainThread) | |
96 | return; | |
97 | initializedMainThread = true; | |
98 | ||
99 | #if !PLATFORM(QT) | |
100 | mainThreadIdentifier = currentThread(); | |
101 | #endif | |
102 | ||
103 | mainThreadFunctionQueueMutex(); | |
104 | initializeMainThreadPlatform(); | |
105 | } | |
106 | ||
107 | #else | |
108 | ||
109 | static pthread_once_t initializeMainThreadKeyOnce = PTHREAD_ONCE_INIT; | |
110 | ||
111 | static void initializeMainThreadOnce() | |
9dae56ea A |
112 | { |
113 | mainThreadFunctionQueueMutex(); | |
ba379fdc | 114 | initializeMainThreadPlatform(); |
9dae56ea | 115 | } |
ba379fdc | 116 | |
4e4e5a6f A |
117 | void initializeMainThread() |
118 | { | |
119 | pthread_once(&initializeMainThreadKeyOnce, initializeMainThreadOnce); | |
120 | } | |
121 | ||
122 | static void initializeMainThreadToProcessMainThreadOnce() | |
123 | { | |
124 | mainThreadFunctionQueueMutex(); | |
125 | initializeMainThreadToProcessMainThreadPlatform(); | |
126 | } | |
127 | ||
128 | void initializeMainThreadToProcessMainThread() | |
129 | { | |
130 | pthread_once(&initializeMainThreadKeyOnce, initializeMainThreadToProcessMainThreadOnce); | |
131 | } | |
132 | #endif | |
133 | ||
ba379fdc A |
134 | // 0.1 sec delays in UI is approximate threshold when they become noticeable. Have a limit that's half of that. |
135 | static const double maxRunLoopSuspensionTime = 0.05; | |
9dae56ea A |
136 | |
137 | void dispatchFunctionsFromMainThread() | |
138 | { | |
139 | ASSERT(isMainThread()); | |
140 | ||
141 | if (callbacksPaused) | |
142 | return; | |
143 | ||
ba379fdc A |
144 | double startTime = currentTime(); |
145 | ||
146 | FunctionWithContext invocation; | |
147 | while (true) { | |
148 | { | |
149 | MutexLocker locker(mainThreadFunctionQueueMutex()); | |
150 | if (!functionQueue().size()) | |
151 | break; | |
152 | invocation = functionQueue().first(); | |
153 | functionQueue().removeFirst(); | |
154 | } | |
9dae56ea | 155 | |
9dae56ea | 156 | invocation.function(invocation.context); |
f9bf01c6 A |
157 | if (invocation.syncFlag) |
158 | invocation.syncFlag->signal(); | |
ba379fdc A |
159 | |
160 | // If we are running accumulated functions for too long so UI may become unresponsive, we need to | |
161 | // yield so the user input can be processed. Otherwise user may not be able to even close the window. | |
162 | // This code has effect only in case the scheduleDispatchFunctionsOnMainThread() is implemented in a way that | |
163 | // allows input events to be processed before we are back here. | |
164 | if (currentTime() - startTime > maxRunLoopSuspensionTime) { | |
165 | scheduleDispatchFunctionsOnMainThread(); | |
166 | break; | |
167 | } | |
9dae56ea A |
168 | } |
169 | } | |
170 | ||
171 | void callOnMainThread(MainThreadFunction* function, void* context) | |
172 | { | |
173 | ASSERT(function); | |
ba379fdc | 174 | bool needToSchedule = false; |
9dae56ea A |
175 | { |
176 | MutexLocker locker(mainThreadFunctionQueueMutex()); | |
ba379fdc | 177 | needToSchedule = functionQueue().size() == 0; |
9dae56ea A |
178 | functionQueue().append(FunctionWithContext(function, context)); |
179 | } | |
ba379fdc A |
180 | if (needToSchedule) |
181 | scheduleDispatchFunctionsOnMainThread(); | |
9dae56ea A |
182 | } |
183 | ||
f9bf01c6 A |
184 | void callOnMainThreadAndWait(MainThreadFunction* function, void* context) |
185 | { | |
186 | ASSERT(function); | |
187 | ||
188 | if (isMainThread()) { | |
189 | function(context); | |
190 | return; | |
191 | } | |
192 | ||
193 | ThreadCondition syncFlag; | |
194 | Mutex& functionQueueMutex = mainThreadFunctionQueueMutex(); | |
195 | MutexLocker locker(functionQueueMutex); | |
196 | functionQueue().append(FunctionWithContext(function, context, &syncFlag)); | |
197 | if (functionQueue().size() == 1) | |
198 | scheduleDispatchFunctionsOnMainThread(); | |
199 | syncFlag.wait(functionQueueMutex); | |
200 | } | |
201 | ||
4e4e5a6f A |
202 | void cancelCallOnMainThread(MainThreadFunction* function, void* context) |
203 | { | |
204 | ASSERT(function); | |
205 | ||
206 | MutexLocker locker(mainThreadFunctionQueueMutex()); | |
207 | ||
208 | FunctionWithContextFinder pred(FunctionWithContext(function, context)); | |
209 | ||
210 | while (true) { | |
211 | // We must redefine 'i' each pass, because the itererator's operator= | |
212 | // requires 'this' to be valid, and remove() invalidates all iterators | |
213 | FunctionQueue::iterator i(functionQueue().findIf(pred)); | |
214 | if (i == functionQueue().end()) | |
215 | break; | |
216 | functionQueue().remove(i); | |
217 | } | |
218 | } | |
219 | ||
9dae56ea A |
220 | void setMainThreadCallbacksPaused(bool paused) |
221 | { | |
222 | ASSERT(isMainThread()); | |
223 | ||
224 | if (callbacksPaused == paused) | |
225 | return; | |
226 | ||
227 | callbacksPaused = paused; | |
228 | ||
229 | if (!callbacksPaused) | |
230 | scheduleDispatchFunctionsOnMainThread(); | |
231 | } | |
232 | ||
4e4e5a6f A |
233 | #if !PLATFORM(MAC) && !PLATFORM(QT) |
234 | bool isMainThread() | |
235 | { | |
236 | return currentThread() == mainThreadIdentifier; | |
237 | } | |
238 | #endif | |
239 | ||
9dae56ea | 240 | } // namespace WTF |