authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-25 16:54:32-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-25 16:54:32-07:00
log37a1d08de2ce263439713180f57741d16fb27e23
tree9c6eae297ebc492c0a1a94a4d3d25fc3bcd07bd9
parent96a08c1698189baa01e051bcee2c488ba594fa0c

haiku: minor fixups

* no isHaiku() function since there is not more than one os tag that this applies to. * clean up some control flow into a switch * add some TODO comments to investigate panics that suspiciously look like they should be compile errors (see #363)

3 files changed, 40 insertions(+), 40 deletions(-)

lib/std/Thread.zig+35-31
...@@ -487,38 +487,42 @@ pub const CpuCountError = error{...@@ -487,38 +487,42 @@ pub const CpuCountError = error{
487};487};
488488
489pub fn cpuCount() CpuCountError!usize {489pub fn cpuCount() CpuCountError!usize {
490 if (std.Target.current.os.tag == .linux) {490 switch (std.Target.current.os.tag) {
491 const cpu_set = try os.sched_getaffinity(0);491 .linux => {
492 return @as(usize, os.CPU_COUNT(cpu_set)); // TODO should not need this usize cast492 const cpu_set = try os.sched_getaffinity(0);
493 }493 return @as(usize, os.CPU_COUNT(cpu_set)); // TODO should not need this usize cast
494 if (std.Target.current.os.tag == .windows) {494 },
495 return os.windows.peb().NumberOfProcessors;495 .windows => {
496 }496 return os.windows.peb().NumberOfProcessors;
497 if (std.Target.current.os.tag == .openbsd) {497 },
498 var count: c_int = undefined;498 .openbsd => {
499 var count_size: usize = @sizeOf(c_int);499 var count: c_int = undefined;
500 const mib = [_]c_int{ os.CTL_HW, os.HW_NCPUONLINE };500 var count_size: usize = @sizeOf(c_int);
501 os.sysctl(&mib, &count, &count_size, null, 0) catch |err| switch (err) {501 const mib = [_]c_int{ os.CTL_HW, os.HW_NCPUONLINE };
502 error.NameTooLong, error.UnknownName => unreachable,502 os.sysctl(&mib, &count, &count_size, null, 0) catch |err| switch (err) {
503 else => |e| return e,503 error.NameTooLong, error.UnknownName => unreachable,
504 };504 else => |e| return e,
505 return @intCast(usize, count);505 };
506 }506 return @intCast(usize, count);
507 if (std.Target.current.os.tag == .haiku) {507 },
508 var count: u32 = undefined;508 .haiku => {
509 var system_info: os.system_info = undefined;509 var count: u32 = undefined;
510 const rc = os.system.get_system_info(&system_info);510 var system_info: os.system_info = undefined;
511 count = system_info.cpu_count;511 const rc = os.system.get_system_info(&system_info);
512 return @intCast(usize, count);512 count = system_info.cpu_count;
513 return @intCast(usize, count);
514 },
515 else => {
516 var count: c_int = undefined;
517 var count_len: usize = @sizeOf(c_int);
518 const name = if (comptime std.Target.current.isDarwin()) "hw.logicalcpu" else "hw.ncpu";
519 os.sysctlbynameZ(name, &count, &count_len, null, 0) catch |err| switch (err) {
520 error.NameTooLong, error.UnknownName => unreachable,
521 else => |e| return e,
522 };
523 return @intCast(usize, count);
524 },
513 }525 }
514 var count: c_int = undefined;
515 var count_len: usize = @sizeOf(c_int);
516 const name = if (comptime std.Target.current.isDarwin()) "hw.logicalcpu" else "hw.ncpu";
517 os.sysctlbynameZ(name, &count, &count_len, null, 0) catch |err| switch (err) {
518 error.NameTooLong, error.UnknownName => unreachable,
519 else => |e| return e,
520 };
521 return @intCast(usize, count);
522}526}
523527
524pub fn getCurrentThreadId() u64 {528pub fn getCurrentThreadId() u64 {
lib/std/os.zig+5-5
...@@ -3892,7 +3892,7 @@ pub fn pipe2(flags: u32) PipeError![2]fd_t {...@@ -3892,7 +3892,7 @@ pub fn pipe2(flags: u32) PipeError![2]fd_t {
3892 }3892 }
3893 }3893 }
3894 }3894 }
3895 if (comptime std.Target.current.isHaiku()) {3895 if (std.Target.current.os.tag == .haiku) {
3896 var fds: [2]fd_t = try pipe();3896 var fds: [2]fd_t = try pipe();
3897 if (flags == 0) return fds;3897 if (flags == 0) return fds;
3898 errdefer {3898 errdefer {
...@@ -3939,10 +3939,10 @@ pub fn sysctl(...@@ -3939,10 +3939,10 @@ pub fn sysctl(
3939 newlen: usize,3939 newlen: usize,
3940) SysCtlError!void {3940) SysCtlError!void {
3941 if (builtin.os.tag == .wasi) {3941 if (builtin.os.tag == .wasi) {
3942 @panic("unsupported");3942 @panic("unsupported"); // TODO should be compile error, not panic
3943 }3943 }
3944 if (builtin.os.tag == .haiku) {3944 if (builtin.os.tag == .haiku) {
3945 @panic("unsupported");3945 @panic("unsupported"); // TODO should be compile error, not panic
3946 }3946 }
39473947
3948 const name_len = math.cast(c_uint, name.len) catch return error.NameTooLong;3948 const name_len = math.cast(c_uint, name.len) catch return error.NameTooLong;
...@@ -3966,10 +3966,10 @@ pub fn sysctlbynameZ(...@@ -3966,10 +3966,10 @@ pub fn sysctlbynameZ(
3966 newlen: usize,3966 newlen: usize,
3967) SysCtlError!void {3967) SysCtlError!void {
3968 if (builtin.os.tag == .wasi) {3968 if (builtin.os.tag == .wasi) {
3969 @panic("unsupported");3969 @panic("unsupported"); // TODO should be compile error, not panic
3970 }3970 }
3971 if (builtin.os.tag == .haiku) {3971 if (builtin.os.tag == .haiku) {
3972 @panic("unsupported");3972 @panic("unsupported"); // TODO should be compile error, not panic
3973 }3973 }
39743974
3975 switch (errno(system.sysctlbyname(name, oldp, oldlenp, newp, newlen))) {3975 switch (errno(system.sysctlbyname(name, oldp, oldlenp, newp, newlen))) {
lib/std/target.zig-4
...@@ -1339,10 +1339,6 @@ pub const Target = struct {...@@ -1339,10 +1339,6 @@ pub const Target = struct {
1339 return self.os.tag.isDarwin();1339 return self.os.tag.isDarwin();
1340 }1340 }
13411341
1342 pub fn isHaiku(self: Target) bool {
1343 return self.os.tag == .haiku;
1344 }
1345
1346 pub fn isGnuLibC_os_tag_abi(os_tag: Os.Tag, abi: Abi) bool {1342 pub fn isGnuLibC_os_tag_abi(os_tag: Os.Tag, abi: Abi) bool {
1347 return os_tag == .linux and abi.isGnu();1343 return os_tag == .linux and abi.isGnu();
1348 }1344 }