Commit 182bec8771

Andrew Kelley <andrew@ziglang.org>
2023-07-31 20:01:42
Revert "std.os: adding linux's sched_setaffinity and its wrapper"
This reverts commit c7bf8bab38f8b89c1371eedb9229e00a29b5ca5b.
1 parent 86a5edc
Changed files (3)
lib/std/c/linux.zig
@@ -13,10 +13,6 @@ pub const ARCH = linux.ARCH;
 pub const AT = linux.AT;
 pub const CLOCK = linux.CLOCK;
 pub const CPU_COUNT = linux.CPU_COUNT;
-pub const CPU_SET = linux.CPU_SET;
-pub const CPU_ISSET = linux.CPU_ISSET;
-pub const CPU_CLR = linux.CPU_CLR;
-pub const CPU_ZERO = linux.CPU_ZERO;
 pub const E = linux.E;
 pub const Elf_Symndx = linux.Elf_Symndx;
 pub const F = linux.F;
@@ -249,7 +245,6 @@ pub extern "c" fn setrlimit64(resource: rlimit_resource, rlim: *const rlimit) c_
 
 pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;
 pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int;
-pub extern "c" fn sched_setaffinity(pid: c_int, size: usize, set: *const cpu_set_t) c_int;
 pub extern "c" fn eventfd(initval: c_uint, flags: c_uint) c_int;
 pub extern "c" fn epoll_ctl(epfd: fd_t, op: c_uint, fd: fd_t, event: ?*epoll_event) c_int;
 pub extern "c" fn epoll_create1(flags: c_uint) c_int;
lib/std/os/linux.zig
@@ -1536,12 +1536,6 @@ pub fn mbind(addr: ?*anyopaque, len: u32, mode: i32, nodemask: *const u32, maxno
     return syscall6(.mbind, @intFromPtr(addr), len, @as(usize, @bitCast(@as(isize, mode))), @intFromPtr(nodemask), maxnode, flags);
 }
 
-pub fn sched_setaffinity(pid: pid_t, size: usize, set: *const cpu_set_t) usize {
-    const rc = syscall3(.sched_setaffinity, @as(usize, @bitCast(@as(isize, pid))), size, @intFromPtr(set));
-    if (@as(isize, @bitCast(rc)) < 0) return rc;
-    return 0;
-}
-
 pub fn epoll_create() usize {
     return epoll_create1(0);
 }
@@ -3560,11 +3554,6 @@ pub const CPU_SETSIZE = 128;
 pub const cpu_set_t = [CPU_SETSIZE / @sizeOf(usize)]usize;
 pub const cpu_count_t = std.meta.Int(.unsigned, std.math.log2(CPU_SETSIZE * 8));
 
-fn cpu_mask(s: usize) cpu_count_t {
-    var x = s & (CPU_SETSIZE * 8);
-    return @as(cpu_count_t, @intCast(1)) << @as(u4, @intCast(x));
-}
-
 pub fn CPU_COUNT(set: cpu_set_t) cpu_count_t {
     var sum: cpu_count_t = 0;
     for (set) |x| {
@@ -3573,32 +3562,6 @@ pub fn CPU_COUNT(set: cpu_set_t) cpu_count_t {
     return sum;
 }
 
-pub fn CPU_ZERO(set: *cpu_set_t) void {
-    @memset(set, 0);
-}
-
-pub fn CPU_SET(cpu: usize, set: *cpu_set_t) void {
-    const x = cpu / @sizeOf(usize);
-    if (x < @sizeOf(cpu_set_t)) {
-        (set.*)[x] |= cpu_mask(x);
-    }
-}
-
-pub fn CPU_ISSET(cpu: usize, set: cpu_set_t) bool {
-    const x = cpu / @sizeOf(usize);
-    if (x < @sizeOf(cpu_set_t)) {
-        return set[x] & cpu_mask(x) != 0;
-    }
-    return false;
-}
-
-pub fn CPU_CLR(cpu: usize, set: *cpu_set_t) void {
-    const x = cpu / @sizeOf(usize);
-    if (x < @sizeOf(cpu_set_t)) {
-        (set.*)[x] &= !cpu_mask(x);
-    }
-}
-
 pub const MINSIGSTKSZ = switch (native_arch) {
     .x86, .x86_64, .arm, .mipsel => 2048,
     .aarch64 => 5120,
lib/std/os.zig
@@ -5494,7 +5494,6 @@ pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void {
 }
 
 pub const SchedGetAffinityError = error{PermissionDenied} || UnexpectedError;
-pub const SchedSetAffinityError = error{ InvalidCpu, PermissionDenied } || UnexpectedError;
 
 pub fn sched_getaffinity(pid: pid_t) SchedGetAffinityError!cpu_set_t {
     var set: cpu_set_t = undefined;
@@ -5522,26 +5521,6 @@ pub fn sched_getaffinity(pid: pid_t) SchedGetAffinityError!cpu_set_t {
     }
 }
 
-pub fn sched_setaffinity(pid: pid_t, cpus: []usize) SchedSetAffinityError!cpu_set_t {
-    var set: cpu_set_t = undefined;
-    if (builtin.os.tag == .linux) {
-        system.CPU_ZERO(&set);
-        for (cpus) |cpu| {
-            system.CPU_SET(cpu, &set);
-        }
-        switch (errno(system.sched_setaffinity(pid, @sizeOf(cpu_set_t), &set))) {
-            .SUCCESS => return set,
-            .FAULT => unreachable,
-            .SRCH => unreachable,
-            .INVAL => return error.InvalidCpu,
-            .PERM => return error.PermissionDenied,
-            else => |err| return unexpectedErrno(err),
-        }
-    } else {
-        @compileError("unsupported platform");
-    }
-}
-
 /// Used to convert a slice to a null terminated slice on the stack.
 /// TODO https://github.com/ziglang/zig/issues/287
 pub fn toPosixPath(file_path: []const u8) ![MAX_PATH_BYTES - 1:0]u8 {