| OLD | NEW |
| (Empty) |
| 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 "RunLoop.h" | |
| 28 | |
| 29 #include <wtf/StdLibExtras.h> | |
| 30 | |
| 31 namespace WebCore { | |
| 32 | |
| 33 #if !PLATFORM(MAC) | |
| 34 | |
| 35 static RunLoop* s_mainRunLoop; | |
| 36 | |
| 37 void RunLoop::initializeMainRunLoop() | |
| 38 { | |
| 39 if (s_mainRunLoop) | |
| 40 return; | |
| 41 s_mainRunLoop = RunLoop::current(); | |
| 42 } | |
| 43 | |
| 44 RunLoop* RunLoop::current() | |
| 45 { | |
| 46 DEFINE_STATIC_LOCAL(WTF::ThreadSpecific<RunLoop>, runLoopData, ()); | |
| 47 return &*runLoopData; | |
| 48 } | |
| 49 | |
| 50 RunLoop* RunLoop::main() | |
| 51 { | |
| 52 ASSERT(s_mainRunLoop); | |
| 53 return s_mainRunLoop; | |
| 54 } | |
| 55 | |
| 56 void RunLoop::setUseApplicationRunLoopOnMainRunLoop() | |
| 57 { | |
| 58 } | |
| 59 | |
| 60 #endif | |
| 61 | |
| 62 void RunLoop::performWork() | |
| 63 { | |
| 64 // It is important to handle the functions in the queue one at a time becaus
e while inside one of these | |
| 65 // functions we might re-enter RunLoop::performWork() and we need to be able
to pick up where we left off. | |
| 66 // See http://webkit.org/b/89590 for more discussion. | |
| 67 | |
| 68 // One possible scenario when handling the function queue is as follows: | |
| 69 // - RunLoop::performWork() is invoked with 1 function on the queue | |
| 70 // - Handling that function results in 1 more function being enqueued | |
| 71 // - Handling that one results in yet another being enqueued | |
| 72 // - And so on | |
| 73 // | |
| 74 // In this situation one invocation of performWork() never returns so all ot
her event sources are blocked. | |
| 75 // By only handling up to the number of functions that were in the queue whe
n performWork() is called | |
| 76 // we guarantee to occasionally return from the run loop so other event sour
ces will be allowed to spin. | |
| 77 | |
| 78 Function<void()> function; | |
| 79 size_t functionsToHandle = 0; | |
| 80 | |
| 81 { | |
| 82 MutexLocker locker(m_functionQueueLock); | |
| 83 functionsToHandle = m_functionQueue.size(); | |
| 84 | |
| 85 if (m_functionQueue.isEmpty()) | |
| 86 return; | |
| 87 | |
| 88 function = m_functionQueue.takeFirst(); | |
| 89 } | |
| 90 | |
| 91 function(); | |
| 92 | |
| 93 for (size_t functionsHandled = 1; functionsHandled < functionsToHandle; ++fu
nctionsHandled) { | |
| 94 { | |
| 95 MutexLocker locker(m_functionQueueLock); | |
| 96 | |
| 97 // Even if we start off with N functions to handle and we've only ha
ndled less than N functions, the queue | |
| 98 // still might be empty because those functions might have been hand
led in an inner RunLoop::performWork(). | |
| 99 // In that case we should bail here. | |
| 100 if (m_functionQueue.isEmpty()) | |
| 101 break; | |
| 102 | |
| 103 function = m_functionQueue.takeFirst(); | |
| 104 } | |
| 105 | |
| 106 function(); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 void RunLoop::dispatch(const Function<void()>& function) | |
| 111 { | |
| 112 MutexLocker locker(m_functionQueueLock); | |
| 113 m_functionQueue.append(function); | |
| 114 | |
| 115 wakeUp(); | |
| 116 } | |
| 117 | |
| 118 } // namespace WebCore | |
| OLD | NEW |