1/* 2 * Copyright (C) 2010, 2012 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 <CoreFoundation/CoreFoundation.h> 30#include <dispatch/dispatch.h> 31#include <wtf/AutodrainedPool.h> 32 33namespace WebCore { 34 35void RunLoop::performWork(void* context) 36{ 37 AutodrainedPool pool; 38 static_cast<RunLoop*>(context)->performWork(); 39} 40 41RunLoop::RunLoop() 42 : m_runLoop(CFRunLoopGetCurrent()) 43 , m_nestingLevel(0) 44{ 45 CFRunLoopSourceContext context = { 0, this, 0, 0, 0, 0, 0, 0, 0, performWork }; 46 m_runLoopSource = adoptCF(CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context)); 47 CFRunLoopAddSource(m_runLoop.get(), m_runLoopSource.get(), kCFRunLoopCommonModes); 48} 49 50RunLoop::~RunLoop() 51{ 52 CFRunLoopSourceInvalidate(m_runLoopSource.get()); 53} 54 55void RunLoop::runForDuration(double duration) 56{ 57 CFRunLoopRunInMode(kCFRunLoopDefaultMode, duration, true); 58} 59 60void RunLoop::wakeUp() 61{ 62 CFRunLoopSourceSignal(m_runLoopSource.get()); 63 CFRunLoopWakeUp(m_runLoop.get()); 64} 65 66#if !PLATFORM(MAC) || PLATFORM(IOS) 67void RunLoop::run() 68{ 69 current()->m_nestingLevel++; 70 71 { 72 AutodrainedPool pool; 73 CFRunLoopRun(); 74 } 75 76 current()->m_nestingLevel--; 77} 78 79void RunLoop::stop() 80{ 81 ASSERT(m_runLoop == CFRunLoopGetCurrent()); 82 CFRunLoopStop(m_runLoop.get()); 83} 84#endif 85 86// RunLoop::Timer 87 88void RunLoop::TimerBase::timerFired(CFRunLoopTimerRef, void* context) 89{ 90 TimerBase* timer = static_cast<TimerBase*>(context); 91 92 AutodrainedPool pool; 93 timer->fired(); 94} 95 96RunLoop::TimerBase::TimerBase(RunLoop* runLoop) 97 : m_runLoop(runLoop) 98{ 99} 100 101RunLoop::TimerBase::~TimerBase() 102{ 103 stop(); 104} 105 106void RunLoop::TimerBase::start(double nextFireInterval, bool repeat) 107{ 108 if (m_timer) 109 stop(); 110 111 CFRunLoopTimerContext context = { 0, this, 0, 0, 0 }; 112 CFTimeInterval repeatInterval = repeat ? nextFireInterval : 0; 113 m_timer = adoptCF(CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + nextFireInterval, repeatInterval, 0, 0, timerFired, &context)); 114 CFRunLoopAddTimer(m_runLoop->m_runLoop.get(), m_timer.get(), kCFRunLoopCommonModes); 115} 116 117void RunLoop::TimerBase::stop() 118{ 119 if (!m_timer) 120 return; 121 122 CFRunLoopTimerInvalidate(m_timer.get()); 123 m_timer = nullptr; 124} 125 126bool RunLoop::TimerBase::isActive() const 127{ 128 return m_timer && CFRunLoopTimerIsValid(m_timer.get()); 129} 130 131} // namespace WebCore 132