RageTimer bug fix

Fast data types are good for audio processing, but not so much for timekeeping. This PR should resolve a number of smaller issues.
This commit is contained in:
sukibaby
2024-12-04 09:41:26 -08:00
committed by teejusb
parent ffc576a210
commit 7b45a5f599
5 changed files with 22 additions and 26 deletions
+4 -4
View File
@@ -682,12 +682,12 @@ void LockMutex::Unlock()
mutex.Unlock();
constexpr uint_fast64_t THRESHOLD_USEC = 15000;
constexpr uint64_t THRESHOLD_USEC = 15000;
if (file && locked_at != FAST_ULL_MAX)
if (file && locked_at != UINT64_MAX)
{
const uint_fast64_t current_usecs = RageTimer::GetTimeSinceStartMicroseconds();
const uint_fast64_t dur_usecs = current_usecs - locked_at;
const uint64_t current_usecs = RageTimer::GetTimeSinceStartMicroseconds();
const uint64_t dur_usecs = current_usecs - locked_at;
if (dur_usecs > THRESHOLD_USEC)
LOG->Trace("Lock at %s:%i took %llu microseconds", file, line, dur_usecs);
+2 -4
View File
@@ -4,8 +4,6 @@
#include <cstdint>
#include <limits>
static constexpr uint_fast64_t FAST_ULL_MAX = std::numeric_limits<uint_fast64_t>::max();
struct ThreadSlot;
class RageTimer;
/** @brief Thread, mutex, semaphore, and event classes. */
@@ -131,12 +129,12 @@ class LockMutex
const char *file;
int line;
uint_fast64_t locked_at;
uint64_t locked_at;
bool locked;
public:
LockMutex(RageMutex &mut, const char *file, int line);
LockMutex(RageMutex &mut): mutex(mut), file(nullptr), line(-1), locked_at(FAST_ULL_MAX), locked(true) { mutex.Lock(); }
LockMutex(RageMutex &mut): mutex(mut), file(nullptr), line(-1), locked_at(UINT64_MAX), locked(true) { mutex.Lock(); }
~LockMutex();
LockMutex(LockMutex &cpy): mutex(cpy.mutex), file(nullptr), line(-1), locked_at(cpy.locked_at), locked(true) { mutex.Lock(); }
+11 -13
View File
@@ -30,16 +30,14 @@
#include <cmath>
#include <cstdint>
const uint64_t ONE_SECOND_IN_MICROSECONDS_ULL = 1000000ULL;
const int64_t ONE_SECOND_IN_MICROSECONDS_LL = 1000000LL;
const uint_fast64_t ONE_SECOND_IN_MICROSECONDS_FAST_ULL = 1000000ULL;
const double ONE_SECOND_IN_MICROSECONDS_DBL = 1000000.0;
// Intialize important variables and definitions
constexpr uint64_t ONE_SECOND_IN_MICROSECONDS_ULL = 1000000ULL;
constexpr int64_t ONE_SECOND_IN_MICROSECONDS_LL = 1000000LL;
constexpr double ONE_SECOND_IN_MICROSECONDS_DBL = 1000000.0;
const RageTimer RageZeroTimer(0,0);
static const uint64_t g_iStartTime = ArchHooks::GetSystemTimeInMicroseconds();
static uint_fast64_t g_iStartTimeFast64 = g_iStartTime;
static uint64_t GetTime()
static inline uint64_t GetTime() noexcept
{
return ArchHooks::GetSystemTimeInMicroseconds();
}
@@ -54,19 +52,19 @@ static uint64_t GetTime()
* and do thorough testing if you change anything here. -sukibaby */
double RageTimer::GetTimeSinceStart()
{
constexpr double USEC_TO_SEC = 1.0 / 1000000.0;
return static_cast<double>(RageTimer::GetTimeSinceStartMicroseconds()) * USEC_TO_SEC;
const uint64_t usecs = (GetTime() - g_iStartTime);
return static_cast<double>(usecs / ONE_SECOND_IN_MICROSECONDS_DBL);
}
int RageTimer::GetTimeSinceStartSeconds()
{
uint_fast64_t usec = RageTimer::GetTimeSinceStartMicroseconds();
return static_cast<int>(usec / ONE_SECOND_IN_MICROSECONDS_FAST_ULL);
const uint64_t usecs = (GetTime() - g_iStartTime);
return static_cast<int>(usecs / ONE_SECOND_IN_MICROSECONDS_ULL);
}
uint_fast64_t RageTimer::GetTimeSinceStartMicroseconds()
uint64_t RageTimer::GetTimeSinceStartMicroseconds()
{
return GetTime() - g_iStartTimeFast64;
return (GetTime() - g_iStartTime);
}
void RageTimer::Touch()
+3 -3
View File
@@ -24,9 +24,9 @@ public:
float PeekDeltaTime() const { return Ago(); }
static double GetTimeSinceStart(); // seconds since the program was started
static float GetTimeSinceStartFast() { return GetTimeSinceStart(); }
static int GetTimeSinceStartSeconds();
static uint_fast64_t GetTimeSinceStartMicroseconds();
static double GetTimeSinceStartFast() { return GetTimeSinceStart(); }
static int GetTimeSinceStartSeconds(); // This is used where GetTimeSinceStart would be cast to an int without rounding.
static uint64_t GetTimeSinceStartMicroseconds();
/* Get a timer representing half of the time ago as this one. */
RageTimer Half() const;
@@ -301,8 +301,8 @@ void RageSoundDriver::Update()
// LOG->Trace("set (#%i) %p from STOPPING to HALTING", i, m_Sounds[i].m_pSound);
}
constexpr uint_fast64_t iUsecs = 1000000;
static uint_fast64_t fNextUsecs = 0;
constexpr uint64_t iUsecs = 1000000;
static uint64_t fNextUsecs = 0;
if (RageTimer::GetTimeSinceStartMicroseconds() >= fNextUsecs)
{
/* Lockless: only Mix() can write to underruns. */