source: webkit/trunk/JavaScriptCore/wtf/MainThread.cpp@ 44897

Last change on this file since 44897 was 40969, checked in by [email protected], 16 years ago

2009-02-13 David Levin <[email protected]>

Reviewed by Darin Adler.

Bug 23926: Race condition in callOnMainThreadAndWait
<https://bugs.webkit.org/show_bug.cgi?id=23926>

  • wtf/MainThread.cpp: Removed callOnMainThreadAndWait since it isn't used.
  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
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
32#include "CurrentTime.h"
33#include "Deque.h"
34#include "StdLibExtras.h"
35#include "Threading.h"
36
37namespace WTF {
38
39struct FunctionWithContext {
40 MainThreadFunction* function;
41 void* context;
42
43 FunctionWithContext(MainThreadFunction* function = 0, void* context = 0)
44 : function(function)
45 , context(context)
46 {
47 }
48};
49
50typedef Deque<FunctionWithContext> FunctionQueue;
51
52static bool callbacksPaused; // This global variable is only accessed from main thread.
53
54Mutex& mainThreadFunctionQueueMutex()
55{
56 DEFINE_STATIC_LOCAL(Mutex, staticMutex, ());
57 return staticMutex;
58}
59
60static FunctionQueue& functionQueue()
61{
62 DEFINE_STATIC_LOCAL(FunctionQueue, staticFunctionQueue, ());
63 return staticFunctionQueue;
64}
65
66void initializeMainThread()
67{
68 mainThreadFunctionQueueMutex();
69 initializeMainThreadPlatform();
70}
71
72// 0.1 sec delays in UI is approximate threshold when they become noticeable. Have a limit that's half of that.
73static const double maxRunLoopSuspensionTime = 0.05;
74
75void dispatchFunctionsFromMainThread()
76{
77 ASSERT(isMainThread());
78
79 if (callbacksPaused)
80 return;
81
82 double startTime = currentTime();
83
84 FunctionWithContext invocation;
85 while (true) {
86 {
87 MutexLocker locker(mainThreadFunctionQueueMutex());
88 if (!functionQueue().size())
89 break;
90 invocation = functionQueue().first();
91 functionQueue().removeFirst();
92 }
93
94 invocation.function(invocation.context);
95
96 // If we are running accumulated functions for too long so UI may become unresponsive, we need to
97 // yield so the user input can be processed. Otherwise user may not be able to even close the window.
98 // This code has effect only in case the scheduleDispatchFunctionsOnMainThread() is implemented in a way that
99 // allows input events to be processed before we are back here.
100 if (currentTime() - startTime > maxRunLoopSuspensionTime) {
101 scheduleDispatchFunctionsOnMainThread();
102 break;
103 }
104 }
105}
106
107void callOnMainThread(MainThreadFunction* function, void* context)
108{
109 ASSERT(function);
110 bool needToSchedule = false;
111 {
112 MutexLocker locker(mainThreadFunctionQueueMutex());
113 needToSchedule = functionQueue().size() == 0;
114 functionQueue().append(FunctionWithContext(function, context));
115 }
116 if (needToSchedule)
117 scheduleDispatchFunctionsOnMainThread();
118}
119
120void setMainThreadCallbacksPaused(bool paused)
121{
122 ASSERT(isMainThread());
123
124 if (callbacksPaused == paused)
125 return;
126
127 callbacksPaused = paused;
128
129 if (!callbacksPaused)
130 scheduleDispatchFunctionsOnMainThread();
131}
132
133} // namespace WTF
Note: See TracBrowser for help on using the repository browser.