Fixed incorrect value that I_FPSTime() may return when OS X thread-based timer implementation is used

This commit is contained in:
alexey.lysiuk 2015-01-11 10:30:30 +02:00
parent 67b68e6b48
commit 5336f1085c

View file

@ -32,6 +32,7 @@
*/ */
#include <assert.h> #include <assert.h>
#include <sys/sysctl.h>
#include <sys/time.h> #include <sys/time.h>
#include <pthread.h> #include <pthread.h>
#include <libkern/OSAtomic.h> #include <libkern/OSAtomic.h>
@ -43,34 +44,23 @@
#include "templates.h" #include "templates.h"
static timeval s_startTicks; namespace
unsigned int I_MSTime()
{ {
timeval now;
gettimeofday(&now, NULL);
const uint32_t ticks = timeval s_gameStartTicks;
(now.tv_sec - s_startTicks.tv_sec ) * 1000 timeval s_systemBootTicks;
+ (now.tv_usec - s_startTicks.tv_usec) / 1000;
return ticks; unsigned int GetMillisecondsSince(const timeval& time)
}
unsigned int I_FPSTime()
{ {
timeval now; timeval now;
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
return static_cast<unsigned int>( return static_cast<unsigned int>(
(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; bool s_isTicFrozen;
timespec GetNextTickTime() timespec GetNextTickTime()
@ -185,6 +175,17 @@ void FreezeTimeThreaded(bool frozen)
} // unnamed namespace } // unnamed namespace
unsigned int I_MSTime()
{
return GetMillisecondsSince(s_gameStartTicks);
}
unsigned int I_FPSTime()
{
return GetMillisecondsSince(s_systemBootTicks);
}
fixed_t I_GetTimeFrac(uint32* ms) fixed_t I_GetTimeFrac(uint32* ms)
{ {
const uint32_t now = I_MSTime(); const uint32_t now = I_MSTime();
@ -205,7 +206,12 @@ void I_InitTimer()
assert(!s_timerInitialized); assert(!s_timerInitialized);
s_timerInitialized = true; 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_cond_init (&s_timerEvent, NULL);
pthread_mutex_init(&s_timerMutex, NULL); pthread_mutex_init(&s_timerMutex, NULL);