[GLIB] Add API to GMainLoopSource to schedule sources after a delay in microseconds
authorcarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 17 Oct 2014 10:10:47 +0000 (10:10 +0000)
committercarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 17 Oct 2014 10:10:47 +0000 (10:10 +0000)
https://bugs.webkit.org/show_bug.cgi?id=137782

Reviewed by Sergio Villar Senin.

Source/WebCore:

* platform/gtk/SharedTimerGtk.cpp:
(WebCore::setSharedTimerFireInterval): Use microseconds instead of
milliseconds.

Source/WebKit2:

* WebProcess/WebPage/gtk/LayerTreeHostGtk.cpp:
(WebKit::LayerTreeHostGtk::layerFlushTimerFired): Use microseconds
instead of milliseconds.

Source/WTF:

In some cases when we have a double with the time in seconds, the
conversion to milliseconds ends up truncating the value to 0, and
the source scheduled immediately.

* wtf/gobject/GMainLoopSource.cpp:
(WTF::createMicrosecondsTimeoutSource): Use a custom timeout
source that handles the interval in microseconds instead of milliseconds.
(WTF::GMainLoopSource::scheduleAfterDelay): Use MicrosecondsTimeoutSource.
(WTF::GMainLoopSource::scheduleAfterDelayAndDeleteOnDestroy): Ditto.
* wtf/gobject/GMainLoopSource.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@174818 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WTF/ChangeLog
Source/WTF/wtf/gobject/GMainLoopSource.cpp
Source/WTF/wtf/gobject/GMainLoopSource.h
Source/WebCore/ChangeLog
Source/WebCore/platform/gtk/SharedTimerGtk.cpp
Source/WebKit2/ChangeLog
Source/WebKit2/WebProcess/WebPage/gtk/LayerTreeHostGtk.cpp

index 430ef76..f83f957 100644 (file)
@@ -1,3 +1,21 @@
+2014-10-17  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        [GLIB] Add API to GMainLoopSource to schedule sources after a delay in microseconds
+        https://bugs.webkit.org/show_bug.cgi?id=137782
+
+        Reviewed by Sergio Villar Senin.
+
+        In some cases when we have a double with the time in seconds, the
+        conversion to milliseconds ends up truncating the value to 0, and
+        the source scheduled immediately.
+
+        * wtf/gobject/GMainLoopSource.cpp:
+        (WTF::createMicrosecondsTimeoutSource): Use a custom timeout
+        source that handles the interval in microseconds instead of milliseconds.
+        (WTF::GMainLoopSource::scheduleAfterDelay): Use MicrosecondsTimeoutSource.
+        (WTF::GMainLoopSource::scheduleAfterDelayAndDeleteOnDestroy): Ditto.
+        * wtf/gobject/GMainLoopSource.h:
+
 2014-10-17  Dan Bernstein  <mitz@apple.com>
 
         Reverted incorrect build fix attempt.
index e5d590f..301dd22 100644 (file)
@@ -233,6 +233,69 @@ void GMainLoopSource::scheduleAfterDelay(const char* name, std::function<bool ()
     scheduleTimeoutSource(name, reinterpret_cast<GSourceFunc>(boolSourceCallback), priority, context);
 }
 
