| ... | @@ -4,6 +4,7 @@ const Os = builtin.Os; | ... | @@ -4,6 +4,7 @@ const Os = builtin.Os; |
| 4 | const debug = std.debug; | 4 | const debug = std.debug; |
| 5 | | 5 | |
| 6 | const windows = std.os.windows; | 6 | const windows = std.os.windows; |
| | 7 | const linux = std.os.linux; |
| 7 | const darwin = std.os.darwin; | 8 | const darwin = std.os.darwin; |
| 8 | const posix = std.os.posix; | 9 | const posix = std.os.posix; |
| 9 | | 10 | |
| ... | @@ -119,8 +120,8 @@ pub const s_per_week = s_per_day * 7; | ... | @@ -119,8 +120,8 @@ pub const s_per_week = s_per_day * 7; |
| 119 | pub const Timer = struct { | 120 | pub const Timer = struct { |
| 120 | | 121 | |
| 121 | //if we used resolution's value when performing the | 122 | //if we used resolution's value when performing the |
| 122 | // performance counter calc on windows, it would be | 123 | // performance counter calc on windows/darwin, it would |
| 123 | // less precise | 124 | // be less precise |
| 124 | frequency: switch(builtin.os) { | 125 | frequency: switch(builtin.os) { |
| 125 | Os.windows => u64, | 126 | Os.windows => u64, |
| 126 | Os.macosx, Os.ios => darwin.mach_timebase_info_data, | 127 | Os.macosx, Os.ios => darwin.mach_timebase_info_data, |
| ... | @@ -129,9 +130,20 @@ pub const Timer = struct { | ... | @@ -129,9 +130,20 @@ pub const Timer = struct { |
| 129 | resolution: u64, | 130 | resolution: u64, |
| 130 | start_time: u64, | 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 | //Initialize the timer structure. | 144 | //Initialize the timer structure. |
| 133 | //This gives us an oportunity to grab the counter frequency in windows. | 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 | //On Posix: CLOCK_MONOTONIC will only fail if the monotonic counter is not | 147 | //On Posix: CLOCK_MONOTONIC will only fail if the monotonic counter is not |
| 136 | // supported, or if the timespec pointer is out of bounds, which should be | 148 | // supported, or if the timespec pointer is out of bounds, which should be |
| 137 | // impossible here barring cosmic rays or other such occurances of | 149 | // impossible here barring cosmic rays or other such occurances of |
| ... | @@ -154,14 +166,14 @@ pub const Timer = struct { | ... | @@ -154,14 +166,14 @@ pub const Timer = struct { |
| 154 | }, | 166 | }, |
| 155 | Os.linux => { | 167 | Os.linux => { |
| 156 | var ts: posix.timespec = undefined; | 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 | switch(posix.getErrno(result)) { | 170 | switch(posix.getErrno(result)) { |
| 159 | 0 => {}, | 171 | 0 => {}, |
| 160 | posix.EINVAL => return error.TimerUnsupported, | 172 | posix.EINVAL => return error.TimerUnsupported, |
| 161 | else => unreachable, | 173 | else => unreachable, |
| 162 | } | 174 | } |
| 163 | self.resolution = u64(ts.tv_sec * ns_per_s + ts.tv_nsec); | 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 | self.start_time = u64(ts.tv_sec * ns_per_s + ts.tv_nsec); | 177 | self.start_time = u64(ts.tv_sec * ns_per_s + ts.tv_nsec); |
| 166 | }, | 178 | }, |
| 167 | Os.macosx, Os.ios => { | 179 | Os.macosx, Os.ios => { |
| ... | @@ -220,7 +232,7 @@ pub const Timer = struct { | ... | @@ -220,7 +232,7 @@ pub const Timer = struct { |
| 220 | | 232 | |
| 221 | fn clockLinux() u64 { | 233 | fn clockLinux() u64 { |
| 222 | var ts: posix.timespec = undefined; | 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 | debug.assert(posix.getErrno(result) == 0); | 236 | debug.assert(posix.getErrno(result) == 0); |
| 225 | return u64(ts.tv_sec * ns_per_s + ts.tv_nsec); | 237 | return u64(ts.tv_sec * ns_per_s + ts.tv_nsec); |
| 226 | } | 238 | } |