Skip to content

Commit 8a9e02c

Browse files
committed
Test that for BSD's normal threads niceness is used.
Also attempts to solve the CI issue with the registry.
1 parent abf7da7 commit 8a9e02c

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,17 @@ jobs:
5656
if: matrix.features == ''
5757
run: cargo build --all-features
5858

59-
- name: Test all features (other)
60-
if: matrix.features == '' && runner.os != 'Linux'
61-
run: cargo test --all-features -- --skip set_deadline_policy
59+
- name: Test all features (macos)
60+
if: matrix.features == '' && runner.os == 'macOS'
61+
run: sudo -E /Users/runner/.cargo/bin/cargo test --all-features
62+
63+
- name: Test all features (Windows)
64+
if: matrix.features == '' && runner.os == 'Windows'
65+
run: cargo test --all-features
66+
67+
- name: Test all features (non-linux and non-macOS)
68+
if: matrix.features == '' && runner.os != 'Linux' && runner.os != 'macOS' && runner.os != 'Windows'
69+
run: sudo -E /home/runner/.cargo/bin/cargo test --all-features -- --skip set_deadline_policy
6270

6371
- name: Test all features (Linux)
6472
if: matrix.features == '' && runner.os == 'Linux'

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ categories = ["concurrency", "asynchronous", "os"]
1212
edition = "2018"
1313

1414
[dev-dependencies]
15-
rstest = "0.15"
15+
rstest = "0.13"
1616

1717
[dependencies]
1818
log = "0.4"

src/unix.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ fn errno() -> libc::c_int {
5454
}
5555
}
5656

57+
fn set_errno(number: libc::c_int) {
58+
unsafe {
59+
cfg_if::cfg_if! {
60+
if #[cfg(any(target_os = "openbsd", target_os = "netbsd"))] {
61+
*libc::__errno() = number;
62+
} else if #[cfg(target_os = "linux")] {
63+
*libc::__errno_location() = number;
64+
} else if #[cfg(any(target_os = "macos", target_os = "freebsd"))] {
65+
*libc::__error() = number;
66+
} else {
67+
compile_error!("Your OS is probably not supported.")
68+
}
69+
}
70+
}
71+
}
72+
5773
/// Copy of the Linux kernel's sched_attr type
5874
#[repr(C)]
5975
#[derive(Debug, Default)]
@@ -315,7 +331,6 @@ impl ThreadPriority {
315331
let niceness_values = NICENESS_MAX.abs() + NICENESS_MIN.abs();
316332
let ratio = p as f32 / ThreadPriorityValue::MAX as f32;
317333
let niceness = ((niceness_values as f32 * ratio) as i8 + NICENESS_MAX) as i32;
318-
dbg!(niceness_values, ratio, niceness);
319334
Self::to_allowed_value_for_policy(niceness, policy).map(|v| v as u32)
320335
}
321336
},
@@ -446,7 +461,6 @@ pub fn set_thread_priority_and_policy(
446461
}
447462
_ => {
448463
let fixed_priority = priority.to_posix(policy)?;
449-
dbg!(policy, priority, fixed_priority);
450464
if let ThreadSchedulePolicy::Realtime(_) = policy {
451465
// If the policy is a realtime one, the priority is set via
452466
// pthread_setschedparam.
@@ -470,11 +484,13 @@ pub fn set_thread_priority_and_policy(
470484
} else {
471485
// If this is a normal-scheduled thread, the priority is
472486
// set via niceness.
473-
let ret = unsafe { libc::nice(fixed_priority) };
487+
set_errno(0);
474488

475-
match ret {
476-
-1 => Err(Error::OS(errno())),
477-
_ => Ok(()),
489+
unsafe { libc::nice(fixed_priority) };
490+
491+
match errno() {
492+
0 => Ok(()),
493+
e => Err(Error::OS(e)),
478494
}
479495
}
480496
}

tests/unix.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn get_and_set_priority_with_normal_policies(
2626
}
2727
}
2828

29+
// In macOS it is allowed to specify number as a SCHED_OTHER policy priority.
2930
#[cfg(any(
3031
target_os = "macos",
3132
target_os = "openbsd",
@@ -39,8 +40,7 @@ fn get_and_set_priority_with_normal_policies(
3940
#[values(ThreadPriority::Min, ThreadPriority::Max, ThreadPriority::Crossplatform(23u8.try_into().unwrap()))]
4041
priority: ThreadPriority,
4142
) {
42-
// In macOS it is allowed to specify number as a SCHED_OTHER policy priority.
43-
assert!(set_thread_priority_and_policy(thread_native_id(), priority, policy,).is_ok());
43+
assert!(set_thread_priority_and_policy(thread_native_id(), priority, policy).is_ok());
4444
}
4545

4646
#[rstest]
@@ -69,6 +69,7 @@ fn check_min_and_max_priority_values(
6969
#[case(ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Other))]
7070
fn set_priority_with_normal_policy_but_with_invalid_value(#[case] policy: ThreadSchedulePolicy) {
7171
let thread_id = thread_native_id();
72+
#[cfg(target_os = "linux")]
7273
let expected = if policy == ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Idle) {
7374
// In Linux we should get an error whenever a non-zero value is passed as priority and a normal
7475
// scheduling policy is used.
@@ -94,7 +95,8 @@ fn set_priority_with_normal_policy_but_with_invalid_value(#[case] policy: Thread
9495
target_os = "netbsd"
9596
))]
9697
#[test]
97-
// In macOS the SCHED_OTHER policy allows having a non-zero priority value.
98+
// In macOS the SCHED_OTHER policy allows having a non-zero priority value,
99+
// but the crate doesn't use this opportunity for normal threads and uses niceness instead.
98100
fn get_and_set_priority_with_normal_policy() {
99101
let thread_id = thread_native_id();
100102
let normal_policy = ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Other);
@@ -108,17 +110,6 @@ fn get_and_set_priority_with_normal_policy() {
108110
Ok(())
109111
);
110112
assert_eq!(thread_schedule_policy(), Ok(normal_policy));
111-
assert_eq!(
112-
thread_schedule_policy_param(thread_native_id()),
113-
Ok((normal_policy, ScheduleParams { sched_priority: 23 }))
114-
);
115-
assert_eq!(
116-
Thread::current(),
117-
Ok(Thread {
118-
priority: ThreadPriority::Crossplatform(23u8.try_into().unwrap()),
119-
id: thread_native_id()
120-
})
121-
);
122113
}
123114

124115
#[rstest]

0 commit comments

Comments
 (0)