Timer scheduling should be based off the monotonic clock
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 18 Jul 2011 20:38:42 +0000 (20:38 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 18 Jul 2011 20:38:42 +0000 (20:38 +0000)
https://bugs.webkit.org/show_bug.cgi?id=64544

Patch by James Robinson <jamesr@chromium.org> on 2011-07-18
Reviewed by Darin Adler.

Source/JavaScriptCore:

Switches ThreadCondition::timedWait and related utility functions from currentTime() to
monotonicallyIncreasingTime().

Add WTF::monotonicallyIncreasingTime() to list of exported functions so it can be accessed from WebCore/WebKit.

* JavaScriptCore.exp:
* JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.def:
* wtf/ThreadingPthreads.cpp:
(WTF::ThreadCondition::timedWait):
* wtf/ThreadingWin.cpp:
(WTF::absoluteTimeToWaitTimeoutInterval):
* wtf/gtk/ThreadingGtk.cpp:
(WTF::ThreadCondition::timedWait):
* wtf/qt/ThreadingQt.cpp:
(WTF::ThreadCondition::timedWait):

Source/WebCore:

Changes the Timer scheduling logic from using absolute values in terms of currentTime() to using relative
intervals in terms of monotonicallyIncreasingTime().  This provides better standards compliance, compatibility,
and predictability when the system clock is adjusted.

No automated tests since there is no way to modify the system clock from DRT.

* platform/SharedTimer.h:
(WebCore::MainThreadSharedTimer::setFireInterval):
* platform/ThreadTimers.cpp:
(WebCore::ThreadTimers::updateSharedTimer):
(WebCore::ThreadTimers::sharedTimerFiredInternal):
* platform/Timer.cpp:
(WebCore::TimerBase::start):
(WebCore::TimerBase::nextFireInterval):
* platform/android/SharedTimerAndroid.cpp:
(WebCore::setSharedTimerFireInterval):
* platform/brew/SharedTimerBrew.cpp:
(WebCore::setSharedTimerFireInterval):
* platform/chromium/PlatformBridge.h:
* platform/chromium/SharedTimerChromium.cpp:
(WebCore::setSharedTimerFireInterval):
* platform/efl/SharedTimerEfl.cpp:
(WebCore::addNewTimer):
(WebCore::setSharedTimerFireInterval):
* platform/gtk/SharedTimerGtk.cpp:
(WebCore::setSharedTimerFireInterval):
* platform/haiku/SharedTimerHaiku.cpp:
(WebCore::SharedTimerHaiku::start):
(WebCore::setSharedTimerFireInterval):
* platform/mac/SharedTimerMac.mm:
(WebCore::setSharedTimerFireInterval):
* platform/qt/SharedTimerQt.cpp:
(WebCore::SharedTimerQt::start):
(WebCore::setSharedTimerFireInterval):
* platform/win/SharedTimerWin.cpp:
(WebCore::setSharedTimerFireInterval):
* platform/wince/SharedTimerWinCE.cpp:
(WebCore::setSharedTimerFireInterval):
* platform/wx/SharedTimerWx.cpp:
(WebCore::setSharedTimerFireInterval):
* workers/WorkerRunLoop.cpp:
(WebCore::WorkerSharedTimer::setFireInterval):

Source/WebKit/chromium:

Renames setSharedTimerFireTime to setSharedTimerFireInterval to be consistent with WebCore.

* public/WebKitClient.h:
(WebKit::WebKitClient::setSharedTimerFireInterval):
* src/PlatformBridge.cpp:
(WebCore::PlatformBridge::setSharedTimerFireInterval):

Source/WebKit2:

Converts the WebKit2 RunLoop and CoreIPC timeouts to use monotonicallyIncreasingTime().

* Platform/CoreIPC/Connection.cpp:
(CoreIPC::Connection::waitForMessage):
(CoreIPC::Connection::waitForSyncReply):
* Platform/RunLoop.h:

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

30 files changed:
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/JavaScriptCore.exp
Source/JavaScriptCore/JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.def
Source/JavaScriptCore/wtf/ThreadingPthreads.cpp
Source/JavaScriptCore/wtf/ThreadingWin.cpp
Source/JavaScriptCore/wtf/gtk/ThreadingGtk.cpp
Source/JavaScriptCore/wtf/qt/ThreadingQt.cpp
Source/WebCore/ChangeLog
Source/WebCore/platform/SharedTimer.h
Source/WebCore/platform/ThreadTimers.cpp
Source/WebCore/platform/Timer.cpp
Source/WebCore/platform/android/SharedTimerAndroid.cpp
Source/WebCore/platform/brew/SharedTimerBrew.cpp
Source/WebCore/platform/chromium/PlatformBridge.h
Source/WebCore/platform/chromium/SharedTimerChromium.cpp
Source/WebCore/platform/efl/SharedTimerEfl.cpp
Source/WebCore/platform/gtk/SharedTimerGtk.cpp
Source/WebCore/platform/haiku/SharedTimerHaiku.cpp
Source/WebCore/platform/mac/SharedTimerMac.mm
Source/WebCore/platform/qt/SharedTimerQt.cpp
Source/WebCore/platform/win/SharedTimerWin.cpp
Source/WebCore/platform/wince/SharedTimerWinCE.cpp
Source/WebCore/platform/wx/SharedTimerWx.cpp
Source/WebCore/workers/WorkerRunLoop.cpp
Source/WebKit/chromium/ChangeLog
Source/WebKit/chromium/public/WebKitClient.h
Source/WebKit/chromium/src/PlatformBridge.cpp
Source/WebKit2/ChangeLog
Source/WebKit2/Platform/CoreIPC/Connection.cpp
Source/WebKit2/Platform/RunLoop.h

index 2f3c6cb..727cd2f 100644 (file)
@@ -1,3 +1,26 @@
+2011-07-18  James Robinson  <jamesr@chromium.org>
+
+        Timer scheduling should be based off the monotonic clock
+        https://bugs.webkit.org/show_bug.cgi?id=64544
+
+        Reviewed by Darin Adler.
+
+        Switches ThreadCondition::timedWait and related utility functions from currentTime() to
+        monotonicallyIncreasingTime().
+
+        Add WTF::monotonicallyIncreasingTime() to list of exported functions so it can be accessed from WebCore/WebKit.
+
+        * JavaScriptCore.exp:
+        * JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.def:
+        * wtf/ThreadingPthreads.cpp:
+        (WTF::ThreadCondition::timedWait):
+        * wtf/ThreadingWin.cpp:
+        (WTF::absoluteTimeToWaitTimeoutInterval):
+        * wtf/gtk/ThreadingGtk.cpp:
+        (WTF::ThreadCondition::timedWait):
+        * wtf/qt/ThreadingQt.cpp:
+        (WTF::ThreadCondition::timedWait):
+
 2011-07-18  Filip Pizlo  <fpizlo@apple.com>
 
         JSC JIT does not inline GC allocation fast paths
index d0fd447..4f9c30d 100644 (file)
@@ -449,6 +449,7 @@ __ZN3WTF22charactersToUIntStrictEPKtmPbi
 __ZN3WTF23callOnMainThreadAndWaitEPFvPvES0_
 __ZN3WTF23dayInMonthFromDayInYearEib
 __ZN3WTF23waitForThreadCompletionEjPPv
+__ZN3WTF27monotonicallyIncreasingTimeEv
 __ZN3WTF27releaseFastMallocFreeMemoryEv
 __ZN3WTF28setMainThreadCallbacksPausedEb
 __ZN3WTF29cryptographicallyRandomNumberEv
index b91f5ae..22b6cbe 100644 (file)
@@ -240,6 +240,7 @@ EXPORTS
     ?lookupGetter@JSObject@JSC@@UAE?AVJSValue@2@PAVExecState@2@ABVIdentifier@2@@Z
     ?lookupSetter@JSObject@JSC@@UAE?AVJSValue@2@PAVExecState@2@ABVIdentifier@2@@Z
     ?materializePropertyMap@Structure@JSC@@AAEXAAVJSGlobalData@2@@Z
+    ?monotonicallyIncreasingTime@WTF@@YANXZ
     ?monthFromDayInYear@WTF@@YAHH_N@Z
     ?msToYear@WTF@@YAHN@Z
     ?name@InternalFunction@JSC@@QAEABVUString@2@PAVExecState@2@@Z
index 916aa36..b962294 100644 (file)
@@ -363,7 +363,7 @@ void ThreadCondition::wait(Mutex& mutex)
 
 bool ThreadCondition::timedWait(Mutex& mutex, double absoluteTime)
 {
-    if (absoluteTime < currentTime())
+    if (absoluteTime < monotonicallyIncreasingTime())
         return false;
 
     if (absoluteTime > INT_MAX) {
index c452205..6cd187e 100644 (file)
@@ -483,7 +483,7 @@ void ThreadCondition::broadcast()
 
 DWORD absoluteTimeToWaitTimeoutInterval(double absoluteTime)
 {
-    double currentTime = WTF::currentTime();
+    double currentTime = monotonicallyIncreasingTime();
 
     // Time is in the past - return immediately.
     if (absoluteTime < currentTime)
index 863ee81..dbee271 100644 (file)
@@ -213,7 +213,7 @@ void ThreadCondition::wait(Mutex& mutex)
 bool ThreadCondition::timedWait(Mutex& mutex, double absoluteTime)
 {
     // Time is in the past - return right away.
-    if (absoluteTime < currentTime())
+    if (absoluteTime < monotonicallyIncreasingTime())
         return false;
     
     // Time is too far in the future for g_cond_timed_wait - wait forever.
index 8041dea..e6820af 100644 (file)
@@ -255,7 +255,7 @@ void ThreadCondition::wait(Mutex& mutex)
 
 bool ThreadCondition::timedWait(Mutex& mutex, double absoluteTime)
 {
-    double currentTime = WTF::currentTime();
+    double currentTime = monotonicallyIncreasingTime();
 
     // Time is in the past - return immediately.
     if (absoluteTime < currentTime)
index 4b54758..7213046 100644 (file)
@@ -1,3 +1,53 @@
+2011-07-18  James Robinson  <jamesr@chromium.org>
+
+        Timer scheduling should be based off the monotonic clock
+        https://bugs.webkit.org/show_bug.cgi?id=64544
+
+        Reviewed by Darin Adler.
+
+        Changes the Timer scheduling logic from using absolute values in terms of currentTime() to using relative
+        intervals in terms of monotonicallyIncreasingTime().  This provides better standards compliance, compatibility,
+        and predictability when the system clock is adjusted.
+
+        No automated tests since there is no way to modify the system clock from DRT.
+
+        * platform/SharedTimer.h:
+        (WebCore::MainThreadSharedTimer::setFireInterval):
+        * platform/ThreadTimers.cpp:
+        (WebCore::ThreadTimers::updateSharedTimer):
+        (WebCore::ThreadTimers::sharedTimerFiredInternal):
+        * platform/Timer.cpp:
+        (WebCore::TimerBase::start):
+        (WebCore::TimerBase::nextFireInterval):
+        * platform/android/SharedTimerAndroid.cpp:
+        (WebCore::setSharedTimerFireInterval):
+        * platform/brew/SharedTimerBrew.cpp:
+        (WebCore::setSharedTimerFireInterval):
+        * platform/chromium/PlatformBridge.h:
+        * platform/chromium/SharedTimerChromium.cpp:
+        (WebCore::setSharedTimerFireInterval):
+        * platform/efl/SharedTimerEfl.cpp:
+        (WebCore::addNewTimer):
+        (WebCore::setSharedTimerFireInterval):
+        * platform/gtk/SharedTimerGtk.cpp:
+        (WebCore::setSharedTimerFireInterval):
+        * platform/haiku/SharedTimerHaiku.cpp:
+        (WebCore::SharedTimerHaiku::start):
+        (WebCore::setSharedTimerFireInterval):
+        * platform/mac/SharedTimerMac.mm:
+        (WebCore::setSharedTimerFireInterval):
+        * platform/qt/SharedTimerQt.cpp:
+        (WebCore::SharedTimerQt::start):
+        (WebCore::setSharedTimerFireInterval):
+        * platform/win/SharedTimerWin.cpp:
+        (WebCore::setSharedTimerFireInterval):
+        * platform/wince/SharedTimerWinCE.cpp:
+        (WebCore::setSharedTimerFireInterval):
+        * platform/wx/SharedTimerWx.cpp:
+        (WebCore::setSharedTimerFireInterval):
+        * workers/WorkerRunLoop.cpp:
+        (WebCore::WorkerSharedTimer::setFireInterval):
+
 2011-07-18  Pratik Solanki  <psolanki@apple.com>
 
         Unreviewed. Fix Windows build. Move static function defaultSessionCookieStorage() to before
index 16e0d0b..6acaba9 100644 (file)
@@ -41,9 +41,8 @@ namespace WebCore {
         virtual ~SharedTimer() {}
         virtual void setFiredFunction(void (*)()) = 0;
 
-        // The fire time is relative to the classic POSIX epoch of January 1, 1970,
-        // as the result of currentTime() is.
-        virtual void setFireTime(double) = 0;
+        // The fire interval is in seconds relative to the current monotonic clock time.
+        virtual void setFireInterval(double) = 0;
         virtual void stop() = 0;
     };
 
@@ -51,7 +50,7 @@ namespace WebCore {
     // Implemented by port (since it provides the run loop for the main thread).
     // FIXME: make ports implement MainThreadSharedTimer directly instead.
     void setSharedTimerFiredFunction(void (*)());
-    void setSharedTimerFireTime(double);
+    void setSharedTimerFireInterval(double);
     void stopSharedTimer();
 
     // Implementation of SharedTimer for the main thread.
@@ -62,9 +61,9 @@ namespace WebCore {
             setSharedTimerFiredFunction(function);
         }
         
-        virtual void setFireTime(double fireTime)
+        virtual void setFireInterval(double interval)
         {
-            setSharedTimerFireTime(fireTime);
+            setSharedTimerFireInterval(interval);
         }
         
         virtual void stop()
index 3ae8207..fbe66cc 100644 (file)
@@ -32,6 +32,8 @@
 #include "Timer.h"
 #include <wtf/CurrentTime.h>
 
+using namespace std;
+
 namespace WebCore {
 
 // Fire timers for this length of time, and then quit to let the run loop process user input events.
@@ -81,7 +83,7 @@ void ThreadTimers::updateSharedTimer()
     if (m_firingTimers || m_timerHeap.isEmpty())
         m_sharedTimer->stop();
     else
-        m_sharedTimer->setFireTime(m_timerHeap.first()->m_nextFireTime);
+        m_sharedTimer->setFireInterval(max(m_timerHeap.first()->m_nextFireTime - monotonicallyIncreasingTime(), 0.0));
 }
 
 void ThreadTimers::sharedTimerFired()
@@ -97,7 +99,7 @@ void ThreadTimers::sharedTimerFiredInternal()
         return;
     m_firingTimers = true;
 
-    double fireTime = currentTime();
+    double fireTime = monotonicallyIncreasingTime();
     double timeToQuit = fireTime + maxDurationOfFiringTimers;
 
     while (!m_timerHeap.isEmpty() && m_timerHeap.first()->m_nextFireTime <= fireTime) {
@@ -112,7 +114,7 @@ void ThreadTimers::sharedTimerFiredInternal()
         timer->fired();
 
         // Catch the case where the timer asked timers to fire in a nested event loop, or we are over time limit.
-        if (!m_firingTimers || timeToQuit < currentTime())
+        if (!m_firingTimers || timeToQuit < monotonicallyIncreasingTime())
             break;
     }
 
index 3f4e334..b2f0d21 100644 (file)
@@ -181,7 +181,7 @@ void TimerBase::start(double nextFireInterval, double repeatInterval)
     ASSERT(m_thread == currentThread());
 
     m_repeatInterval = repeatInterval;
-    setNextFireTime(currentTime() + nextFireInterval);
+    setNextFireTime(monotonicallyIncreasingTime() + nextFireInterval);
 }
 
 void TimerBase::stop()
@@ -199,7 +199,7 @@ void TimerBase::stop()
 double TimerBase::nextFireInterval() const
 {
     ASSERT(isActive());
-    double current = currentTime();
+    double current = monotonicallyIncreasingTime();
     if (m_nextFireTime < current)
         return 0;
     return m_nextFireTime - current;
index e4f3b36..39d318c 100644 (file)
@@ -45,13 +45,11 @@ void setSharedTimerFiredFunction(void (*f)())
         JavaSharedClient::GetTimerClient()->setSharedTimerCallback(f);
 }
 
-// The fire time is relative to the classic POSIX epoch of January 1, 1970,
-// as the result of currentTime() is.
-void setSharedTimerFireTime(double fireTime)
+void setSharedTimerFireInterval(double interval)
 {
-    long long timeInMs = static_cast<long long>((fireTime - WTF::currentTime()) * 1000);
+    long long timeInMs = static_cast<long long>(interval * 1000);
 
-    LOGV("setSharedTimerFireTime: in %ld millisec", timeInMs);
+    LOGV("setSharedTimerFireInterval: in %ld millisec", timeInMs);
     if (JavaSharedClient::GetTimerClient())
         JavaSharedClient::GetTimerClient()->setSharedTimer(timeInMs);
 }
index d20fe48..b171ea0 100644 (file)
@@ -48,21 +48,13 @@ void setSharedTimerFiredFunction(void (*f)())
     sharedTimerFiredFunction = f;
 }
 
-void setSharedTimerFireTime(double fireTime)
+void setSharedTimerFireInterval(double interval)
 {
     ASSERT(sharedTimerFiredFunction);
 
     CALLBACK_Cancel(&sharedTimerCallback);
 
-    double interval = fireTime - currentTime();
-    int intervalInMS;
-
-    if (interval < 0)
-        intervalInMS = 0;
-    else {
-        interval *= 1000;
-        intervalInMS = static_cast<int>(interval);
-    }
+    int intervalInMS = statit_cast<int>(interval * 1000);
 
     sharedTimerCallback.pfnCancel = 0;
     sharedTimerCallback.pfnNotify = invokeCallback;
index 6eb0871..e01ae26 100644 (file)
@@ -234,7 +234,7 @@ public:
 
     // SharedTimers -------------------------------------------------------
     static void setSharedTimerFiredFunction(void (*func)());
-    static void setSharedTimerFireTime(double);
+    static void setSharedTimerFireInterval(double);
     static void stopSharedTimer();
 
     // StatsCounters ------------------------------------------------------
index 5acb253..e091f55 100644 (file)
@@ -35,9 +35,9 @@ void setSharedTimerFiredFunction(void (*f)())
     PlatformBridge::setSharedTimerFiredFunction(f);
 }
 
-void setSharedTimerFireTime(double fireTime)
+void setSharedTimerFireInterval(double fireTime)
 {
-    PlatformBridge::setSharedTimerFireTime(fireTime);
+    PlatformBridge::setSharedTimerFireInterval(fireTime);
 }
 
 void stopSharedTimer()
index ad7fd27..a2442a6 100644 (file)
@@ -62,17 +62,16 @@ void stopSharedTimer()
     }
 }
 
-void addNewTimer(double fireTime)
+void addNewTimer(double interval)
 {
-    double interval = fireTime - currentTime();
     stopSharedTimer();
 
     _sharedTimer = ecore_timer_loop_add(interval, timerEvent, 0);
 }
 
-void setSharedTimerFireTime(double fireTime)
+void setSharedTimerFireInterval(double interval)
 {
-    addNewTimer(fireTime);
+    addNewTimer(interval);
 }
 
 }
index ee4d75b..b421fda 100644 (file)
@@ -50,18 +50,11 @@ static gboolean timeout_cb(gpointer)
     return FALSE;
 }
 
-void setSharedTimerFireTime(double fireTime)
+void setSharedTimerFireInterval(double interval)
 {
     ASSERT(sharedTimerFiredFunction);
 
-    double interval = fireTime - currentTime();
-    guint intervalInMS;
-    if (interval < 0)
-        intervalInMS = 0;
-    else {
-        interval *= 1000;
-        intervalInMS = (guint)interval;
-    }
+    guint intervalInMS = static_cast<guint>(interval * 1000);
 
     stopSharedTimer();
     sharedTimer = g_timeout_add_full(GDK_PRIORITY_REDRAW, intervalInMS, timeout_cb, 0, 0);
index 6265a2a..d68ec28 100644 (file)
@@ -82,12 +82,11 @@ SharedTimerHaiku* SharedTimerHaiku::instance()
     return timer;
 }
 
-void SharedTimerHaiku::start(double fireTime)
+void SharedTimerHaiku::start(double intervalInSeconds)
 {
     m_shouldRun = true;
 
-    double intervalInSeconds = fireTime - currentTime();
-    bigtime_t intervalInMicroSeconds = intervalInSeconds < 0 ? 0 : intervalInSeconds * 1000000;
+    bigtime_t intervalInMicroSeconds = intervalInSeconds * 1000000;
 
     BMessageRunner::StartSending(Looper(), new BMessage(FIRE_MESSAGE), intervalInMicroSeconds, 1);
 }
@@ -111,9 +110,9 @@ void setSharedTimerFiredFunction(void (*f)())
     SharedTimerHaiku::instance()->m_timerFunction = f;
 }
 
