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...@@ -1710,18 +1710,21 @@ fn testWindowsCmdLine(input_cmd_line: &const u8, expected_args: []const []const
1710 assert(it.next(debug.global_allocator) == null);1710 assert(it.next(debug.global_allocator) == null);
1711}1711}
17121712
1713test "std.os" {1713comptime {
1714 _ = @import("child_process.zig");1714 if(builtin.is_test) {
1715 _ = @import("darwin_errno.zig");1715 _ = @import("child_process.zig");
1716 _ = @import("darwin.zig");1716 _ = @import("darwin_errno.zig");
1717 _ = @import("get_user_id.zig");1717 _ = @import("darwin.zig");
1718 _ = @import("linux/errno.zig");1718 _ = @import("get_user_id.zig");
1719 //_ = @import("linux_i386.zig");1719 _ = @import("linux/errno.zig");
1720 _ = @import("linux/x86_64.zig");1720 //_ = @import("linux_i386.zig");
1721 _ = @import("linux/index.zig");1721 _ = @import("linux/x86_64.zig");
1722 _ = @import("path.zig");1722 _ = @import("linux/index.zig");
1723 _ = @import("windows/index.zig");1723 _ = @import("path.zig");
1724 _ = @import("test.zig");1724 _ = @import("time.zig");
1725 _ = @import("windows/index.zig");
1726 _ = @import("test.zig");
1727 }
1725}1728}
17261729
17271730
std/os/time.zig+18-6
...@@ -4,6 +4,7 @@ const Os = builtin.Os;...@@ -4,6 +4,7 @@ const Os = builtin.Os;
4const debug = std.debug;4const debug = std.debug;
55
6const windows = std.os.windows;6const windows = std.os.windows;
7const linux = std.os.linux;
7const darwin = std.os.darwin;8const darwin = std.os.darwin;
8const posix = std.os.posix;9const posix = std.os.posix;
910
...@@ -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;
119pub const Timer = struct {120pub const Timer = struct {
120 121
121 //if we used resolution's value when performing the122 //if we used resolution's value when performing the
122 // performance counter calc on windows, it would be123 // performance counter calc on windows/darwin, it would
123 // less precise124 // 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 of149 // 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 }