Skip to content

Commit 333e5f4

Browse files
committed
Snapshot
1 parent 7f967a5 commit 333e5f4

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

src/new.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,25 +190,29 @@ impl From<crate::unix::Niceness> for ThreadPriority {
190190
fn from(value: crate::unix::Niceness) -> Self {
191191
use crate::unix::Niceness;
192192

193-
let niceness_values = Niceness::MAX.abs() + Niceness::MIN.abs();
194-
let value = (((value.get_value() + Niceness::MAX) / niceness_values)
195-
as ThreadPriorityValue)
193+
// Add 1 to include the corner value (0 also counts), so in the
194+
// range of [-20;19] there are 40 values, not just 39.
195+
let niceness_values = Niceness::MAX.abs() + Niceness::MIN.abs() + 1;
196+
let value = ((((value.get_value() + Niceness::MAX.abs()) as f32 / niceness_values as f32)
197+
* 100.0f32) as ThreadPriorityValue)
196198
.clamp(ThreadPriority::MINIMUM.0, ThreadPriority::MAXIMUM.0);
197199
Self(value)
198200
}
199201
}
200202

203+
/// Maps the [0;100] range to [-19;20].
201204
#[cfg(not(windows))]
202205
impl From<ThreadPriority> for crate::unix::Niceness {
203206
fn from(value: ThreadPriority) -> Self {
204207
use crate::unix::{Niceness, NicenessValue};
205208

206-
let niceness_values = Niceness::MAX.abs() + Niceness::MIN.abs();
209+
// Add one to include the corner value.
210+
let niceness_values = Niceness::MAX.abs() + Niceness::MIN.abs() + 1;
207211
// As the MIN-MAX is [0; 100], we don't need to work with percentages:
208212
// the value may already be considered a percentage.
209-
let ratio = value.0 as f32;
210-
let niceness =
211-
((niceness_values as f32 * ratio) as NicenessValue + Niceness::MAX) as NicenessValue;
213+
let ratio = -(value.0 as f32 / ThreadPriority::MAXIMUM.0 as f32);
214+
let niceness_value = (niceness_values as f32 * ratio) as NicenessValue;
215+
let niceness = (niceness_value + Niceness::MAX.abs() + 1) as NicenessValue;
212216
Niceness::new_unchecked(niceness)
213217
}
214218
}
@@ -239,6 +243,13 @@ impl std::convert::TryFrom<UnixThreadPriority> for ThreadPriority {
239243
impl std::convert::TryFrom<ThreadPriority> for UnixThreadPriority {
240244
type Error = Error;
241245

246+
#[cfg_attr(
247+
all(
248+
any(target_os = "linux", target_os = "android"),
249+
not(target_arch = "wasm32")
250+
),
251+
allow(unused_variables)
252+
)]
242253
fn try_from(value: ThreadPriority) -> Result<Self, Self::Error> {
243254
// We only work with the default (Other) schedule policy here.
244255
let policy = crate::unix::ThreadSchedulePolicy::Normal(

src/unix.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl ThreadId {
5050
any(target_os = "linux", target_os = "android"),
5151
not(target_arch = "wasm32")
5252
))]
53-
let system = Some(get_thread_system_id());
53+
let system = Some(get_thread_system_id() as libc::id_t);
5454
#[cfg(not(all(
5555
any(target_os = "linux", target_os = "android"),
5656
not(target_arch = "wasm32")
@@ -342,12 +342,17 @@ impl NormalThreadSchedulePolicy {
342342
/// Returns true if the schedule policy allows to control the thread
343343
/// priority only via the dynamic priority value, - niceness.
344344
pub fn is_niceness_based(&self) -> bool {
345-
match self {
346-
#[cfg(any(target_os = "linux", target_os = "android"))]
347-
NormalThreadSchedulePolicy::Idle => true,
348-
#[cfg(any(target_os = "linux", target_os = "android"))]
349-
NormalThreadSchedulePolicy::Batch => true,
350-
NormalThreadSchedulePolicy::Other => false,
345+
cfg_if::cfg_if! {
346+
if #[cfg(any(target_os = "linux", target_os = "android"))] {
347+
// On Linux all normal thread scheduling policies can't
348+
// have any other value than `0` and should be controlled
349+
// via niceness.
350+
return true;
351+
} else {
352+
// On other platforms, there is only SCHED_OTHER which
353+
// can be controlled via pthread's priority.
354+
return false;
355+
}
351356
}
352357
}
353358
}
@@ -575,7 +580,9 @@ fn set_thread_priority_and_policy_deadline(
575580
))
576581
}
577582
};
578-
let tid = native as libc::pid_t;
583+
let tid = native
584+
.system
585+
.ok_or(Error::Priority("Can't obtain the system thread id."))?;
579586
let sched_attr = SchedAttr {
580587
size: std::mem::size_of::<SchedAttr>() as u32,
581588
sched_policy: RealtimeThreadSchedulePolicy::Deadline.to_posix() as u32,
@@ -633,7 +640,7 @@ pub fn set_thread_priority_and_policy(
633640
// SCHED_DEADLINE policy requires its own syscall
634641
#[cfg(any(target_os = "linux", target_os = "android"))]
635642
ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::Deadline) => {
636-
set_thread_priority_and_policy_deadline(thread_id.pthread, priority)
643+
set_thread_priority_and_policy_deadline(thread_id, priority)
637644
}
638645
_ => {
639646
let fixed_priority = priority.to_posix(policy)?;
@@ -848,9 +855,9 @@ pub trait ThreadExt {
848855
cfg_if::cfg_if! {
849856
if #[cfg(all(any(target_os = "linux", target_os = "android"), not(target_arch = "wasm32")))] {
850857
if policy == ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::Deadline) {
851-
set_thread_priority_and_policy(get_thread_pthread_id(), ThreadPriority::Crossplatform(ThreadPriorityValue(0)), policy)
858+
set_thread_priority_and_policy(ThreadId::get_for_current(), ThreadPriority::Crossplatform(ThreadPriorityValue(0)), policy)
852859
} else {
853-
set_thread_priority_and_policy(get_thread_pthread_id(), priority, policy)
860+
set_thread_priority_and_policy(ThreadId::get_for_current(), priority, policy)
854861
}
855862
} else {
856863
set_thread_priority_and_policy(ThreadId::get_for_current(), priority, policy)
@@ -919,7 +926,7 @@ mod tests {
919926
use std::time::Duration;
920927

921928
assert!(set_thread_priority_and_policy(
922-
0, // current thread
929+
ThreadId::get_for_current(),
923930
ThreadPriority::Deadline {
924931
runtime: Duration::from_millis(1),
925932
deadline: Duration::from_millis(10),

tests/unix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn get_and_set_priority_with_normal_policies(
1616
#[values(ThreadPriority::Min, ThreadPriority::Max, ThreadPriority::Crossplatform(23u8.try_into().unwrap()))]
1717
priority: ThreadPriority,
1818
) {
19-
let ret = set_thread_priority_and_policy(get_thread_pthread_id(), priority, policy);
19+
let ret = set_thread_priority_and_policy(ThreadId::get_for_current(), priority, policy);
2020
if policy == ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Idle)
2121
&& priority == ThreadPriority::Crossplatform(23u8.try_into().unwrap())
2222
{

0 commit comments

Comments
 (0)