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