authorgravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2018-04-19 10:01:41-05:00
committergravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2018-04-19 10:01:41-05:00
log89eade0548c3afe8531fcbccc753c5c1e22f8e89
tree5dbd450243aaf7fdfdbed44b299160a8885e0ced
parent3c9b6f8cd54b9ffa1fd4c32fd2811a7c20c04b62

Style cleanups, u64 casts, Timer.start returns error instead of unreachable on unexpected errno.


2 files changed, 35 insertions(+), 24 deletions(-)

std/os/index.zig+1-1
......@@ -1711,7 +1711,7 @@ fn testWindowsCmdLine(input_cmd_line: &const u8, expected_args: []const []const
17111711}
17121712
17131713comptime {
1714 if(builtin.is_test) {
1714 if (builtin.is_test) {
17151715 _ = @import("child_process.zig");
17161716 _ = @import("darwin_errno.zig");
17171717 _ = @import("darwin.zig");
std/os/time.zig+34-23
......@@ -12,12 +12,13 @@ pub const epoch = @import("epoch.zig");
1212
1313/// Sleep for the specified duration
1414pub fn sleep(seconds: usize, nanoseconds: usize) void {
15 switch(builtin.os) {
15 switch (builtin.os) {
1616 Os.linux, Os.macosx, Os.ios => {
1717 posixSleep(u63(seconds), u63(nanoseconds));
1818 },
1919 Os.windows => {
20 const milliseconds = seconds * ms_per_s + nanoseconds / (ns_per_s / ms_per_s);
20 const ns_per_ms = ns_per_s / ms_per_s;
21 const milliseconds = seconds * ms_per_s + nanoseconds / ns_per_ms;
2122 windows.Sleep(windows.DWORD(milliseconds));
2223 },
2324 else => @compileError("Unsupported OS"),
......@@ -57,7 +58,7 @@ pub fn timestamp() u64 {
5758}
5859
5960/// Get the posix timestamp, UTC, in milliseconds
60pub const milliTimestamp = switch(builtin.os) {
61pub const milliTimestamp = switch (builtin.os) {
6162 Os.windows => milliTimestampWindows,
6263 Os.linux => milliTimestampPosix,
6364 Os.macosx, Os.ios => milliTimestampDarwin,
......@@ -80,20 +81,21 @@ fn milliTimestampDarwin() u64 {
8081 var tv: darwin.timeval = undefined;
8182 var err = darwin.gettimeofday(&tv, null);
8283 debug.assert(err == 0);
83 const sec_ms = tv.tv_sec * ms_per_s;
84 const usec_ms = @divFloor(tv.tv_usec, (us_per_s / ms_per_s));
84 const sec_ms = u64(tv.tv_sec) * ms_per_s;
85 const usec_ms = @divFloor(u64(tv.tv_usec), us_per_s / ms_per_s);
8586 return u64(sec_ms) + u64(usec_ms);
8687}
8788
8889fn milliTimestampPosix() u64 {
8990 //From what I can tell there's no reason clock_gettime
90 // should ever fail for us with CLOCK_REALTIME
91 // should ever fail for us with CLOCK_REALTIME,
92 // seccomp aside.
9193 var ts: posix.timespec = undefined;
9294 const err = posix.clock_gettime(posix.CLOCK_REALTIME, &ts);
9395 debug.assert(err == 0);
94 const sec_ms = ts.tv_sec * ms_per_s;
95 const nsec_ms = @divFloor(ts.tv_nsec, ns_per_s / ms_per_s);
96 return u64(sec_ms) + u64(nsec_ms);
96 const sec_ms = u64(ts.tv_sec) * ms_per_s;
97 const nsec_ms = @divFloor(u64(ts.tv_nsec), ns_per_s / ms_per_s);
98 return sec_ms + nsec_ms;
9799}
98100
99101/// Divisions of a second
......@@ -122,7 +124,7 @@ pub const Timer = struct {
122124 //if we used resolution's value when performing the
123125 // performance counter calc on windows/darwin, it would
124126 // be less precise
125 frequency: switch(builtin.os) {
127 frequency: switch (builtin.os) {
126128 Os.windows => u64,
127129 Os.macosx, Os.ios => darwin.mach_timebase_info_data,
128130 else => void,
......@@ -141,7 +143,8 @@ pub const Timer = struct {
141143 //};
142144 const monotonic_clock_id = posix.CLOCK_MONOTONIC;
143145
144 //Initialize the timer structure.
146
147 /// Initialize the timer structure.
145148 //This gives us an oportunity to grab the counter frequency in windows.
146149 //On Windows: QueryPerformanceCounter will succeed on anything >= XP/2000.
147150 //On Posix: CLOCK_MONOTONIC will only fail if the monotonic counter is not
......@@ -149,32 +152,40 @@ pub const Timer = struct {
149152 // impossible here barring cosmic rays or other such occurances of
150153 // incredibly bad luck.
151154 //On Darwin: This cannot fail, as far as I am able to tell.
152 const TimerError = error{TimerUnsupported};
155 const TimerError = error{TimerUnsupported, UnexpectedErrnoValue};
153156 pub fn start() TimerError!Timer {
154157 var self: Timer = undefined;
155158
156 switch(builtin.os) {
159 switch (builtin.os) {
157160 Os.windows => {
158161 var freq: i64 = undefined;
159162 var err = windows.QueryPerformanceFrequency(&freq);
160 if(err == 0) return error.TimerUnsupported;
163 if (err == 0) return error.TimerUnsupported;
161164 self.frequency = u64(freq);
162165 self.resolution = @divFloor(ns_per_s, self.frequency);
166
163167 var start_time: i64 = undefined;
164 _ = windows.QueryPerformanceCounter(&start_time);
168 err = windows.QueryPerformanceCounter(&start_time);
169 debug.assert(err != 0);
165170 self.start_time = u64(start_time);
166171 },
167172 Os.linux => {
173 //On Linux, seccomp can do arbitrary things to our ability to call
174 // syscalls, including return any errno value it wants and
175 // inconsistently throwing errors. Since we can't account for
176 // abuses of seccomp in a reasonable way, we'll assume that if
177 // seccomp is going to block us it will at least do so consistently
168178 var ts: posix.timespec = undefined;
169179 var result = posix.clock_getres(monotonic_clock_id, &ts);
170 switch(posix.getErrno(result)) {
180 switch (posix.getErrno(result)) {
171181 0 => {},
172182 posix.EINVAL => return error.TimerUnsupported,
173 else => unreachable,
183 else => return error.UnexpectedErrnoValue,
174184 }
175 self.resolution = u64(ts.tv_sec * ns_per_s + ts.tv_nsec);
176 _ = posix.clock_gettime(monotonic_clock_id, &ts);
177 self.start_time = u64(ts.tv_sec * ns_per_s + ts.tv_nsec);
185 self.resolution = u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
186 result = posix.clock_gettime(monotonic_clock_id, &ts);
187 if (posix.getErrno(result) != 0) return error.UnexpectedErrnoValue;
188 self.start_time = u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
178189 },
179190 Os.macosx, Os.ios => {
180191 darwin.mach_timebase_info(&self.frequency);
......@@ -189,7 +200,7 @@ pub const Timer = struct {
189200 /// Reads the timer value since start or the last reset in nanoseconds
190201 pub fn read(self: &Timer) u64 {
191202 var clock = clockNative() - self.start_time;
192 return switch(builtin.os) {
203 return switch (builtin.os) {
193204 Os.windows => @divFloor(clock * ns_per_s, self.frequency),
194205 Os.linux => clock,
195206 Os.macosx, Os.ios => @divFloor(clock * self.frequency.numer, self.frequency.denom),
......@@ -212,7 +223,7 @@ pub const Timer = struct {
212223 }
213224
214225
215 const clockNative = switch(builtin.os) {
226 const clockNative = switch (builtin.os) {
216227 Os.windows => clockWindows,
217228 Os.linux => clockLinux,
218229 Os.macosx, Os.ios => clockDarwin,
......@@ -234,7 +245,7 @@ pub const Timer = struct {
234245 var ts: posix.timespec = undefined;
235246 var result = posix.clock_gettime(monotonic_clock_id, &ts);
236247 debug.assert(posix.getErrno(result) == 0);
237 return u64(ts.tv_sec * ns_per_s + ts.tv_nsec);
248 return u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
238249 }
239250};
240251