core: move to steady_clock where applicable

avoid issues when system clock gets desynchronized or changed
This commit is contained in:
Vaxry 2024-06-25 16:05:39 +02:00
parent 918d8340af
commit 1d70962892
10 changed files with 31 additions and 31 deletions

View file

@ -19,7 +19,7 @@
static void checkDefaultCursorWarp(SP<CMonitor> PNEWMONITOR, std::string monitorName) {
static auto PCURSORMONITOR = CConfigValue<std::string>("cursor:default_monitor");
static auto firstMonitorAdded = std::chrono::system_clock::now();
static auto firstMonitorAdded = std::chrono::steady_clock::now();
static bool cursorDefaultDone = false;
static bool firstLaunch = true;
@ -37,7 +37,7 @@ static void checkDefaultCursorWarp(SP<CMonitor> PNEWMONITOR, std::string monitor
return;
// after 10s, don't set cursor to default monitor
auto timePassedSec = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - firstMonitorAdded);
auto timePassedSec = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - firstMonitorAdded);
if (timePassedSec.count() > 10) {
cursorDefaultDone = true;
return;

View file

@ -57,11 +57,11 @@ void CBaseAnimatedVariable::registerVar() {
int CBaseAnimatedVariable::getDurationLeftMs() {
return std::max(
(int)(m_pConfig->pValues->internalSpeed * 100) - (int)std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - animationBegin).count(), 0);
(int)(m_pConfig->pValues->internalSpeed * 100) - (int)std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - animationBegin).count(), 0);
}
float CBaseAnimatedVariable::getPercent() {
const auto DURATIONPASSED = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - animationBegin).count();
const auto DURATIONPASSED = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - animationBegin).count();
return std::clamp((DURATIONPASSED / 100.f) / m_pConfig->pValues->internalSpeed, 0.f, 1.f);
}

View file

