mirror of
https://github.com/janet-lang/janet
synced 2025-08-27 08:02:25 +00:00
Read System Clock for Monotonic Time
This PR changes the `janet_gettime` implementation for OSX <10.12 to read the system clock for `(os/clock :monotonic)`. As far as I was able to find online this is _a_ monotonic clock, although it produces different values from `clock_gettime(CLOCK_MONOTONIC, ...)` on the same system. I can speculate that this is related to `SYSTEM_CLOCK` monotonic time being implemented with `mach_absolute_time` which is documented to _not advance during sleep_, and I suspect that `clock_gettime(CLOCK_MONOTONIC, ...)` does. **Resources**: - `clock_get_time` implementation for the `SYSTEM_CLOCK`: <e3723e1f17/osfmk/kern/clock_oldops.c (L284-L296)
> <2ff845c2e0/osfmk/arm/rtclock.c (L248-L260)
> - `mach_absolute_time` and `mach_continuous_time` definitions: <e3723e1f17/osfmk/mach/mach_time.h (L55-L68)
> - Stack overflow post for implementing `clock_gettime` on OS X before 10.12: <https://stackoverflow.com/questions/11680461/monotonic-clock-on-osx>
This commit is contained in:
parent
99abada2c2
commit
ad8a5cb6c7
@ -931,27 +931,24 @@ int janet_gettime(struct timespec *spec, enum JanetTimeSource source) {
|
||||
#include <mach/clock.h>
|
||||
#include <mach/mach.h>
|
||||
int janet_gettime(struct timespec *spec, enum JanetTimeSource source) {
|
||||
if (source == JANET_TIME_REALTIME) {
|
||||
clock_serv_t cclock;
|
||||
mach_timespec_t mts;
|
||||
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
||||
clock_get_time(cclock, &mts);
|
||||
mach_port_deallocate(mach_task_self(), cclock);
|
||||
spec->tv_sec = mts.tv_sec;
|
||||
spec->tv_nsec = mts.tv_nsec;
|
||||
} else if (source == JANET_TIME_MONOTONIC) {
|
||||
clock_serv_t cclock;
|
||||
int nsecs;
|
||||
mach_msg_type_number_t count;
|
||||
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
|
||||
clock_get_attributes(cclock, CLOCK_GET_TIME_RES, (clock_attr_t)&nsecs, &count);
|
||||
mach_port_deallocate(mach_task_self(), cclock);
|
||||
clock_getres(CLOCK_MONOTONIC, spec);
|
||||
}
|
||||
if (source == JANET_TIME_CPUTIME) {
|
||||
clock_t tmp = clock();
|
||||
spec->tv_sec = tmp / CLOCKS_PER_SEC;
|
||||
spec->tv_nsec = ((tmp - (spec->tv_sec * CLOCKS_PER_SEC)) * 1000000000) / CLOCKS_PER_SEC;
|
||||
} else {
|
||||
clock_serv_t cclock;
|
||||
mach_timespec_t mts;
|
||||
clock_id_t cid = CALENDAR_CLOCK;
|
||||
if (source == JANET_TIME_REALTIME) {
|
||||
cid = CALENDAR_CLOCK;
|
||||
} else if (source == JANET_TIME_MONOTONIC) {
|
||||
cid = SYSTEM_CLOCK;
|
||||
}
|
||||
host_get_clock_service(mach_host_self(), cid, &cclock);
|
||||
clock_get_time(cclock, &mts);
|
||||
mach_port_deallocate(mach_task_self(), cclock);
|
||||
spec->tv_sec = mts.tv_sec;
|
||||
spec->tv_nsec = mts.tv_nsec;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user