feat(time): add common time functions

This commit is contained in:
fallenoak 2022-12-28 14:28:47 -06:00 committed by GitHub
parent d1bfb1394e
commit 1da79e35bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 0 deletions

53
common/Time.cpp Normal file
View file

@ -0,0 +1,53 @@
#include "common/Time.hpp"
#if defined(WHOA_SYSTEM_WIN)
#include <windows.h>
#elif defined(WHOA_SYSTEM_MAC)
#include <mach/mach_time.h>
#include <unistd.h>
#elif defined(WHOA_SYSTEM_LINUX)
#include <chrono>
#include <unistd.h>
#endif
uint64_t OsGetAsyncTimeMs() {
#if defined(WHOA_SYSTEM_WIN)
return GetTickCount();
#elif defined(WHOA_SYSTEM_MAC)
static mach_timebase_info_data_t timebase;
if (timebase.denom == 0) {
mach_timebase_info(&timebase);
}
uint64_t ticks = mach_absolute_time();
return ticks * (timebase.numer / timebase.denom) / 1000000;
#elif defined(WHOA_SYSTEM_LINUX)
auto now = std::chrono::steady_clock::now();
uint64_t ticks = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
return ticks;
#endif
}
uint64_t OsGetAsyncTimeMsPrecise() {
#if defined(WHOA_SYSTEM_WIN)
// TODO QueryPerformanceCounter implementation
return OsGetAsyncTimeMs();
#else
return OsGetAsyncTimeMs();
#endif
}
void OsSleep(uint32_t duration) {
#if defined(WHOA_SYSTEM_WIN)
Sleep(duration);
#endif
#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX)
usleep(duration);
#endif
}

12
common/Time.hpp Normal file
View file

@ -0,0 +1,12 @@
#ifndef COMMON_TIME_HPP
#define COMMON_TIME_HPP
#include <cstdint>
uint64_t OsGetAsyncTimeMs();
uint64_t OsGetAsyncTimeMsPrecise();
void OsSleep(uint32_t duration);
#endif