@ -155,7 +155,7 @@ class CBaseAnimatedVariable {
bool m_bIsRegistered = false;
bool m_bIsBeingAnimated = false;
std::chrono::system_clock::time_point animationBegin;
std::chrono::steady_clock::time_point animationBegin;
AVARDAMAGEPOLICY m_eDamagePolicy = AVARDAMAGE_NONE;
ANIMATEDVARTYPE m_Type;
@ -253,7 +253,7 @@ class CAnimatedVariable : public CBaseAnimatedVariable {
return *this;
m_Goal = v;
animationBegin = std::chrono::system_clock::now();
animationBegin = std::chrono::steady_clock::now();
m_Begun = m_Value;
onAnimationBegin();
@ -267,7 +267,7 @@ class CAnimatedVariable : public CBaseAnimatedVariable {
return;
m_Value = v;
animationBegin = std::chrono::system_clock::now();
animationBegin = std::chrono::steady_clock::now();
m_Begun = m_Value;
onAnimationBegin();

View file

@ -1,11 +1,11 @@
#include "Timer.hpp"
void CTimer::reset() {
m_tpLastReset = std::chrono::system_clock::now();
m_tpLastReset = std::chrono::steady_clock::now();
}
std::chrono::system_clock::duration CTimer::getDuration() {
return std::chrono::system_clock::now() - m_tpLastReset;
std::chrono::steady_clock::duration CTimer::getDuration() {
return std::chrono::steady_clock::now() - m_tpLastReset;
}
int CTimer::getMillis() {
@ -16,6 +16,6 @@ float CTimer::getSeconds() {
return std::chrono::duration_cast<std::chrono::milliseconds>(getDuration()).count() / 1000.f;
}
const std::chrono::system_clock::time_point& CTimer::chrono() const {
const std::chrono::steady_clock::time_point& CTimer::chrono() const {
return m_tpLastReset;
}

View file

@ -7,10 +7,10 @@ class CTimer {
void reset();
float getSeconds();
int getMillis();
const std::chrono::system_clock::time_point& chrono() const;
const std::chrono::steady_clock::time_point& chrono() const;
private:
std::chrono::system_clock::time_point m_tpLastReset;
std::chrono::steady_clock::time_point m_tpLastReset;
std::chrono::system_clock::duration getDuration();
std::chrono::steady_clock::duration getDuration();
};

View file

@ -552,7 +552,7 @@ void CAnimationManager::scheduleTick() {
float refreshDelayMs = std::floor(1000.f / PMOSTHZ->refreshRate);
const float SINCEPRES = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - PMOSTHZ->lastPresentationTimer.chrono()).count() / 1000.f;
const float SINCEPRES = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - PMOSTHZ->lastPresentationTimer.chrono()).count() / 1000.f;
const auto TOPRES = std::clamp(refreshDelayMs - SINCEPRES, 1.1f, 1000.f); // we can't send 0, that will disarm it

View file

@ -2,8 +2,8 @@
#include <uuid/uuid.h>
#include <algorithm>
CUUIDToken::CUUIDToken(const std::string& uuid_, std::any data_, std::chrono::system_clock::duration expires) : data(data_), uuid(uuid_) {
expiresAt = std::chrono::system_clock::now() + expires;
CUUIDToken::CUUIDToken(const std::string& uuid_, std::any data_, std::chrono::steady_clock::duration expires) : data(data_), uuid(uuid_) {
expiresAt = std::chrono::steady_clock::now() + expires;
}
std::string CUUIDToken::getUUID() {
@ -23,7 +23,7 @@ std::string CTokenManager::getRandomUUID() {
return uuid;
}
std::string CTokenManager::registerNewToken(std::any data, std::chrono::system_clock::duration expires) {
std::string CTokenManager::registerNewToken(std::any data, std::chrono::steady_clock::duration expires) {
std::string uuid = getRandomUUID();
m_mTokens[uuid] = makeShared<CUUIDToken>(uuid, data, expires);
@ -33,7 +33,7 @@ std::string CTokenManager::registerNewToken(std::any data, std::chrono::system_c
SP<CUUIDToken> CTokenManager::getToken(const std::string& uuid) {
// cleanup expired tokens
const auto NOW = std::chrono::system_clock::now();
const auto NOW = std::chrono::steady_clock::now();
std::erase_if(m_mTokens, [this, &NOW](const auto& el) { return el.second->expiresAt < NOW; });
if (!m_mTokens.contains(uuid))

View file

@ -9,7 +9,7 @@
class CUUIDToken {
public:
CUUIDToken(const std::string& uuid_, std::any data_, std::chrono::system_clock::duration expires);
CUUIDToken(const std::string& uuid_, std::any data_, std::chrono::steady_clock::duration expires);
std::string getUUID();
@ -18,14 +18,14 @@ class CUUIDToken {
private:
std::string uuid;
std::chrono::system_clock::time_point expiresAt;
std::chrono::steady_clock::time_point expiresAt;
friend class CTokenManager;
};
class CTokenManager {
public:
std::string registerNewToken(std::any data, std::chrono::system_clock::duration expires);
std::string registerNewToken(std::any data, std::chrono::steady_clock::duration expires);
std::string getRandomUUID();
SP<CUUIDToken> getToken(const std::string& uuid);

View file

@ -2,23 +2,23 @@
#include <limits>
#include "EventLoopManager.hpp"
CEventLoopTimer::CEventLoopTimer(std::optional<std::chrono::system_clock::duration> timeout, std::function<void(SP<CEventLoopTimer> self, void* data)> cb_, void* data_) :
CEventLoopTimer::CEventLoopTimer(std::optional<std::chrono::steady_clock::duration> timeout, std::function<void(SP<CEventLoopTimer> self, void* data)> cb_, void* data_) :
cb(cb_), data(data_) {
if (!timeout.has_value())
expires.reset();
else
expires = std::chrono::system_clock::now() + *timeout;
expires = std::chrono::steady_clock::now() + *timeout;
}
void CEventLoopTimer::updateTimeout(std::optional<std::chrono::system_clock::duration> timeout) {
void CEventLoopTimer::updateTimeout(std::optional<std::chrono::steady_clock::duration> timeout) {
if (!timeout.has_value()) {
expires.reset();
g_pEventLoopManager->nudgeTimers();
return;
}
expires = std::chrono::system_clock::now() + *timeout;
expires = std::chrono::steady_clock::now() + *timeout;
g_pEventLoopManager->nudgeTimers();
}
@ -26,7 +26,7 @@ void CEventLoopTimer::updateTimeout(std::optional<std::chrono::system_clock::dur
bool CEventLoopTimer::passed() {
if (!expires.has_value())
return false;
return std::chrono::system_clock::now() > *expires;
return std::chrono::steady_clock::now() > *expires;
}
void CEventLoopTimer::cancel() {
@ -47,5 +47,5 @@ float CEventLoopTimer::leftUs() {
if (!expires.has_value())
return std::numeric_limits<float>::max();
return std::chrono::duration_cast<std::chrono::microseconds>(*expires - std::chrono::system_clock::now()).count();
return std::chrono::duration_cast<std::chrono::microseconds>(*expires - std::chrono::steady_clock::now()).count();
}

View file

@ -8,11 +8,11 @@
class CEventLoopTimer {
public:
CEventLoopTimer(std::optional<std::chrono::system_clock::duration> timeout, std::function<void(SP<CEventLoopTimer> self, void* data)> cb_, void* data_);
CEventLoopTimer(std::optional<std::chrono::steady_clock::duration> timeout, std::function<void(SP<CEventLoopTimer> self, void* data)> cb_, void* data_);
// if not specified, disarms.
// if specified, arms.
void updateTimeout(std::optional<std::chrono::system_clock::duration> timeout);
void updateTimeout(std::optional<std::chrono::steady_clock::duration> timeout);
void cancel();
bool passed();
@ -26,6 +26,6 @@ class CEventLoopTimer {
private:
std::function<void(SP<CEventLoopTimer> self, void* data)> cb;
void* data = nullptr;
std::optional<std::chrono::system_clock::time_point> expires;
std::optional<std::chrono::steady_clock::time_point> expires;
bool wasCancelled = false;
};