From 5336f1085c327ca06f5f549448c5cc594a4fe7b2 Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Sun, 11 Jan 2015 10:30:30 +0200 Subject: [PATCH] Fixed incorrect value that I_FPSTime() may return when OS X thread-based timer implementation is used --- src/posix/cocoa/i_timer.cpp | 42 +++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/posix/cocoa/i_timer.cpp b/src/posix/cocoa/i_timer.cpp index 319cdd2b83..b08a43139d 100644 --- a/src/posix/cocoa/i_timer.cpp +++ b/src/posix/cocoa/i_timer.cpp @@ -32,6 +32,7 @@ */ #include +#include #include #include #include @@ -43,34 +44,23 @@ #include "templates.h" -static timeval s_startTicks; - - -unsigned int I_MSTime() +namespace { - timeval now; - gettimeofday(&now, NULL); - const uint32_t ticks = - (now.tv_sec - s_startTicks.tv_sec ) * 1000 - + (now.tv_usec - s_startTicks.tv_usec) / 1000; +timeval s_gameStartTicks; +timeval s_systemBootTicks; - return ticks; -} - -unsigned int I_FPSTime() +unsigned int GetMillisecondsSince(const timeval& time) { timeval now; gettimeofday(&now, NULL); return static_cast( - (now.tv_sec) * 1000 + (now.tv_usec) / 1000); + (now.tv_sec - time.tv_sec ) * 1000 + + (now.tv_usec - time.tv_usec) / 1000); } -namespace -{ - bool s_isTicFrozen; timespec GetNextTickTime() @@ -185,6 +175,17 @@ void FreezeTimeThreaded(bool frozen) } // unnamed namespace +unsigned int I_MSTime() +{ + return GetMillisecondsSince(s_gameStartTicks); +} + +unsigned int I_FPSTime() +{ + return GetMillisecondsSince(s_systemBootTicks); +} + + fixed_t I_GetTimeFrac(uint32* ms) { const uint32_t now = I_MSTime(); @@ -205,7 +206,12 @@ void I_InitTimer() assert(!s_timerInitialized); s_timerInitialized = true; - gettimeofday(&s_startTicks, NULL); + gettimeofday(&s_gameStartTicks, NULL); + + int mib[2] = { CTL_KERN, KERN_BOOTTIME }; + size_t len = sizeof s_systemBootTicks; + + sysctl(mib, 2, &s_systemBootTicks, &len, NULL, 0); pthread_cond_init (&s_timerEvent, NULL); pthread_mutex_init(&s_timerMutex, NULL);