authorgravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2018-04-18 19:48:19-05:00
committergravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2018-04-18 19:48:19-05:00
logfdebe38fa3763fe60aab191ed3fb44bfdb9a3b6f
treede25b72295b00d3544e3aa592be0c356628774a2
parent5c83d271a31508b33276310c93d4552f78ce459e

Added notes regarding CLOCK_MONOTONIC_RAW and made it easy to change our mind in the future.

Updated std.os imported tests' block with lazy declaration workaround and added time.zig. Corrected some incorrect comments.

2 files changed, 33 insertions(+), 18 deletions(-)

std/os/index.zig+15-12
......@@ -1710,18 +1710,21 @@ fn testWindowsCmdLine(input_cmd_line: &const u8, expected_args: []const []const
17101710 assert(it.next(debug.global_allocator) == null);
17111711}
17121712
1713test "std.os" {
1714 _ = @import("child_process.zig");
1715 _ = @import("darwin_errno.zig");
1716 _ = @import("darwin.zig");
1717 _ = @import("get_user_id.zig");
1718 _ = @import("linux/errno.zig");
1719 //_ = @import("linux_i386.zig");
1720 _ = @import("linux/x86_64.zig");
1721 _ = @import("linux/index.zig");
1722 _ = @import("path.zig");
1723 _ = @import("windows/index.zig");
1724 _ = @import("test.zig");
1713comptime {
1714 if(builtin.is_test) {
1715 _ = @import("child_process.zig");
1716 _ = @import("darwin_errno.zig");
1717 _ = @import("darwin.zig");
1718 _ = @import("get_user_id.zig");
1719 _ = @import("linux/errno.zig");
1720 //_ = @import("linux_i386.zig");
1721 _ = @import("linux/x86_64.zig");
1722 _ = @import("linux/index.zig");
1723 _ = @import("path.zig");
1724 _ = @import("time.zig");
1725 _ = @import("windows/index.zig");
1726 _ = @import("test.zig");
1727 }
17251728}
17261729
17271730
std/os/time.zig+18-6
......@@ -4,6 +4,7 @@ const Os = builtin.Os;
44const debug = std.debug;
55
66const windows = std.os.windows;
7const linux = std.os.linux;
78const darwin = std.os.darwin;
89const posix = std.os.posix;
910
......@@ -119,8 +120,8 @@ pub const s_per_week = s_per_day * 7;
119120pub const Timer = struct {
120121
121122 //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
124125 frequency: switch(builtin.os) {
125126 Os.windows => u64,
126127 Os.macosx, Os.ios => darwin.mach_timebase_info_data,
......@@ -129,9 +130,20 @@ pub const Timer = struct {
129130 resolution: u64,
130131 start_time: u64,
131132
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
132144 //Initialize the timer structure.
133145 //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.
135147 //On Posix: CLOCK_MONOTONIC will only fail if the monotonic counter is not
136148 // supported, or if the timespec pointer is out of bounds, which should be
137149 // impossible here barring cosmic rays or other such occurances of
......@@ -154,14 +166,14 @@ pub const Timer = struct {
154166 },
155167 Os.linux => {
156168 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);
158170 switch(posix.getErrno(result)) {
159171 0 => {},
160172 posix.EINVAL => return error.TimerUnsupported,
161173 else => unreachable,
162174 }
163175 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);
165177 self.start_time = u64(ts.tv_sec * ns_per_s + ts.tv_nsec);
166178 },
167179 Os.macosx, Os.ios => {
......@@ -220,7 +232,7 @@ pub const Timer = struct {
220232
221233 fn clockLinux() u64 {
222234 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);
224236 debug.assert(posix.getErrno(result) == 0);
225237 return u64(ts.tv_sec * ns_per_s + ts.tv_nsec);
226238 }