Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add realtime scheduling calls to std.os.linux (issue #19671) #19675

Merged
merged 15 commits into from
Nov 16, 2024
Merged
52 changes: 52 additions & 0 deletions lib/std/os/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2001,6 +2001,58 @@ pub fn fremovexattr(fd: usize, name: [*:0]const u8) usize {
return syscall2(.fremovexattr, fd, @intFromPtr(name));
}

pub const sched_param = extern struct {
sched_priority: i32,
alexrp marked this conversation as resolved.
Show resolved Hide resolved
};

pub const SCHED = packed struct(i32) {
pub const Mode = enum(u3) {
/// normal multi-user scheduling
OTHER = 0,
curuvar marked this conversation as resolved.
Show resolved Hide resolved
/// FIFO realtime scheduling
FIFO = 1,
/// Round-robin realtime scheduling
RR = 2,
/// For "batch" style execution of processes
BATCH = 3,
/// Low latency scheduling
ISO = 4,
curuvar marked this conversation as resolved.
Show resolved Hide resolved
/// For running very low priority background jobs
IDLE = 5,
/// Sporadic task model deadline scheduling
DEADLINE = 6,
};
mode: Mode, //bits [0, 2]
_3: u27 = 0, //bits [3, 29]
/// set to true to stop children from inheriting policies
RESET_ON_FORK: bool = false, //bit 30
_31: u1 = 0, //bit 31
};

pub fn sched_setparam(pid: pid_t, param: *const sched_param) usize {
return syscall2(.sched_setparam, @as(usize, @bitCast(@as(isize, pid))), @intFromPtr(param));
}

pub fn sched_getparam(pid: pid_t, param: *sched_param) usize {
return syscall2(.sched_getparam, @as(usize, @bitCast(@as(isize, pid))), @intFromPtr(param));
}

pub fn sched_setscheduler(pid: pid_t, policy: SCHED, param: *const sched_param) usize {
return syscall3(.sched_setscheduler, @as(usize, @bitCast(@as(isize, pid))), @intCast(@as(u32, @bitCast(policy))), @intFromPtr(param));
}

pub fn sched_getscheduler(pid: pid_t) SCHED {
curuvar marked this conversation as resolved.
Show resolved Hide resolved
return @bitCast( @as(u32, @intCast(syscall1(.sched_getscheduler, @as(usize, @bitCast(@as(isize, pid)))))));
curuvar marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn sched_get_priority_max(policy: SCHED) isize {
curuvar marked this conversation as resolved.
Show resolved Hide resolved
return @bitCast(syscall1(.sched_get_priority_max,@intCast(@as(u32, @bitCast(policy)))));
curuvar marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn sched_get_priority_min(policy: SCHED) isize {
curuvar marked this conversation as resolved.
Show resolved Hide resolved
return @bitCast(syscall1(.sched_get_priority_min, @intCast(@as(u32, @bitCast(policy)))));
}

pub fn sched_yield() usize {
return syscall0(.sched_yield);
}
Expand Down