Skip to content

Commit 697fdcf

Browse files
authored
Merge pull request #1627 from nlordell/fix/system-monotonic-clock
Read System Clock for Monotonic Time
2 parents 99abada + ad8a5cb commit 697fdcf

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/core/util.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -931,27 +931,24 @@ int janet_gettime(struct timespec *spec, enum JanetTimeSource source) {
931931
#include <mach/clock.h>
932932
#include <mach/mach.h>
933933
int janet_gettime(struct timespec *spec, enum JanetTimeSource source) {
934-
if (source == JANET_TIME_REALTIME) {
934+
if (source == JANET_TIME_CPUTIME) {
935+
clock_t tmp = clock();
936+
spec->tv_sec = tmp / CLOCKS_PER_SEC;
937+
spec->tv_nsec = ((tmp - (spec->tv_sec * CLOCKS_PER_SEC)) * 1000000000) / CLOCKS_PER_SEC;
938+
} else {
935939
clock_serv_t cclock;
936940
mach_timespec_t mts;
937-
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
941+
clock_id_t cid = CALENDAR_CLOCK;
942+
if (source == JANET_TIME_REALTIME) {
943+
cid = CALENDAR_CLOCK;
944+
} else if (source == JANET_TIME_MONOTONIC) {
945+
cid = SYSTEM_CLOCK;
946+
}
947+
host_get_clock_service(mach_host_self(), cid, &cclock);
938948
clock_get_time(cclock, &mts);
939949
mach_port_deallocate(mach_task_self(), cclock);
940950
spec->tv_sec = mts.tv_sec;
941951
spec->tv_nsec = mts.tv_nsec;
942-
} else if (source == JANET_TIME_MONOTONIC) {
943-
clock_serv_t cclock;
944-
int nsecs;
945-
mach_msg_type_number_t count;
946-
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
947-
clock_get_attributes(cclock, CLOCK_GET_TIME_RES, (clock_attr_t)&nsecs, &count);
948-
mach_port_deallocate(mach_task_self(), cclock);
949-
clock_getres(CLOCK_MONOTONIC, spec);
950-
}
951-
if (source == JANET_TIME_CPUTIME) {
952-
clock_t tmp = clock();
953-
spec->tv_sec = tmp / CLOCKS_PER_SEC;
954-
spec->tv_nsec = ((tmp - (spec->tv_sec * CLOCKS_PER_SEC)) * 1000000000) / CLOCKS_PER_SEC;
955952
}
956953
return 0;
957954
}

0 commit comments

Comments
 (0)