Skip to content

Commit

Permalink
Fixes behavior when changing from real-time to normal policy
Browse files Browse the repository at this point in the history
  • Loading branch information
kmdade committed Apr 28, 2024
1 parent c0634a0 commit 72bdeb7
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,19 +596,29 @@ pub fn set_thread_priority_and_policy(
e => Err(Error::OS(e)),
}
} else {
// If this is a normal-scheduled thread, the priority is
// set via niceness.
set_errno(0);
// Normal priority threads must be set with static priority 0.
let params = ScheduleParams { sched_priority: 0 }.into_posix();

let ret = unsafe { libc::setpriority(libc::PRIO_PROCESS, 0, fixed_priority) };
if ret == 0 {
return Ok(());
let ret = unsafe {
libc::pthread_setschedparam(
native,
policy.to_posix(),
&params as *const libc::sched_param,
)
};

if ret != 0 {
return Err(Error::OS(ret));
}

match errno() {
0 => Ok(()),
e => Err(Error::OS(e)),
// Normal priority threads adjust relative priority through niceness.
set_errno(0);
let ret = unsafe { libc::setpriority(libc::PRIO_PROCESS, 0, fixed_priority) };
if ret != 0 {
return Err(Error::OS(errno()));
}

Ok(())
}
}
}
Expand Down

0 comments on commit 72bdeb7

Please sign in to comment.