mirror of
https://github.com/thunderbrewhq/common.git
synced 2025-12-12 11:12: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>
|
#include <cstdint>
|
||||||
|
|
||||||
enum TimingMethod {
|
enum TimingMethod {
|
||||||
NotSet = -1,
|
Timing_NotSet = -1,
|
||||||
BestAvailable = 0,
|
Timing_BestAvailable = 0,
|
||||||
// GetTickCount (Windows), mach_absolute_time (MacOS)
|
Timing_GetTickCount = 1,
|
||||||
SystemMethod1 = 1,
|
Timing_QueryPerformanceCounter = 2
|
||||||
// QueryPerformanceCounter (Windows), Carbon Microseconds (MacOS)
|
|
||||||
SystemMethod2 = 2
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void OsTimeStartup(TimingMethod timingMethod);
|
void OsTimeStartup(TimingMethod timingMethod);
|
||||||
|
|
@ -24,4 +22,10 @@ int64_t OsGetAsyncClocksPerSecond();
|
||||||
|
|
||||||
void OsSleep(uint32_t duration);
|
void OsSleep(uint32_t duration);
|
||||||
|
|
||||||
|
#if defined(WHOA_SYSTEM_WIN)
|
||||||
|
|
||||||
|
const char* OsTimeGetTimingMethodName(TimingMethod timingMethod);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,21 @@ void OsTimeShutdown() {
|
||||||
OsTimeManagerDestroy();
|
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() {
|
uint64_t OsGetAsyncTimeMsPrecise() {
|
||||||
return s_OsTimeMgr->Snapshot();
|
return s_OsTimeMgr->Snapshot();
|
||||||
}
|
}
|
||||||
|
|
@ -39,7 +54,7 @@ uint64_t OsGetAsyncTimeMs() {
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t OsGetAsyncClocksPerSecond() {
|
int64_t OsGetAsyncClocksPerSecond() {
|
||||||
if (s_OsTimeMgr->timingMethod == SystemMethod2) {
|
if (s_OsTimeMgr->timingMethod == Timing_QueryPerformanceCounter) {
|
||||||
return s_OsTimeMgr->performanceFrequency;
|
return s_OsTimeMgr->performanceFrequency;
|
||||||
} else {
|
} else {
|
||||||
return 1000LL;
|
return 1000LL;
|
||||||
|
|
|
||||||
|
|
@ -7,19 +7,19 @@
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
OsTimeManager::OsTimeManager(TimingMethod tm) {
|
OsTimeManager::OsTimeManager(TimingMethod tm) {
|
||||||
this->timingMethod = NotSet;
|
this->timingMethod = Timing_NotSet;
|
||||||
|
|
||||||
auto method = this->Calibrate();
|
auto method = this->Calibrate();
|
||||||
|
|
||||||
if (tm != BestAvailable && method != timingMethod) {
|
if (tm != Timing_BestAvailable && method != timingMethod) {
|
||||||
if (tm == SystemMethod2 && method == SystemMethod1) {
|
if (tm == Timing_QueryPerformanceCounter && method == Timing_GetTickCount) {
|
||||||
this->timingTestError = 5;
|
this->timingTestError = 5;
|
||||||
}
|
}
|
||||||
method = tm;
|
method = tm;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->timingMethod = method;
|
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->scaleToMs = 1000.0 / static_cast<double>(freq);
|
||||||
this->timeBegin = 0;
|
this->timeBegin = 0;
|
||||||
}
|
}
|
||||||
|
|
@ -27,12 +27,12 @@ OsTimeManager::OsTimeManager(TimingMethod tm) {
|
||||||
TimingMethod OsTimeManager::Calibrate() {
|
TimingMethod OsTimeManager::Calibrate() {
|
||||||
if (!QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&this->performanceFrequency))) {
|
if (!QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&this->performanceFrequency))) {
|
||||||
this->timingTestError = 1;
|
this->timingTestError = 1;
|
||||||
return SystemMethod1;
|
return Timing_GetTickCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->performanceFrequency == 0) {
|
if (this->performanceFrequency == 0) {
|
||||||
this->timingTestError = 2;
|
this->timingTestError = 2;
|
||||||
return SystemMethod1;
|
return Timing_GetTickCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto process = GetCurrentProcess();
|
auto process = GetCurrentProcess();
|
||||||
|
|
@ -95,11 +95,11 @@ TimingMethod OsTimeManager::Calibrate() {
|
||||||
SetPriorityClass(process, priorityClass);
|
SetPriorityClass(process, priorityClass);
|
||||||
SetThreadPriority(thread, threadPriority);
|
SetThreadPriority(thread, threadPriority);
|
||||||
OsSleep(0);
|
OsSleep(0);
|
||||||
return this->timingMethod == BestAvailable ? SystemMethod2 : SystemMethod1;
|
return this->timingMethod == Timing_BestAvailable ? Timing_QueryPerformanceCounter : Timing_GetTickCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t OsTimeManager::Snapshot() {
|
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);
|
return static_cast<uint64_t>((static_cast<double>(GetTickCount()) * this->scaleToMs) + this->timeBegin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue