Skip to content

Commit a428509

Browse files
Aidan63Aidan Lee
andauthored
add millisecond resolution timer (#1234)
Co-authored-by: Aidan Lee <[email protected]>
1 parent 87e2baf commit a428509

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

include/hx/StdLibs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Array<unsigned char> __hxcpp_resource_bytes(String inName);
4343
// System access
4444
Array<String> __get_args();
4545
double __time_stamp();
46+
::cpp::Int64 __time_stamp_ms();
4647

4748
HXCPP_EXTERN_CLASS_ATTRIBUTES void __hxcpp_print_string(const String &inV);
4849
HXCPP_EXTERN_CLASS_ATTRIBUTES void __hxcpp_println_string(const String &inV);

src/hx/StdLibs.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,17 @@ int __hxcpp_irand(int inMax)
242242
return (lo | (mid<<12) | (hi<<24) ) % inMax;
243243
}
244244

245+
#ifdef HX_WINDOWS
246+
LARGE_INTEGER qpcFrequency;
247+
#endif
248+
245249
void __hxcpp_stdlibs_boot()
246250
{
251+
#ifdef HX_WINDOWS
252+
// MSDN states that QueryPerformanceFrequency will always succeed on XP and above, so I'm ignoring the result.
253+
QueryPerformanceFrequency(&qpcFrequency);
254+
#endif
255+
247256
#if defined(_MSC_VER) && !defined(HX_WINRT)
248257
HMODULE kernel32 = LoadLibraryA("kernel32");
249258
if (kernel32)
@@ -327,10 +336,7 @@ double __time_stamp()
327336
if (t0==0)
328337
{
329338
t0 = now;
330-
__int64 freq;
331-
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
332-
if (freq!=0)
333-
period = 1.0/freq;
339+
period = 1.0/qpcFrequency.QuadPart;
334340
}
335341
if (period!=0)
336342
return (now-t0)*period;
@@ -349,6 +355,26 @@ double __time_stamp()
349355
#endif
350356
}
351357

358+
::cpp::Int64 __time_stamp_ms()
359+
{
360+
#ifdef HX_WINDOWS
361+
// MSDN states that QueryPerformanceCounter will always succeed on XP and above, so I'm ignoring the result.
362+
auto now = LARGE_INTEGER{ 0 };
363+
QueryPerformanceCounter(&now);
364+
365+
return now.QuadPart * LONGLONG{ 1000 } / qpcFrequency.QuadPart;
366+
#else
367+
auto time = timespec();
368+
369+
if (clock_gettime(CLOCK_MONOTONIC, &time))
370+
{
371+
throw ::Dynamic(HX_CSTRING("Failed to get the monotonic clock time"));
372+
}
373+
374+
return time.tv_sec * 1000 + (time.tv_nsec / 1000000);
375+
#endif
376+
}
377+
352378
#if defined(HX_WINDOWS) && !defined(HX_WINRT)
353379

354380
/*

0 commit comments

Comments
 (0)