1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/*
* Copyright (C) 2010 Apple Inc. All rights reserved.
* Portions Copyright (c) 2010 Motorola Mobility, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "RunLoop.h"
#include <glib.h>
#include <wtf/MainThread.h>
namespace WTF {
static GSourceFuncs runLoopSourceFunctions = {
nullptr, // prepare
nullptr, // check
// dispatch
[](GSource* source, GSourceFunc callback, gpointer userData) -> gboolean
{
if (g_source_get_ready_time(source) == -1)
return G_SOURCE_CONTINUE;
g_source_set_ready_time(source, -1);
return callback(userData);
},
nullptr, // finalize
nullptr, // closure_callback
nullptr, // closure_marshall
};
RunLoop::RunLoop()
{
m_mainContext = g_main_context_get_thread_default();
if (!m_mainContext)
m_mainContext = isMainThread() ? g_main_context_default() : adoptGRef(g_main_context_new());
ASSERT(m_mainContext);
GRefPtr<GMainLoop> innermostLoop = adoptGRef(g_main_loop_new(m_mainContext.get(), FALSE));
ASSERT(innermostLoop);
m_mainLoops.append(innermostLoop);
m_source = adoptGRef(g_source_new(&runLoopSourceFunctions, sizeof(GSource)));
g_source_set_name(m_source.get(), "[WebKit] RunLoop work");
g_source_set_can_recurse(m_source.get(), TRUE);
g_source_set_callback(m_source.get(), [](gpointer userData) -> gboolean {
static_cast<RunLoop*>(userData)->performWork();
return G_SOURCE_CONTINUE;
}, this, nullptr);
g_source_attach(m_source.get(), m_mainContext.get());
}
RunLoop::~RunLoop()
{
g_source_destroy(m_source.get());
for (int i = m_mainLoops.size() - 1; i >= 0; --i) {
if (!g_main_loop_is_running(m_mainLoops[i].get()))
continue;
g_main_loop_quit(m_mainLoops[i].get());
}
}
void RunLoop::run()
{
RunLoop& runLoop = RunLoop::current();
GMainContext* mainContext = runLoop.m_mainContext.get();
// The innermost main loop should always be there.
ASSERT(!runLoop.m_mainLoops.isEmpty());
GMainLoop* innermostLoop = runLoop.m_mainLoops[0].get();
if (!g_main_loop_is_running(innermostLoop)) {
g_main_context_push_thread_default(mainContext);
g_main_loop_run(innermostLoop);
g_main_context_pop_thread_default(mainContext);
return;
}
// Create and run a nested loop if the innermost one was already running.
GMainLoop* nestedMainLoop = g_main_loop_new(mainContext, FALSE);
runLoop.m_mainLoops.append(adoptGRef(nestedMainLoop));
g_main_context_push_thread_default(mainContext);
g_main_loop_run(nestedMainLoop);
g_main_context_pop_thread_default(mainContext);
runLoop.m_mainLoops.removeLast();
}
void RunLoop::stop()
{
// The innermost main loop should always be there.
ASSERT(!m_mainLoops.isEmpty());
GRefPtr<GMainLoop> lastMainLoop = m_mainLoops.last();
if (g_main_loop_is_running(lastMainLoop.get()))
g_main_loop_quit(lastMainLoop.get());
}
void RunLoop::wakeUp()
{
g_source_set_ready_time(m_source.get(), g_get_monotonic_time());
}
RunLoop::TimerBase::TimerBase(RunLoop& runLoop)
: m_runLoop(runLoop)
, m_source(adoptGRef(g_source_new(&runLoopSourceFunctions, sizeof(GSource))))
{
g_source_set_name(m_source.get(), "[WebKit] RunLoop::Timer work");
g_source_set_callback(m_source.get(), [](gpointer userData) -> gboolean {
RunLoop::TimerBase* timer = static_cast<RunLoop::TimerBase*>(userData);
timer->fired();
if (timer->m_isRepeating)
timer->updateReadyTime();
return G_SOURCE_CONTINUE;
}, this, nullptr);
g_source_attach(m_source.get(), m_runLoop.m_mainContext.get());
}
RunLoop::TimerBase::~TimerBase()
{
g_source_destroy(m_source.get());
}
void RunLoop::TimerBase::setPriority(int priority)
{
g_source_set_priority(m_source.get(), priority);
}
void RunLoop::TimerBase::updateReadyTime()
{
if (!m_fireInterval.count()) {
g_source_set_ready_time(m_source.get(), 0);
return;
}
gint64 currentTime = g_get_monotonic_time();
gint64 targetTime = currentTime + std::min<gint64>(G_MAXINT64 - currentTime, m_fireInterval.count());
ASSERT(targetTime >= currentTime);
g_source_set_ready_time(m_source.get(), targetTime);
}
void RunLoop::TimerBase::start(double fireInterval, bool repeat)
{
auto intervalDuration = std::chrono::duration<double>(fireInterval);
auto safeDuration = std::chrono::microseconds::max();
if (intervalDuration < safeDuration)
safeDuration = std::chrono::duration_cast<std::chrono::microseconds>(intervalDuration);
m_fireInterval = safeDuration;
m_isRepeating = repeat;
updateReadyTime();
}
void RunLoop::TimerBase::stop()
{
g_source_set_ready_time(m_source.get(), -1);
}
bool RunLoop::TimerBase::isActive() const
{
return g_source_get_ready_time(m_source.get()) != -1;
}
} // namespace WTF
|