-void setSharedTimerFireTime(double fireTime)
+void setSharedTimerFireInterval(double interval)
 {
-    SharedTimerHaiku::instance()->start(fireTime);
+    SharedTimerHaiku::instance()->start(interval);
 }
 
 void stopSharedTimer()
index 4448c21..d7c501d 100644 (file)
@@ -167,7 +167,7 @@ static void timerFired(CFRunLoopTimerRef, void*)
     [pool drain];
 }
 
-void setSharedTimerFireTime(double fireTime)
+void setSharedTimerFireInterval(double interval)
 {
     ASSERT(sharedTimerFiredFunction);
 
@@ -176,7 +176,7 @@ void setSharedTimerFireTime(double fireTime)
         CFRelease(sharedTimer);
     }
 
-    CFAbsoluteTime fireDate = fireTime - kCFAbsoluteTimeIntervalSince1970;
+    CFAbsoluteTime fireDate = CFAbsoluteTimeGetCurrent() + interval;
     sharedTimer = CFRunLoopTimerCreate(0, fireDate, 0, 0, 0, timerFired, 0);
     CFRunLoopAddTimer(CFRunLoopGetCurrent(), sharedTimer, kCFRunLoopCommonModes);
     
index 5075395..258cf3d 100644 (file)
@@ -92,16 +92,9 @@ SharedTimerQt* SharedTimerQt::inst()
     return timer;
 }
 
