| 1 | /* |
| 2 | * Copyright (C) 2010 Apple Inc. All rights reserved. |
| 3 | * Portions Copyright (c) 2010 Motorola Mobility, Inc. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 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. |
| 13 | * |
| 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. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include <wtf/RunLoop.h> |
| 29 | |
| 30 | #include <glib.h> |
| 31 | #include <wtf/MainThread.h> |
| 32 | #include <wtf/glib/RunLoopSourcePriority.h> |
| 33 | |
| 34 | namespace WTF { |
| 35 | |
| 36 | static GSourceFuncs runLoopSourceFunctions = { |
| 37 | nullptr, // prepare |
| 38 | nullptr, // check |
| 39 | // dispatch |
| 40 | [](GSource* source, GSourceFunc callback, gpointer userData) -> gboolean |
| 41 | { |
| 42 | if (g_source_get_ready_time(source) == -1) |
| 43 | return G_SOURCE_CONTINUE; |
| 44 | g_source_set_ready_time(source, -1); |
| 45 | return callback(userData); |
| 46 | }, |
| 47 | nullptr, // finalize |
| 48 | nullptr, // closure_callback |
| 49 | nullptr, // closure_marshall |
| 50 | }; |
| 51 | |
| 52 | RunLoop::RunLoop() |
| 53 | { |
| 54 | m_mainContext = g_main_context_get_thread_default(); |
| 55 | if (!m_mainContext) |
| 56 | m_mainContext = isMainThread() ? g_main_context_default() : adoptGRef(g_main_context_new()); |
| 57 | ASSERT(m_mainContext); |
| 58 | |
| 59 | GRefPtr<GMainLoop> innermostLoop = adoptGRef(g_main_loop_new(m_mainContext.get(), FALSE)); |
| 60 | ASSERT(innermostLoop); |
| 61 | m_mainLoops.append(innermostLoop); |
| 62 | |
| 63 | m_source = adoptGRef(g_source_new(&runLoopSourceFunctions, sizeof(GSource))); |
| 64 | g_source_set_priority(m_source.get(), RunLoopSourcePriority::RunLoopDispatcher); |
| 65 | g_source_set_name(m_source.get(), "[WebKit] RunLoop work" ); |
| 66 | g_source_set_can_recurse(m_source.get(), TRUE); |
| 67 | g_source_set_callback(m_source.get(), [](gpointer userData) -> gboolean { |
| 68 | static_cast<RunLoop*>(userData)->performWork(); |
| 69 | return G_SOURCE_CONTINUE; |
| 70 | }, this, nullptr); |
| 71 | g_source_attach(m_source.get(), m_mainContext.get()); |
| 72 | } |
| 73 | |
| 74 | RunLoop::~RunLoop() |
| 75 | { |
| 76 | g_source_destroy(m_source.get()); |
| 77 | |
| 78 | for (int i = m_mainLoops.size() - 1; i >= 0; --i) { |
| 79 | if (!g_main_loop_is_running(m_mainLoops[i].get())) |
| 80 | continue; |
| 81 | g_main_loop_quit(m_mainLoops[i].get()); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void RunLoop::run() |
| 86 | { |
| 87 | RunLoop& runLoop = RunLoop::current(); |
| 88 | GMainContext* mainContext = runLoop.m_mainContext.get(); |
| 89 | |
| 90 | // The innermost main loop should always be there. |
| 91 | ASSERT(!runLoop.m_mainLoops.isEmpty()); |
| 92 | |
| 93 | GMainLoop* innermostLoop = runLoop.m_mainLoops[0].get(); |
| 94 | if (!g_main_loop_is_running(innermostLoop)) { |
| 95 | g_main_context_push_thread_default(mainContext); |
| 96 | g_main_loop_run(innermostLoop); |
| 97 | g_main_context_pop_thread_default(mainContext); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | // Create and run a nested loop if the innermost one was already running. |
| 102 | GMainLoop* nestedMainLoop = g_main_loop_new(mainContext, FALSE); |
| 103 | runLoop.m_mainLoops.append(adoptGRef(nestedMainLoop)); |
| 104 | |
| 105 | g_main_context_push_thread_default(mainContext); |
| 106 | g_main_loop_run(nestedMainLoop); |
| 107 | g_main_context_pop_thread_default(mainContext); |
| 108 | |
| 109 | runLoop.m_mainLoops.removeLast(); |
| 110 | } |
| 111 | |
| 112 | void RunLoop::stop() |
| 113 | { |
| 114 | // The innermost main loop should always be there. |
| 115 | ASSERT(!m_mainLoops.isEmpty()); |
| 116 | GRefPtr<GMainLoop> lastMainLoop = m_mainLoops.last(); |
| 117 | if (g_main_loop_is_running(lastMainLoop.get())) |
| 118 | g_main_loop_quit(lastMainLoop.get()); |
| 119 | } |
| 120 | |
| 121 | void RunLoop::wakeUp() |
| 122 | { |
| 123 | g_source_set_ready_time(m_source.get(), 0); |
| 124 | } |
| 125 | |
| 126 | class DispatchAfterContext { |
| 127 | WTF_MAKE_FAST_ALLOCATED; |
| 128 | public: |
| 129 | DispatchAfterContext(Function<void()>&& function) |
| 130 | : m_function(WTFMove(function)) |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | void dispatch() |
| 135 | { |
| 136 | m_function(); |
| 137 | } |
| 138 | |
| 139 | private: |
| 140 | Function<void()> m_function; |
| 141 | }; |
| 142 | |
| 143 | void RunLoop::dispatchAfter(Seconds duration, Function<void()>&& function) |
| 144 | { |
| 145 | GRefPtr<GSource> source = adoptGRef(g_source_new(&runLoopSourceFunctions, sizeof(GSource))); |
| 146 | g_source_set_priority(source.get(), RunLoopSourcePriority::RunLoopTimer); |
| 147 | g_source_set_name(source.get(), "[WebKit] RunLoop dispatchAfter" ); |
| 148 | g_source_set_ready_time(source.get(), g_get_monotonic_time() + duration.microsecondsAs<gint64>()); |
| 149 | |
| 150 | std::unique_ptr<DispatchAfterContext> context = std::make_unique<DispatchAfterContext>(WTFMove(function)); |
| 151 | g_source_set_callback(source.get(), [](gpointer userData) -> gboolean { |
| 152 | std::unique_ptr<DispatchAfterContext> context(static_cast<DispatchAfterContext*>(userData)); |
| 153 | context->dispatch(); |
| 154 | return G_SOURCE_REMOVE; |
| 155 | }, context.release(), nullptr); |
| 156 | g_source_attach(source.get(), m_mainContext.get()); |
| 157 | } |
| 158 | |
| 159 | RunLoop::TimerBase::TimerBase(RunLoop& runLoop) |
| 160 | : m_runLoop(runLoop) |
| 161 | , m_source(adoptGRef(g_source_new(&runLoopSourceFunctions, sizeof(GSource)))) |
| 162 | { |
| 163 | g_source_set_priority(m_source.get(), RunLoopSourcePriority::RunLoopTimer); |
| 164 | g_source_set_name(m_source.get(), "[WebKit] RunLoop::Timer work" ); |
| 165 | g_source_set_callback(m_source.get(), [](gpointer userData) -> gboolean { |
| 166 | // fired() executes the user's callback. It may destroy timer, |
| 167 | // so we must check if the source is still active afterwards |
| 168 | // before it is safe to dereference timer again. |
| 169 | RunLoop::TimerBase* timer = static_cast<RunLoop::TimerBase*>(userData); |
| 170 | GSource* source = timer->m_source.get(); |
| 171 | timer->fired(); |
| 172 | if (g_source_is_destroyed(source)) |
| 173 | return G_SOURCE_REMOVE; |
| 174 | if (timer->m_isRepeating) |
| 175 | timer->updateReadyTime(); |
| 176 | return G_SOURCE_CONTINUE; |
| 177 | }, this, nullptr); |
| 178 | g_source_attach(m_source.get(), m_runLoop->m_mainContext.get()); |
| 179 | } |
| 180 | |
| 181 | RunLoop::TimerBase::~TimerBase() |
| 182 | { |
| 183 | g_source_destroy(m_source.get()); |
| 184 | } |
| 185 | |
| 186 | void RunLoop::TimerBase::setName(const char* name) |
| 187 | { |
| 188 | g_source_set_name(m_source.get(), name); |
| 189 | } |
| 190 | |
| 191 | void RunLoop::TimerBase::setPriority(int priority) |
| 192 | { |
| 193 | g_source_set_priority(m_source.get(), priority); |
| 194 | } |
| 195 | |
| 196 | void RunLoop::TimerBase::updateReadyTime() |
| 197 | { |
| 198 | if (!m_fireInterval) { |
| 199 | g_source_set_ready_time(m_source.get(), 0); |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | gint64 currentTime = g_get_monotonic_time(); |
| 204 | gint64 targetTime = currentTime + std::min<gint64>(G_MAXINT64 - currentTime, m_fireInterval.microsecondsAs<gint64>()); |
| 205 | ASSERT(targetTime >= currentTime); |
| 206 | g_source_set_ready_time(m_source.get(), targetTime); |
| 207 | } |
| 208 | |
| 209 | void RunLoop::TimerBase::start(Seconds fireInterval, bool repeat) |
| 210 | { |
| 211 | m_fireInterval = fireInterval; |
| 212 | m_isRepeating = repeat; |
| 213 | updateReadyTime(); |
| 214 | } |
| 215 | |
| 216 | void RunLoop::TimerBase::stop() |
| 217 | { |
| 218 | g_source_set_ready_time(m_source.get(), -1); |
| 219 | } |
| 220 | |
| 221 | bool RunLoop::TimerBase::isActive() const |
| 222 | { |
| 223 | return g_source_get_ready_time(m_source.get()) != -1; |
| 224 | } |
| 225 | |
| 226 | Seconds RunLoop::TimerBase::secondsUntilFire() const |
| 227 | { |
| 228 | gint64 time = g_source_get_ready_time(m_source.get()); |
| 229 | if (time != -1) |
| 230 | return std::max<Seconds>(Seconds::fromMicroseconds(time - g_get_monotonic_time()), 0_s); |
| 231 | return 0_s; |
| 232 | } |
| 233 | |
| 234 | } // namespace WTF |
| 235 | |