1 #include "HalideRuntime.h"
2 
3 struct mach_timebase_info {
4     uint32_t numer;
5     uint32_t denom;
6 };
7 
8 typedef struct mach_timebase_info *mach_timebase_info_t;
9 typedef struct mach_timebase_info mach_timebase_info_data_t;
10 
11 typedef int kern_return_t;
12 
13 namespace Halide {
14 namespace Runtime {
15 namespace Internal {
16 WEAK bool halide_reference_clock_inited = false;
17 WEAK uint64_t halide_reference_clock = 0;
18 WEAK mach_timebase_info_data_t halide_timebase_info;
19 }  // namespace Internal
20 }  // namespace Runtime
21 }  // namespace Halide
22 
23 extern "C" {
24 
25 extern uint64_t mach_absolute_time(void);
26 extern kern_return_t mach_timebase_info(mach_timebase_info_t info);
27 
halide_start_clock(void * user_context)28 WEAK int halide_start_clock(void *user_context) {
29     // Guard against multiple calls
30     if (!halide_reference_clock_inited) {
31         mach_timebase_info(&halide_timebase_info);
32         halide_reference_clock = mach_absolute_time();
33         halide_reference_clock_inited = true;
34     }
35 
36     return 0;
37 }
38 
halide_current_time_ns(void * user_context)39 WEAK int64_t halide_current_time_ns(void *user_context) {
40     uint64_t now = mach_absolute_time();
41     return (now - halide_reference_clock) * halide_timebase_info.numer / halide_timebase_info.denom;
42 }
43 
44 extern int usleep(int);
halide_sleep_ms(void * user_context,int ms)45 WEAK void halide_sleep_ms(void *user_context, int ms) {
46     usleep(ms * 1000);
47 }
48 }
49