+struct MicrosecondsTimeoutSource {
+    GSource source;
+    uint64_t delay;
+};
+
+static GSourceFuncs microsecondsTimeoutSourceFunctions = {
+    nullptr, // prepare
+    nullptr, // check
+    // dispatch
+    [](GSource* source, GSourceFunc callback, gpointer userData) -> gboolean
+    {
+        bool repeat = callback(userData);
+        if (repeat)
+            g_source_set_ready_time(source, g_source_get_time(source) + reinterpret_cast<MicrosecondsTimeoutSource*>(source)->delay);
+        return repeat;
+    },
+    nullptr, // finalize
+    nullptr, // closure_callback
+    nullptr // closure_marshall
+};
+
+static GSource* createMicrosecondsTimeoutSource(uint64_t delay)
+{
+    GSource* source = g_source_new(&microsecondsTimeoutSourceFunctions, sizeof(MicrosecondsTimeoutSource));
+    reinterpret_cast<MicrosecondsTimeoutSource*>(source)->delay = delay;
+    g_source_set_ready_time(source, g_get_monotonic_time() + delay);
+    return source;
+}
+
+void GMainLoopSource::scheduleAfterDelay(const char* name, std::function<void ()> function, std::chrono::microseconds delay, int priority, std::function<void ()> destroyFunction, GMainContext* context)
+{
+    cancel();
+
+    ASSERT(!m_context.source);
+    m_context = {
+        adoptGRef(createMicrosecondsTimeoutSource(delay.count())),
+        nullptr, // cancellable
+        nullptr, // socketCancellable
+        WTF::move(function),
+        nullptr, // boolCallback
+        nullptr, // socketCallback
+        WTF::move(destroyFunction)
+    };
+    scheduleTimeoutSource(name, reinterpret_cast<GSourceFunc>(voidSourceCallback), priority, context);
+}
+
+void GMainLoopSource::scheduleAfterDelay(const char* name, std::function<bool ()> function, std::chrono::microseconds delay, int priority, std::function<void ()> destroyFunction, GMainContext* context)
+{
+    cancel();
+
+    ASSERT(!m_context.source);
+    m_context = {
+        adoptGRef(createMicrosecondsTimeoutSource(delay.count())),
+        nullptr, // cancellable
+        nullptr, // socketCancellable
+        nullptr, // voidCallback
+        WTF::move(function),
+        nullptr, // socketCallback
+        WTF::move(destroyFunction)
+    };
+    scheduleTimeoutSource(name, reinterpret_cast<GSourceFunc>(boolSourceCallback), priority, context);
+}
+
 void GMainLoopSource::scheduleAndDeleteOnDestroy(const char* name, std::function<void()> function, int priority, std::function<void()> destroyFunction, GMainContext* context)
 {
     create().schedule(name, function, priority, destroyFunction, context);
@@ -263,6 +326,16 @@ void GMainLoopSource::scheduleAfterDelayAndDeleteOnDestroy(const char* name, std
     create().scheduleAfterDelay(name, function, delay, priority, destroyFunction, context);
 }
 
+void GMainLoopSource::scheduleAfterDelayAndDeleteOnDestroy(const char* name, std::function<void()> function, std::chrono::microseconds delay, int priority, std::function<void()> destroyFunction, GMainContext* context)
+{
+    create().scheduleAfterDelay(name, function, delay, priority, destroyFunction, context);
+}
+
+void GMainLoopSource::scheduleAfterDelayAndDeleteOnDestroy(const char* name, std::function<bool()> function, std::chrono::microseconds delay, int priority, std::function<void()> destroyFunction, GMainContext* context)
+{
+    create().scheduleAfterDelay(name, function, delay, priority, destroyFunction, context);
+}
+
 bool GMainLoopSource::prepareVoidCallback(Context& context)
 {
     if (!m_context.source)
index 36dd459..097fda7 100644 (file)
@@ -56,6 +56,8 @@ public:
     WTF_EXPORT_PRIVATE virtual void scheduleAfterDelay(const char* name, std::function<bool()>, std::chrono::milliseconds, int priority = G_PRIORITY_DEFAULT, std::function<void()> destroyFunction = nullptr, GMainContext* = nullptr);
     WTF_EXPORT_PRIVATE virtual void scheduleAfterDelay(const char* name, std::function<void()>, std::chrono::seconds, int priority = G_PRIORITY_DEFAULT, std::function<void()> destroyFunction = nullptr, GMainContext* = nullptr);
     WTF_EXPORT_PRIVATE virtual void scheduleAfterDelay(const char* name, std::function<bool()>, std::chrono::seconds, int priority = G_PRIORITY_DEFAULT, std::function<void()> destroyFunction = nullptr, GMainContext* = nullptr);
+    WTF_EXPORT_PRIVATE virtual void scheduleAfterDelay(const char* name, std::function<void()>, std::chrono::microseconds, int priority = G_PRIORITY_DEFAULT, std::function<void()> destroyFunction = nullptr, GMainContext* = nullptr);
+    WTF_EXPORT_PRIVATE virtual void scheduleAfterDelay(const char* name, std::function<bool()>, std::chrono::microseconds, int priority = G_PRIORITY_DEFAULT, std::function<void()> destroyFunction = nullptr, GMainContext* = nullptr);
     WTF_EXPORT_PRIVATE virtual void cancel();
 
     WTF_EXPORT_PRIVATE void schedule(const char* name, std::function<bool(GIOCondition)>, GSocket*, GIOCondition, std::function<void()> destroyFunction = nullptr, GMainContext* = nullptr);
@@ -66,6 +68,8 @@ public:
     static void scheduleAfterDelayAndDeleteOnDestroy(const char* name, std::function<bool()>, std::chrono::milliseconds, int priority = G_PRIORITY_DEFAULT, std::function<void()> destroyFunction = nullptr, GMainContext* = nullptr);
     static void scheduleAfterDelayAndDeleteOnDestroy(const char* name, std::function<void()>, std::chrono::seconds, int priority = G_PRIORITY_DEFAULT, std::function<void()> destroyFunction = nullptr, GMainContext* = nullptr);
     static void scheduleAfterDelayAndDeleteOnDestroy(const char* name, std::function<bool()>, std::chrono::seconds, int priority = G_PRIORITY_DEFAULT, std::function<void()> destroyFunction = nullptr, GMainContext* = nullptr);
+    static void scheduleAfterDelayAndDeleteOnDestroy(const char* name, std::function<void()>, std::chrono::microseconds, int priority = G_PRIORITY_DEFAULT, std::function<void()> destroyFunction = nullptr, GMainContext* = nullptr);
+    static void scheduleAfterDelayAndDeleteOnDestroy(const char* name, std::function<bool()>, std::chrono::microseconds, int priority = G_PRIORITY_DEFAULT, std::function<void()> destroyFunction = nullptr, GMainContext* = nullptr);
 
 protected:
     enum Status { Ready, Scheduled, Dispatching };
index 977cd2d..27cba9e 100644 (file)
@@ -1,5 +1,16 @@
 2014-10-17  Carlos Garcia Campos  <cgarcia@igalia.com>
 
+        [GLIB] Add API to GMainLoopSource to schedule sources after a delay in microseconds
+        https://bugs.webkit.org/show_bug.cgi?id=137782
+
+        Reviewed by Sergio Villar Senin.
+
+        * platform/gtk/SharedTimerGtk.cpp:
+        (WebCore::setSharedTimerFireInterval): Use microseconds instead of
+        milliseconds.
+
+2014-10-17  Carlos Garcia Campos  <cgarcia@igalia.com>
+
         [GTK] Move touch events handling from Platform to WebKit2
         https://bugs.webkit.org/show_bug.cgi?id=137735
 
index ec90fd4..bdc3c55 100644 (file)
@@ -48,7 +48,7 @@ void setSharedTimerFireInterval(double interval)
     ASSERT(sharedTimerFiredFunction);
 
     gSharedTimer.scheduleAfterDelay("[WebKit] sharedTimerTimeoutCallback", std::function<void()>(sharedTimerFiredFunction),
-        std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::duration<double>(interval)), GDK_PRIORITY_REDRAW);
+        std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::duration<double>(interval)), GDK_PRIORITY_REDRAW);
 }
 
 void stopSharedTimer()
