| ... | ... | @@ -4,6 +4,7 @@ const Os = builtin.Os; |
| 4 | 4 | const debug = std.debug; |
| 5 | 5 | |
| 6 | 6 | const windows = std.os.windows; |
| 7 | const linux = std.os.linux; |
| 7 | 8 | const darwin = std.os.darwin; |
| 8 | 9 | const posix = std.os.posix; |
| 9 | 10 | |
| ... | ... | @@ -119,8 +120,8 @@ pub const s_per_week = s_per_day * 7; |
| 119 | 120 | pub const Timer = struct { |
| 120 | 121 | |
| 121 | 122 | //if we used resolution's value when performing the |
| 122 | | // performance counter calc on windows, it would be |
| 123 | | // less precise |
| 123 | // performance counter calc on windows/darwin, it would |
| 124 | // be less precise |
| 124 | 125 | frequency: switch(builtin.os) { |
| 125 | 126 | Os.windows => u64, |
| 126 | 127 | Os.macosx, Os.ios => darwin.mach_timebase_info_data, |
| ... | ... | @@ -129,9 +130,20 @@ pub const Timer = struct { |
| 129 | 130 | resolution: u64, |
| 130 | 131 | start_time: u64, |
| 131 | 132 | |
| 133 | |
| 134 | //At some point we may change our minds on RAW, but for now we're |
| 135 | // sticking with posix standard MONOTONIC. For more information, see: |
| 136 | // https://github.com/zig-lang/zig/pull/933 |
| 137 | // |
| 138 | //const monotonic_clock_id = switch(builtin.os) { |
| 139 | // Os.linux => linux.CLOCK_MONOTONIC_RAW, |
| 140 | // else => posix.CLOCK_MONOTONIC, |
| 141 | //}; |
| 142 | const monotonic_clock_id = posix.CLOCK_MONOTONIC; |
| 143 | |
| 132 | 144 | //Initialize the timer structure. |
| 133 | 145 | //This gives us an oportunity to grab the counter frequency in windows. |
| 134 | | //On Windows: QueryPerformanceCounter will succeed on anything > XP. |
| 146 | //On Windows: QueryPerformanceCounter will succeed on anything >= XP/2000. |
| 135 | 147 | //On Posix: CLOCK_MONOTONIC will only fail if the monotonic counter is not |
| 136 | 148 | // supported, or if the timespec pointer is out of bounds, which should be |
| 137 | 149 | // impossible here barring cosmic rays or other such occurances of |
| ... | ... | @@ -154,14 +166,14 @@ pub const Timer = struct { |
| 154 | 166 | }, |
| 155 | 167 | Os.linux => { |
| 156 | 168 | var ts: posix.timespec = undefined; |
| 157 | | var result = posix.clock_getres(posix.CLOCK_MONOTONIC, &ts); |
| 169 | var result = posix.clock_getres(monotonic_clock_id, &ts); |
| 158 | 170 | switch(posix.getErrno(result)) { |
| 159 | 171 | 0 => {}, |
| 160 | 172 | posix.EINVAL => return error.TimerUnsupported, |
| 161 | 173 | else => unreachable, |
| 162 | 174 | } |
| 163 | 175 | self.resolution = u64(ts.tv_sec * ns_per_s + ts.tv_nsec); |
| 164 | | _ = posix.clock_gettime(posix.CLOCK_MONOTONIC, &ts); |
| 176 | _ = posix.clock_gettime(monotonic_clock_id, &ts); |
| 165 | 177 | self.start_time = u64(ts.tv_sec * ns_per_s + ts.tv_nsec); |
| 166 | 178 | }, |
| 167 | 179 | Os.macosx, Os.ios => { |
| ... | ... | @@ -220,7 +232,7 @@ pub const Timer = struct { |
| 220 | 232 | |
| 221 | 233 | fn clockLinux() u64 { |
| 222 | 234 | var ts: posix.timespec = undefined; |
| 223 | | var result = posix.clock_gettime(posix.CLOCK_MONOTONIC, &ts); |
| 235 | var result = posix.clock_gettime(monotonic_clock_id, &ts); |
| 224 | 236 | debug.assert(posix.getErrno(result) == 0); |
| 225 | 237 | return u64(ts.tv_sec * ns_per_s + ts.tv_nsec); |
| 226 | 238 | } |