Changeset 40888 in webkit for trunk/JavaScriptCore/wtf/MainThread.cpp
- Timestamp:
- Feb 11, 2009, 11:58:36 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/MainThread.cpp
r40684 r40888 30 30 #include "MainThread.h" 31 31 32 #include "CurrentTime.h" 33 #include "Deque.h" 32 34 #include "StdLibExtras.h" 33 35 #include "Threading.h" 34 #include "Vector.h"35 36 36 37 namespace WTF { … … 49 50 }; 50 51 51 typedef Vector<FunctionWithContext> FunctionQueue;52 typedef Deque<FunctionWithContext> FunctionQueue; 52 53 53 54 static bool callbacksPaused; // This global variable is only accessed from main thread. … … 65 66 } 66 67 67 #if !PLATFORM(WIN) && !PLATFORM(CHROMIUM)68 68 void initializeMainThread() 69 69 { 70 70 mainThreadFunctionQueueMutex(); 71 initializeMainThreadPlatform(); 71 72 } 72 #endif 73 74 // 0.1 sec delays in UI is approximate threshold when they become noticeable. Have a limit that's half of that. 75 static const double maxRunLoopSuspensionTime = 0.05; 73 76 74 77 void dispatchFunctionsFromMainThread() … … 79 82 return; 80 83 81 FunctionQueue queueCopy; 82 { 83 MutexLocker locker(mainThreadFunctionQueueMutex()); 84 queueCopy.swap(functionQueue()); 85 } 84 double startTime = currentTime(); 86 85 87 for (unsigned i = 0; i < queueCopy.size(); ++i) { 88 FunctionWithContext& invocation = queueCopy[i]; 86 FunctionWithContext invocation; 87 while (true) { 88 { 89 MutexLocker locker(mainThreadFunctionQueueMutex()); 90 if (!functionQueue().size()) 91 break; 92 invocation = functionQueue().first(); 93 functionQueue().removeFirst(); 94 } 95 89 96 invocation.function(invocation.context); 90 97 if (invocation.syncFlag) 91 98 invocation.syncFlag->signal(); 99 100 // If we are running accumulated functions for too long so UI may become unresponsive, we need to 101 // yield so the user input can be processed. Otherwise user may not be able to even close the window. 102 // This code has effect only in case the scheduleDispatchFunctionsOnMainThread() is implemented in a way that 103 // allows input events to be processed before we are back here. 104 if (currentTime() - startTime > maxRunLoopSuspensionTime) { 105 scheduleDispatchFunctionsOnMainThread(); 106 break; 107 } 92 108 } 93 109 } … … 96 112 { 97 113 ASSERT(function); 98 114 bool needToSchedule = false; 99 115 { 100 116 MutexLocker locker(mainThreadFunctionQueueMutex()); 117 needToSchedule = functionQueue().size() == 0; 101 118 functionQueue().append(FunctionWithContext(function, context)); 102 119 } 103 104 scheduleDispatchFunctionsOnMainThread();120 if (needToSchedule) 121 scheduleDispatchFunctionsOnMainThread(); 105 122 } 106 123 … … 116 133 ThreadCondition syncFlag; 117 134 Mutex conditionMutex; 118 135 bool needToSchedule = false; 119 136 { 120 137 MutexLocker locker(mainThreadFunctionQueueMutex()); 138 needToSchedule = functionQueue().size() == 0; 121 139 functionQueue().append(FunctionWithContext(function, context, &syncFlag)); 122 140 conditionMutex.lock(); 123 141 } 124 142 125 scheduleDispatchFunctionsOnMainThread(); 143 if (needToSchedule) 144 scheduleDispatchFunctionsOnMainThread(); 126 145 syncFlag.wait(conditionMutex); 127 146 }
Note:
See TracChangeset
for help on using the changeset viewer.