~ chicken-core (chicken-5) 982090150a74fdfc344bb60903021063923a38bf


commit 982090150a74fdfc344bb60903021063923a38bf
Author:     felix <felix@call-with-current-continuation.org>
AuthorDate: Sun Jul 6 15:37:06 2025 +0200
Commit:     felix <felix@call-with-current-continuation.org>
CommitDate: Sun Jul 6 15:37:06 2025 +0200

    use clock_gettime/QueryPerrformanceCounter for jiffies

diff --git a/chicken.h b/chicken.h
index 6ab9b8df..6bf400fc 100644
--- a/chicken.h
+++ b/chicken.h
@@ -2205,6 +2205,8 @@ C_fctexport C_word C_i_live_finalizer_count(void) C_regparm;
 C_fctexport C_word C_i_profilingp(void) C_regparm;
 C_fctexport C_word C_i_tty_forcedp(void) C_regparm;
 C_fctexport C_word C_i_setenv(C_word var, C_word val) C_regparm;
+C_fctexport C_long C_current_jiffy(void) C_regparm;
+C_fctexport C_long C_jiffies_per_second(void) C_regparm;
 
 C_fctexport C_word C_a_i_cpu_time(C_word **a, int c, C_word buf) C_regparm;
 C_fctexport C_word C_a_i_exact_to_inexact(C_word **a, int c, C_word n) C_regparm;
diff --git a/r7lib.scm b/r7lib.scm
index aac1c576..a34ea280 100644
--- a/r7lib.scm
+++ b/r7lib.scm
@@ -174,21 +174,21 @@
 )
 
 (module scheme.time (current-second
-		     current-jiffy
-		     jiffies-per-second)
+                     current-jiffy
+                     jiffies-per-second)
   (import (only chicken.base define-constant)
           (chicken foreign)
-	  (only chicken.time current-seconds)
-	  (only scheme + define inexact->exact))
+          (only chicken.time current-seconds)
+          (only scheme + define inexact->exact))
 
   ;; As of 2012-06-30.
   (define-constant tai-offset 35.)
 
   (define (current-second) (+ (current-seconds) tai-offset))
 
-  (define current-jiffy (foreign-lambda integer "clock"))
+  (define current-jiffy (foreign-lambda long "C_current_jiffy"))
 
-  (define (jiffies-per-second) (foreign-value "CLOCKS_PER_SEC" integer))
+  (define jiffies-per-second (foreign-lambda long "C_jiffies_per_second"))
 
 )
 
diff --git a/runtime.c b/runtime.c
index d1d08ee8..00cd6677 100644
--- a/runtime.c
+++ b/runtime.c
@@ -13692,3 +13692,25 @@ C_char *C_getenventry(int i)
 	return environ[ i ] == NULL ? NULL : C_strdup(environ[ i ]);
 #endif
 }
+
+C_long C_current_jiffy(void) {
+#if defined(_WIN32) && !defined(__CYGWIN__)
+	LARGE_INTEGER ticks;
+	QueryPerformanceCounter(&ticks);
+	return ticks.QuadPart;
+#else
+	struct timespec tm;
+	clock_gettime(CLOCK_MONOTONIC, &tm);
+	return tm.tv_nsec / 1000 + tm.tv_sec * 1000000;
+#endif
+}
+
+C_long C_jiffies_per_second(void) {
+#if defined(_WIN32) && !defined(__CYGWIN__)
+	LARGE_INTEGER ticks;
+	QueryPerformanceFrequency(&ticks);
+	return ticks.QuadPart;
+#else
+	return 1000000;
+#endif
+}
Trap