mirror of
https://github.com/thunderbrewhq/common.git
synced 2025-12-12 03:02:29 +00:00
feat(time): change TimingMethod enum names, add OsTimeGetTimingMethodName for Windows
This commit is contained in:
parent
52330085e1
commit
b3d83388ef
3 changed files with 35 additions and 16 deletions
|
|
@ -4,12 +4,10 @@
|
|||
#include <cstdint>
|
||||
|
||||
enum TimingMethod {
|
||||
NotSet = -1,
|
||||
BestAvailable = 0,
|
||||
// GetTickCount (Windows), mach_absolute_time (MacOS)
|
||||
SystemMethod1 = 1,
|
||||
// QueryPerformanceCounter (Windows), Carbon Microseconds (MacOS)
|
||||
SystemMethod2 = 2
|
||||
Timing_NotSet = -1,
|
||||
Timing_BestAvailable = 0,
|
||||
Timing_GetTickCount = 1,
|
||||
Timing_QueryPerformanceCounter = 2
|
||||
};
|
||||
|
||||
void OsTimeStartup(TimingMethod timingMethod);
|
||||
|
|
@ -24,4 +22,10 @@ int64_t OsGetAsyncClocksPerSecond();
|
|||
|
||||
void OsSleep(uint32_t duration);
|
||||
|
||||
#if defined(WHOA_SYSTEM_WIN)
|
||||
|
||||
const char* OsTimeGetTimingMethodName(TimingMethod timingMethod);
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ static OsTimeManager* s_OsTimeMgr;
|
|||
void OsTimeManagerCreate(TimingMethod timingMethod) {
|
||||
auto m = SMemAlloc(sizeof(OsTimeManager), __FILE__, __LINE__, 0x8);
|
||||
if (m) {
|
||||
s_OsTimeMgr = new (m) OsTimeManager(timingMethod);
|
||||
s_OsTimeMgr = new(m) OsTimeManager(timingMethod);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -30,6 +30,21 @@ void OsTimeShutdown() {
|
|||
OsTimeManagerDestroy();
|
||||
}
|
||||
|
||||
const char* OsTimeGetTimingMethodName(TimingMethod timingMethod) {
|
||||
switch (timingMethod) {
|
||||
case Timing_BestAvailable:
|
||||
return "[Best Available]";
|
||||
case Timing_GetTickCount:
|
||||
return "GetTickCount";
|
||||
case Timing_QueryPerformanceCounter:
|
||||
return "QueryPerformanceCounter";
|
||||
case Timing_NotSet:
|
||||
return "[Not Set]";
|
||||
default:
|
||||
return "[Unknown]";
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t OsGetAsyncTimeMsPrecise() {
|
||||
return s_OsTimeMgr->Snapshot();
|
||||
}
|
||||
|
|
@ -39,7 +54,7 @@ uint64_t OsGetAsyncTimeMs() {
|
|||
}
|
||||
|
||||
int64_t OsGetAsyncClocksPerSecond() {
|
||||
if (s_OsTimeMgr->timingMethod == SystemMethod2) {
|
||||
if (s_OsTimeMgr->timingMethod == Timing_QueryPerformanceCounter) {
|
||||
return s_OsTimeMgr->performanceFrequency;
|
||||
} else {
|
||||
return 1000LL;
|
||||
|
|
|
|||
|
|
@ -7,19 +7,19 @@
|
|||
#include <cmath>
|
||||
|
||||
OsTimeManager::OsTimeManager(TimingMethod tm) {
|
||||
this->timingMethod = NotSet;
|
||||
this->timingMethod = Timing_NotSet;
|
||||
|
||||
auto method = this->Calibrate();
|
||||
|
||||
if (tm != BestAvailable && method != timingMethod) {
|
||||
if (tm == SystemMethod2 && method == SystemMethod1) {
|
||||
if (tm != Timing_BestAvailable && method != timingMethod) {
|
||||
if (tm == Timing_QueryPerformanceCounter && method == Timing_GetTickCount) {
|
||||
this->timingTestError = 5;
|
||||
}
|
||||
method = tm;
|
||||
}
|
||||
|
||||
this->timingMethod = method;
|
||||
auto freq = method == SystemMethod2 ? this->performanceFrequency : static_cast<int64_t>(1000LL);
|
||||
auto freq = method == Timing_QueryPerformanceCounter ? this->performanceFrequency : static_cast<int64_t>(1000LL);
|
||||
this->scaleToMs = 1000.0 / static_cast<double>(freq);
|
||||
this->timeBegin = 0;
|
||||
}
|
||||
|
|
@ -27,12 +27,12 @@ OsTimeManager::OsTimeManager(TimingMethod tm) {
|
|||
TimingMethod OsTimeManager::Calibrate() {
|
||||
if (!QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&this->performanceFrequency))) {
|
||||
this->timingTestError = 1;
|
||||
return SystemMethod1;
|
||||
return Timing_GetTickCount;
|
||||
}
|
||||
|
||||
if (this->performanceFrequency == 0) {
|
||||
this->timingTestError = 2;
|
||||
return SystemMethod1;
|
||||
return Timing_GetTickCount;
|
||||
}
|
||||
|
||||
auto process = GetCurrentProcess();
|
||||
|
|
@ -95,11 +95,11 @@ TimingMethod OsTimeManager::Calibrate() {
|
|||
SetPriorityClass(process, priorityClass);
|
||||
SetThreadPriority(thread, threadPriority);
|
||||
OsSleep(0);
|
||||
return this->timingMethod == BestAvailable ? SystemMethod2 : SystemMethod1;
|
||||
return this->timingMethod == Timing_BestAvailable ? Timing_QueryPerformanceCounter : Timing_GetTickCount;
|
||||
}
|
||||
|
||||
uint64_t OsTimeManager::Snapshot() {
|
||||
if (this->timingMethod != SystemMethod2) {
|
||||
if (this->timingMethod != Timing_QueryPerformanceCounter) {
|
||||
return static_cast<uint64_t>((static_cast<double>(GetTickCount()) * this->scaleToMs) + this->timeBegin);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue