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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
/*
* 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 <wtf/RunLoop.h>
#include <glib.h>
#include <wtf/MainThread.h>
#include <wtf/glib/RunLoopSourcePriority.h>
namespace WTF {
typedef struct {
GSource source;
RunLoop* runLoop;
} RunLoopSource;
GSourceFuncs RunLoop::s_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);
const char* name = g_source_get_name(source);
auto& runLoopSource = *reinterpret_cast<RunLoopSource*>(source);
runLoopSource.runLoop->notify(RunLoop::Event::WillDispatch, name);
auto returnValue = callback(userData);
runLoopSource.runLoop->notify(RunLoop::Event::DidDispatch, name);
return returnValue;
},
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(&RunLoop::s_runLoopSourceFunctions, sizeof(RunLoopSource)));
auto& runLoopSource = *reinterpret_cast<RunLoopSource*>(m_source.get());
runLoopSource.runLoop = this;
g_source_set_priority(m_source.get(), RunLoopSourcePriority::RunLoopDispatcher);
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()
{
Ref 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(), 0);
}
RunLoop::CycleResult RunLoop::cycle(RunLoopMode)
{
g_main_context_iteration(NULL, FALSE);
return CycleResult::Continue;
}
void RunLoop::observe(const RunLoop::Observer& observer)
{
ASSERT(!m_observers.contains(observer));
m_observers.add(observer);
}
void RunLoop::notify(RunLoop::Event event, const char* name)
{
if (m_observers.isEmptyIgnoringNullReferences())
return;
m_observers.forEach([event, name = String::fromUTF8(name)](auto& observer) {
observer(event, name);
});
}
RunLoop::TimerBase::TimerBase(Ref<RunLoop>&& runLoop)
: m_runLoop(WTFMove(runLoop))
, m_source(adoptGRef(g_source_new(&RunLoop::s_runLoopSourceFunctions, sizeof(RunLoopSource))))
{
auto& runLoopSource = *reinterpret_cast<RunLoopSource*>(m_source.get());
runLoopSource.runLoop = m_runLoop.ptr();
g_source_set_priority(m_source.get(), RunLoopSourcePriority::RunLoopTimer);
g_source_set_name(m_source.get(), "[WebKit] RunLoop::Timer work");
g_source_set_callback(m_source.get(), [](gpointer userData) -> gboolean {
// fired() executes the user's callback. It may destroy timer,
// so we must check if the source is still active afterwards
// before it is safe to dereference timer again.
RunLoop::TimerBase* timer = static_cast<RunLoop::TimerBase*>(userData);
GSource* source = timer->m_source.get();
if (timer->m_isRepeating)
timer->updateReadyTime();
timer->fired();
if (g_source_is_destroyed(source))
return G_SOURCE_REMOVE;
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::setName(ASCIILiteral name)
{
g_source_set_name(m_source.get(), name);
}
void RunLoop::TimerBase::setPriority(int priority)
{
g_source_set_priority(m_source.get(), priority);
}
void RunLoop::TimerBase::updateReadyTime()
{
if (!m_interval) {
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_interval.microsecondsAs<gint64>());
ASSERT(targetTime >= currentTime);
g_source_set_ready_time(m_source.get(), targetTime);
}
void RunLoop::TimerBase::start(Seconds interval, bool repeat)
{
m_interval = interval;
m_isRepeating = repeat;
updateReadyTime();
}
void RunLoop::TimerBase::stop()
{
g_source_set_ready_time(m_source.get(), -1);
m_interval = { };
m_isRepeating = false;
}
bool RunLoop::TimerBase::isActive() const
{
return g_source_get_ready_time(m_source.get()) != -1;
}
Seconds RunLoop::TimerBase::secondsUntilFire() const
{
gint64 time = g_source_get_ready_time(m_source.get());
if (time != -1)
return std::max<Seconds>(Seconds::fromMicroseconds(time - g_get_monotonic_time()), 0_s);
return 0_s;
}
} // namespace WTF
|