2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Portions Copyright (c) 2010 Motorola Mobility, Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
36 m_runLoopContext = g_main_context_default();
37 ASSERT(m_runLoopContext);
38 m_runLoopMain = g_main_loop_new(m_runLoopContext, FALSE);
39 ASSERT(m_runLoopMain);
45 if (g_main_loop_is_running(m_runLoopMain))
46 g_main_loop_quit(m_runLoopMain);
47 g_main_loop_unref(m_runLoopMain);
51 g_main_context_unref(m_runLoopContext);
56 g_main_loop_run(RunLoop::main()->mainLoop());
59 GMainLoop* RunLoop::mainLoop()
66 g_main_loop_quit(m_runLoopMain);
69 gboolean RunLoop::queueWork(RunLoop* runLoop)
71 runLoop->performWork();
75 void RunLoop::wakeUp()
77 GRefPtr<GSource> source = adoptGRef(g_idle_source_new());
78 g_source_set_priority(source.get(), G_PRIORITY_DEFAULT);
79 g_source_set_callback(source.get(), reinterpret_cast<GSourceFunc>(&RunLoop::queueWork), this, 0);
80 g_source_attach(source.get(), m_runLoopContext);
82 g_main_context_wakeup(m_runLoopContext);
85 RunLoop::TimerBase::TimerBase(RunLoop* runLoop)
91 RunLoop::TimerBase::~TimerBase()
96 void RunLoop::TimerBase::clearTimerSource()
101 gboolean RunLoop::TimerBase::timerFiredCallback(RunLoop::TimerBase* timer)
103 GSource* currentTimerSource = timer->m_timerSource.get();
104 bool isRepeating = timer->isRepeating();
105 // This can change the timerSource by starting a new timer within the callback.
107 if (!isRepeating && currentTimerSource == timer->m_timerSource.get())
108 timer->clearTimerSource();
112 void RunLoop::TimerBase::start(double fireInterval, bool repeat)
117 m_timerSource = adoptGRef(g_timeout_source_new(static_cast<guint>(fireInterval * 1000)));
118 m_isRepeating = repeat;
119 g_source_set_callback(m_timerSource.get(), reinterpret_cast<GSourceFunc>(&RunLoop::TimerBase::timerFiredCallback), this, 0);
120 g_source_attach(m_timerSource.get(), m_runLoop->m_runLoopContext);
123 void RunLoop::TimerBase::stop()
128 g_source_destroy(m_timerSource.get());
132 bool RunLoop::TimerBase::isActive() const
134 return m_timerSource;