ソースを参照

add millisecond resolution timer (#790)

* add millisecond resolution timer

* Don't get in a muddle with the variable names from other functions

---------

Co-authored-by: Aidan Lee <[email protected]>
Aidan Lee 2 ヶ月 前
コミット
1ffbbb4c0d
1 ファイル変更25 行追加5 行削除
  1. 25 5
      src/std/sys.c

+ 25 - 5
src/std/sys.c

@@ -202,6 +202,10 @@ HL_PRIM void hl_sys_vtune_init() {
 static double CURT = 0;
 #endif
 
+#ifdef HL_WIN
+static LARGE_INTEGER qpcFrequency;
+#endif
+
 HL_PRIM double hl_sys_time() {
 #ifdef HL_DEBUG_REPRO
 	CURT += 0.001;
@@ -210,13 +214,9 @@ HL_PRIM double hl_sys_time() {
 #ifdef HL_WIN
 	#define EPOCH_DIFF	(134774*24*60*60.0)
 	static double time_diff = 0.;
-	static double freq = 0.;
 	LARGE_INTEGER time;
 
-	if( freq == 0 ) {
-		QueryPerformanceFrequency(&time);
-		freq = (double)time.QuadPart;
-	}
+	double freq = (double)qpcFrequency.QuadPart;
 	QueryPerformanceCounter(&time);
 	if( time_diff == 0 ) {
 		FILETIME ft;
@@ -235,6 +235,22 @@ HL_PRIM double hl_sys_time() {
 #endif
 }
 
+HL_PRIM int64 hl_sys_timestamp_ms() {
+#ifdef HL_WIN
+	LARGE_INTEGER time;
+	QueryPerformanceCounter(&time);
+
+	return time.QuadPart * 1000LL / qpcFrequency.QuadPart;
+#else
+	struct timespec ts;
+	if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
+		return 0;
+	}
+
+	return ts.tv_sec * 1000 + (ts.tv_nsec / 1000000);
+#endif
+}
+
 HL_PRIM vbyte *hl_sys_get_env( vbyte *v ) {
 	return (vbyte*)getenv((pchar*)v);
 }
@@ -671,6 +687,9 @@ HL_PRIM varray *hl_sys_args() {
 static void *hl_file = NULL;
 
 HL_PRIM void hl_sys_init(void **args, int nargs, void *hlfile) {
+#ifdef HL_WIN
+	QueryPerformanceFrequency(&qpcFrequency);
+#endif
 	sys_args = (pchar**)args;
 	sys_nargs = nargs;
 	hl_file = hlfile;
@@ -718,6 +737,7 @@ DEFINE_PRIM(_BYTES, sys_locale, _NO_ARG);
 DEFINE_PRIM(_VOID, sys_print, _BYTES);
 DEFINE_PRIM(_VOID, sys_exit, _I32);
 DEFINE_PRIM(_F64, sys_time, _NO_ARG);
+DEFINE_PRIM(_I64, sys_timestamp_ms, _NO_ARG);
 DEFINE_PRIM(_BYTES, sys_get_env, _BYTES);
 DEFINE_PRIM(_BOOL, sys_put_env, _BYTES _BYTES);
 DEFINE_PRIM(_ARR, sys_env, _NO_ARG);