-void SharedTimerQt::start(double fireTime)
+void SharedTimerQt::start(double interval)
 {
-    double interval = fireTime - currentTime();
-    unsigned int intervalInMS;
-    if (interval < 0)
-        intervalInMS = 0;
-    else {
-        interval *= 1000;
-        intervalInMS = (unsigned int)interval;
-    }
+    unsigned int intervalInMS = static_cast<unsigned int>(interval * 1000);
 
     m_timer.start(intervalInMS, this);
 }
@@ -128,12 +121,12 @@ void setSharedTimerFiredFunction(void (*f)())
     SharedTimerQt::inst()->m_timerFunction = f;
 }
 
-void setSharedTimerFireTime(double fireTime)
+void setSharedTimerFireInterval(double interval)
 {
     if (!QCoreApplication::instance())
         return;
 
-    SharedTimerQt::inst()->start(fireTime);
+    SharedTimerQt::inst()->start(interval);
 }
 
 void stopSharedTimer()
index b7367aa..72e8992 100644 (file)
@@ -146,21 +146,16 @@ static void NTAPI queueTimerProc(PVOID, BOOLEAN)
         PostMessage(timerWindowHandle, timerFiredMessage, 0, 0);
 }
 
-void setSharedTimerFireTime(double fireTime)
+void setSharedTimerFireInterval(double interval)
 {
     ASSERT(sharedTimerFiredFunction);
 
-    double interval = fireTime - currentTime();
     unsigned intervalInMS;
-    if (interval < 0)
-        intervalInMS = 0;
-    else {
-        interval *= 1000;
-        if (interval > USER_TIMER_MAXIMUM)
-            intervalInMS = USER_TIMER_MAXIMUM;
-        else
-            intervalInMS = (unsigned)interval;
-    }
+    interval *= 1000;
+    if (interval > USER_TIMER_MAXIMUM)
+        intervalInMS = USER_TIMER_MAXIMUM;
+    else
+        intervalInMS = static_cast<unsigned>(interval);
 
     initializeOffScreenTimerWindow();
     bool timerSet = false;
index 829d95e..778fe09 100644 (file)
@@ -88,16 +88,16 @@ void setSharedTimerFiredFunction(void (*f)())
 #define USER_TIMER_MAXIMUM  0x7FFFFFFF
 #define USER_TIMER_MINIMUM  0x0000000A
 
-void setSharedTimerFireTime(double fireTime)
+void setSharedTimerFireInterval(double intervalSeconds)
 {
     ASSERT(sharedTimerFiredFunction);
 
-    double interval = (fireTime - currentTime()) * 1000.;
-    unsigned intervalInMS = interval < USER_TIMER_MINIMUM
+    double intervalMS = intervalSeconds * 1000.;
+    unsigned clampedIntervalMS = intervalMS < USER_TIMER_MINIMUM
         ? USER_TIMER_MINIMUM
-        : interval > USER_TIMER_MAXIMUM
+        : intervalMS > USER_TIMER_MAXIMUM
         ? USER_TIMER_MAXIMUM
-        : static_cast<unsigned>(interval);
+        : static_cast<unsigned>(intervalMS);
 
     if (timerID == TimerIdAuto) {
         KillTimer(timerWindowHandle, TimerIdAuto);
@@ -105,7 +105,7 @@ void setSharedTimerFireTime(double fireTime)
     }
 
     initializeOffScreenTimerWindow();
-    if (SetTimer(timerWindowHandle, TimerIdAuto, intervalInMS, 0))
+    if (SetTimer(timerWindowHandle, TimerIdAuto, clampedIntervalMS, 0))
         timerID = TimerIdAuto;
     else if (timerID != TimerIdManual)
         PostMessage(timerWindowHandle, WM_USER, 0, 0);
index 7206d80..eaf3bde 100644 (file)
@@ -67,12 +67,10 @@ void setSharedTimerFiredFunction(void (*f)())
     sharedTimerFiredFunction = f;
 }
 
-void setSharedTimerFireTime(double fireTime)
+void setSharedTimerFireInterval(double interval)
 {
     ASSERT(sharedTimerFiredFunction);
     
-    double interval = fireTime - currentTime();
-    
     if (!wkTimer)
         wkTimer = new WebKitTimer();
         
index 031b9f2..1adab3d 100644 (file)
@@ -39,6 +39,7 @@
 #include "WorkerRunLoop.h"
 #include "WorkerContext.h"
 #include "WorkerThread.h"
+#include <wtf/CurrentTime.h>
 
 namespace WebCore {
 
@@ -52,7 +53,7 @@ public:
 
     // SharedTimer interface.
     virtual void setFiredFunction(void (*function)()) { m_sharedTimerFunction = function; }
-    virtual void setFireTime(double fireTime) { m_nextFireTime = fireTime; }
+    virtual void setFireInterval(double interval) { m_nextFireTime = interval + monotonicallyIncreasingTime(); }
     virtual void stop() { m_nextFireTime = 0; }
 
     bool isActive() { return m_sharedTimerFunction && m_nextFireTime; }
index 51ff948..589eb66 100644 (file)
@@ -1,3 +1,17 @@
+2011-07-18  James Robinson  <jamesr@chromium.org>
+
+        Timer scheduling should be based off the monotonic clock
+        https://bugs.webkit.org/show_bug.cgi?id=64544
+
+        Reviewed by Darin Adler.
+
+        Renames setSharedTimerFireTime to setSharedTimerFireInterval to be consistent with WebCore.
+
+        * public/WebKitClient.h:
+        (WebKit::WebKitClient::setSharedTimerFireInterval):
+        * src/PlatformBridge.cpp:
+        (WebCore::PlatformBridge::setSharedTimerFireInterval):
+
 2011-07-18  Yuzhu Shen  <yzshen@chromium.com>
 
         Reviewed by James Robinson.
index 57acde8..de919e6 100644 (file)
@@ -47,6 +47,9 @@
 typedef void *HANDLE;
 #endif
 
+// FIXME: remove after rolling deps
+#define WEBKIT_USE_MONOTONIC_CLOCK_FOR_TIMER_SCHEDULING
+
 namespace WebKit {
 
 class WebApplicationCacheHost;
@@ -280,7 +283,7 @@ public:
     // Delayed work is driven by a shared timer.
     typedef void (*SharedTimerFunction)();
     virtual void setSharedTimerFiredFunction(SharedTimerFunction timerFunction) { }
-    virtual void setSharedTimerFireTime(double fireTime) { }
+    virtual void setSharedTimerFireInterval(double) { }
     virtual void stopSharedTimer() { }
 
     // Callable from a background WebKit thread.
index 812c6b9..11d9bcb 100644 (file)
@@ -665,9 +665,9 @@ void PlatformBridge::setSharedTimerFiredFunction(void (*func)())
     webKitClient()->setSharedTimerFiredFunction(func);
 }
 
-void PlatformBridge::setSharedTimerFireTime(double fireTime)
+void PlatformBridge::setSharedTimerFireInterval(double interval)
 {
-    webKitClient()->setSharedTimerFireTime(fireTime);
+    webKitClient()->setSharedTimerFireInterval(interval);
 }
 
 void PlatformBridge::stopSharedTimer()
index 98a8ebc..0ad3792 100644 (file)
@@ -1,3 +1,17 @@
+2011-07-18  James Robinson  <jamesr@chromium.org>
+
+        Timer scheduling should be based off the monotonic clock
+        https://bugs.webkit.org/show_bug.cgi?id=64544
+
+        Reviewed by Darin Adler.
+
+        Converts the WebKit2 RunLoop and CoreIPC timeouts to use monotonicallyIncreasingTime().
+
+        * Platform/CoreIPC/Connection.cpp:
+        (CoreIPC::Connection::waitForMessage):
+        (CoreIPC::Connection::waitForSyncReply):
+        * Platform/RunLoop.h:
+
 2011-07-18  Anders Carlsson  <andersca@apple.com>
 
         Make using lowercase parameter names for AppleConnect be a plug-in quirk
index 3677835..2c73264 100644 (file)
@@ -344,7 +344,7 @@ PassOwnPtr<ArgumentDecoder> Connection::waitForMessage(MessageID messageID, uint
         }
     }
     
-    double absoluteTime = currentTime() + timeout;
+    double absoluteTime = monotonicallyIncreasingTime() + timeout;
     
     std::pair<unsigned, uint64_t> messageAndDestination(std::make_pair(messageID.toInt(), destinationID));
     
@@ -435,7 +435,7 @@ PassOwnPtr<ArgumentDecoder> Connection::waitForSyncReply(uint64_t syncRequestID,
     if (timeout == NoTimeout)
         timeout = 1e10;
 
-    double absoluteTime = currentTime() + timeout;
+    double absoluteTime = monotonicallyIncreasingTime() + timeout;
 
     bool timedOut = false;
     while (!timedOut) {
index 0ded793..8ab714c 100644 (file)
@@ -58,8 +58,7 @@ public:
     void scheduleWork(PassOwnPtr<WorkItem>);
 
 #if PLATFORM(WIN)
-    // The absoluteTime is in seconds, starting on January 1, 1970. The time is assumed to use the
-    // same time zone as WTF::currentTime(). Dispatches sent (not posted) messages to the passed-in
+    // The absoluteTime is in seconds in terms of the monotonic clock. Dispatches sent (not posted) messages to the passed-in
     // set of HWNDs until the semaphore is signaled or absoluteTime is reached. Returns true if the
     // semaphore is signaled, false otherwise.
     static bool dispatchSentMessagesUntil(const Vector<HWND>& windows, CoreIPC::BinarySemaphore&, double absoluteTime);