commit dbdafb6cc503ce5820713dfa79cc956438b7957a (tree)
parent 0282c2a924054472ccc8d82325e8393442381760
Author: David CARLIER <devnexen@gmail.com>
Date: Wed, 19 Apr 2023 22:49:14 +0100
os: expand sched_getaffinity wrapper and update freebsd's cpuset api flags.
Diffstat:
2 files changed, 40 insertions(+), 8 deletions(-)
diff --git a/lib/std/c/freebsd.zig b/lib/std/c/freebsd.zig
@@ -13,6 +13,19 @@ pub const cpulevel_t = c_int;
pub const cpuwhich_t = c_int;
pub const id_t = i64;
+pub const CPU_LEVEL_ROOT: cpulevel_t = 1;
+pub const CPU_LEVEL_CPUSET: cpulevel_t = 2;
+pub const CPU_LEVEL_WHICH: cpulevel_t = 3;
+pub const CPU_WHICH_TID: cpuwhich_t = 1;
+pub const CPU_WHICH_PID: cpuwhich_t = 2;
+pub const CPU_WHICH_CPUSET: cpuwhich_t = 3;
+pub const CPU_WHICH_IRQ: cpuwhich_t = 4;
+pub const CPU_WHICH_JAIL: cpuwhich_t = 5;
+pub const CPU_WHICH_DOMAIN: cpuwhich_t = 6;
+pub const CPU_WHICH_INTRHANDLER: cpuwhich_t = 7;
+pub const CPU_WHICH_ITHREAD: cpuwhich_t = 8;
+pub const CPU_WHICH_TIDPID: cpuwhich_t = 8;
+
extern "c" fn __error() *c_int;
pub const _errno = __error;
diff --git a/lib/std/os.zig b/lib/std/os.zig
@@ -141,7 +141,12 @@ pub const addrinfo = system.addrinfo;
pub const blkcnt_t = system.blkcnt_t;
pub const blksize_t = system.blksize_t;
pub const clock_t = system.clock_t;
-pub const cpu_set_t = system.cpu_set_t;
+pub const cpu_set_t = if (builtin.os.tag == .linux)
+ system.cpu_set_t
+else if (builtin.os.tag == .freebsd)
+ freebsd.cpuset_t
+else
+ u32;
pub const dev_t = system.dev_t;
pub const dl_phdr_info = system.dl_phdr_info;
pub const empty_sigset = system.empty_sigset;
@@ -5518,13 +5523,27 @@ pub const SchedGetAffinityError = error{PermissionDenied} || UnexpectedError;
pub fn sched_getaffinity(pid: pid_t) SchedGetAffinityError!cpu_set_t {
var set: cpu_set_t = undefined;
- switch (errno(system.sched_getaffinity(pid, @sizeOf(cpu_set_t), &set))) {
- .SUCCESS => return set,
- .FAULT => unreachable,
- .INVAL => unreachable,
- .SRCH => unreachable,
- .PERM => return error.PermissionDenied,
- else => |err| return unexpectedErrno(err),
+ if (builtin.os.tag == .linux) {
+ switch (errno(system.sched_getaffinity(pid, @sizeOf(cpu_set_t), &set))) {
+ .SUCCESS => return set,
+ .FAULT => unreachable,
+ .INVAL => unreachable,
+ .SRCH => unreachable,
+ .PERM => return error.PermissionDenied,
+ else => |err| return unexpectedErrno(err),
+ }
+ } else if (builtin.os.tag == .freebsd) {
+ switch (errno(freebsd.cpuset_getaffinity(freebsd.CPU_LEVEL_WHICH, freebsd.CPU_WHICH_PID, pid, @sizeOf(cpu_set_t), &set))) {
+ .SUCCESS => return set,
+ .FAULT => unreachable,
+ .INVAL => unreachable,
+ .SRCH => unreachable,
+ .EDEADLK => unreachable,
+ .PERM => return error.PermissionDenied,
+ else => |err| return unexpectedErrno(err),
+ }
+ } else {
+ @compileError("unsupported platform");
}
}