arch/sim: fix host_settimer clock mismatch on macOS

dispatch_walltime() expects a delta in nanoseconds from now, but the
previous code subtracted CLOCK_REALTIME from a CLOCK_MONOTONIC absolute
timestamp, which have different epochs and produce a meaningless result.

Fix by subtracting the current CLOCK_MONOTONIC absolute time to get the
correct remaining duration.

Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
This commit is contained in:
Lingao Meng
2026-04-25 12:43:30 +08:00
committed by Xiang Xiao
parent dfd735045c
commit 62629d5efe
+6 -1
View File
@@ -193,7 +193,12 @@ int host_settimer(uint64_t nsec)
#ifdef __APPLE__
dispatch_time_t start;
start = dispatch_walltime(NULL, nsec - host_gettime(true));
struct timespec now;
uint64_t now_ns;
clock_gettime(CLOCK_MONOTONIC, &now);
now_ns = 1000000000ull * now.tv_sec + now.tv_nsec;
start = dispatch_walltime(NULL, nsec - now_ns);
dispatch_source_set_timer(g_timer, start, DISPATCH_TIME_FOREVER, 1000);
return 0;
#else