| 51 | } |
| 52 | |
| 53 | static int ClockGetres(clockid_t clk_id, struct timespec* res) { |
| 54 | #if defined(_darwin_) |
| 55 | static auto func = (decltype(&ClockGetres))dlsym(RTLD_SELF, "_clock_getres"); |
| 56 | |
| 57 | if (func) { |
| 58 | return func(clk_id, res); |
| 59 | } |
| 60 | |
| 61 | // https://opensource.apple.com/source/Libc/Libc-1158.1.2/gen/clock_gettime.c.auto.html |
| 62 | |
| 63 | switch (clk_id){ |
| 64 | case CLOCK_REALTIME: |
| 65 | case CLOCK_MONOTONIC: |
| 66 | case CLOCK_PROCESS_CPUTIME_ID: |
| 67 | res->tv_nsec = NSEC_PER_USEC; |
| 68 | res->tv_sec = 0; |
| 69 | |
| 70 | return 0; |
| 71 | |
| 72 | case CLOCK_MONOTONIC_RAW: |
| 73 | case CLOCK_MONOTONIC_RAW_APPROX: |
| 74 | case CLOCK_UPTIME_RAW: |
| 75 | case CLOCK_UPTIME_RAW_APPROX: |
| 76 | case CLOCK_THREAD_CPUTIME_ID: { |
| 77 | mach_timebase_info_data_t tb_info; |
| 78 | |
| 79 | if (mach_timebase_info(&tb_info)) { |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | res->tv_nsec = tb_info.numer / tb_info.denom + (tb_info.numer % tb_info.denom != 0); |
| 84 | res->tv_sec = 0; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | default: |
| 90 | errno = EINVAL; |
| 91 | return -1; |
| 92 | } |
| 93 | #else |
| 94 | return clock_getres(clk_id, res); |
| 95 | #endif |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | BEGIN_SYMS("c") |