Use Ord::clamp to simplify the scheduling priority value

This commit is contained in:
LI Qing
2024-06-19 14:08:58 +08:00
committed by Tate, Hongliang Tian
parent ef075d15d0
commit 9711d43c84
2 changed files with 5 additions and 19 deletions

View File

@ -30,12 +30,8 @@ impl Nice {
/// Values given beyond the permissible range are automatically adjusted /// Values given beyond the permissible range are automatically adjusted
/// to the nearest boundary value. /// to the nearest boundary value.
pub fn new(raw: i8) -> Self { pub fn new(raw: i8) -> Self {
if raw > Self::MAX.to_raw() { Self {
Self::MAX value: raw.clamp(Self::MIN.to_raw(), Self::MAX.to_raw()),
} else if raw < Self::MIN.to_raw() {
Self::MIN
} else {
Self { value: raw }
} }
} }
@ -87,12 +83,8 @@ impl Priority {
/// Values given beyond the permissible range are automatically adjusted /// Values given beyond the permissible range are automatically adjusted
/// to the nearest boundary value. /// to the nearest boundary value.
pub fn new(raw: u8) -> Self { pub fn new(raw: u8) -> Self {
if raw > Self::MAX.to_raw() { Self {
Self::MAX value: raw.clamp(Self::MIN.to_raw(), Self::MAX.to_raw()),
} else if raw < Self::MIN.to_raw() {
Self::MIN
} else {
Self { value: raw }
} }
} }

View File

@ -12,13 +12,7 @@ use crate::{
pub fn sys_set_priority(which: i32, who: u32, prio: i32) -> Result<SyscallReturn> { pub fn sys_set_priority(which: i32, who: u32, prio: i32) -> Result<SyscallReturn> {
let prio_target = PriorityTarget::new(which, who)?; let prio_target = PriorityTarget::new(which, who)?;
let new_nice = { let new_nice = {
let norm_prio = if prio > i8::MAX as i32 { let norm_prio = prio.clamp(i8::MIN as i32, i8::MAX as i32) as i8;
i8::MAX
} else if prio < i8::MIN as i32 {
i8::MIN
} else {
prio as i8
};
Nice::new(norm_prio) Nice::new(norm_prio)
}; };