From 8784c7b581b8a21c3698c4564e2a912676adaa2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Marie?= Date: Tue, 10 Nov 2020 05:25:59 +0000 Subject: [PATCH] openbsd: proper implementation for Thread.cpuCount() --- lib/std/os/bits/openbsd.zig | 3 +++ lib/std/thread.zig | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/std/os/bits/openbsd.zig b/lib/std/os/bits/openbsd.zig index 472b93a34a..24b4a5add3 100644 --- a/lib/std/os/bits/openbsd.zig +++ b/lib/std/os/bits/openbsd.zig @@ -292,10 +292,13 @@ pub const AI_ADDRCONFIG = 64; pub const CTL_KERN = 1; pub const CTL_DEBUG = 5; +pub const CTL_HW = 6; pub const KERN_PROC_ARGS = 55; pub const KERN_PROC_ARGV = 1; +pub const HW_NCPUONLINE = 25; + pub const PATH_MAX = 1024; pub const STDIN_FILENO = 0; diff --git a/lib/std/thread.zig b/lib/std/thread.zig index 330c425dd6..b45a37ca1b 100644 --- a/lib/std/thread.zig +++ b/lib/std/thread.zig @@ -491,6 +491,16 @@ pub const Thread = struct { if (std.Target.current.os.tag == .windows) { return os.windows.peb().NumberOfProcessors; } + if (std.Target.current.os.tag == .openbsd) { + var count: c_int = undefined; + var count_size: usize = @sizeOf(c_int); + const mib = [_]c_int{ os.CTL_HW, os.HW_NCPUONLINE }; + os.sysctl(&mib, &count, &count_size, null, 0) catch |err| switch (err) { + error.NameTooLong, error.UnknownName => unreachable, + else => |e| return e, + }; + return @intCast(usize, count); + } var count: c_int = undefined; var count_len: usize = @sizeOf(c_int); const name = if (comptime std.Target.current.isDarwin()) "hw.logicalcpu" else "hw.ncpu";