2020-04-24 07:33:25 +02:00
|
|
|
module time
|
|
|
|
|
|
|
|
#include <mach/mach_time.h>
|
|
|
|
|
|
|
|
const (
|
|
|
|
// start_time is needed on Darwin and Windows because of potential overflows
|
|
|
|
start_time = C.mach_absolute_time()
|
|
|
|
time_base = init_time_base()
|
|
|
|
)
|
|
|
|
|
|
|
|
[typedef]
|
|
|
|
struct C.mach_timebase_info_data_t {
|
2020-04-26 19:36:38 +02:00
|
|
|
numer u32
|
|
|
|
denom u32
|
2020-04-24 07:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn C.mach_absolute_time() u64
|
|
|
|
fn C.mach_timebase_info(&C.mach_timebase_info_data_t)
|
2020-04-26 19:36:38 +02:00
|
|
|
fn C.clock_gettime_nsec_np(int) u64
|
2020-04-24 07:33:25 +02:00
|
|
|
|
|
|
|
struct InternalTimeBase {
|
2020-04-26 19:36:38 +02:00
|
|
|
numer u32 = 1
|
|
|
|
denom u32 = 1
|
2020-04-24 07:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn init_time_base() InternalTimeBase {
|
2020-04-26 19:36:38 +02:00
|
|
|
tb := C.mach_timebase_info_data_t{}
|
2020-04-24 07:33:25 +02:00
|
|
|
C.mach_timebase_info(&tb)
|
|
|
|
return InternalTimeBase{numer:tb.numer, denom:tb.denom}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sys_mono_now_darwin() u64 {
|
|
|
|
tm := C.mach_absolute_time()
|
2020-04-26 19:36:38 +02:00
|
|
|
if time_base.denom == 0 {
|
|
|
|
C.mach_timebase_info(&time_base)
|
|
|
|
}
|
|
|
|
return (tm - start_time) * time_base.numer / time_base.denom
|
2020-04-24 07:33:25 +02:00
|
|
|
}
|
2020-05-05 16:19:25 +02:00
|
|
|
|
|
|
|
// NB: vpc_now_darwin is used by `v -profile` .
|
|
|
|
// It should NOT call *any other v function*, just C functions and casts.
|
|
|
|
[inline]
|
|
|
|
fn vpc_now_darwin() u64 {
|
|
|
|
tm := C.mach_absolute_time()
|
|
|
|
if time_base.denom == 0 {
|
|
|
|
C.mach_timebase_info(&time_base)
|
|
|
|
}
|
|
|
|
return (tm - start_time) * time_base.numer / time_base.denom
|
|
|
|
}
|