index 38f2821..0532dcd 100644 (file)
@@ -1,5 +1,16 @@
 2014-10-17  Carlos Garcia Campos  <cgarcia@igalia.com>
 
+        [GLIB] Add API to GMainLoopSource to schedule sources after a delay in microseconds
+        https://bugs.webkit.org/show_bug.cgi?id=137782
+
+        Reviewed by Sergio Villar Senin.
+
+        * WebProcess/WebPage/gtk/LayerTreeHostGtk.cpp:
+        (WebKit::LayerTreeHostGtk::layerFlushTimerFired): Use microseconds
+        instead of milliseconds.
+
+2014-10-17  Carlos Garcia Campos  <cgarcia@igalia.com>
+
         [GTK] Move touch events handling from Platform to WebKit2
         https://bugs.webkit.org/show_bug.cgi?id=137735
 
index 5e94b08..bfc796a 100644 (file)
@@ -259,7 +259,7 @@ void LayerTreeHostGtk::layerFlushTimerFired()
         const double targetFPS = 60;
         double nextFlush = std::max((1 / targetFPS) - (currentTime() - m_lastFlushTime), 0.0);
         m_layerFlushTimerCallback.scheduleAfterDelay("[WebKit] layerFlushTimer", std::bind(&LayerTreeHostGtk::layerFlushTimerFired, this),
-            std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::duration<double>(nextFlush)), GDK_PRIORITY_EVENTS);
+            std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::duration<double>(nextFlush)), GDK_PRIORITY_EVENTS);
     }
 }