Skip to content

Commit

Permalink
Fix macOS tests and bump MSRV
Browse files Browse the repository at this point in the history
  • Loading branch information
iddm committed Nov 13, 2023
1 parent 6f980a9 commit 1f69973
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -3,7 +3,7 @@ name: CI
on: [push, pull_request]

env:
minrust: 1.46.0
minrust: 1.56.0

jobs:
test:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -9,7 +9,7 @@ readme = "README.md"
license = "MIT"
keywords = ["thread", "schedule", "priority", "pthread"]
categories = ["concurrency", "asynchronous", "os"]
edition = "2018"
edition = "2021"

[dev-dependencies]
rstest = "0.13"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -7,7 +7,7 @@ A simple library to control thread schedule policies and thread priority.
If your operating system isn't yet supported, please, create an issue.

## Minimal Rust Compiler Version
Is `1.46`. If you need any help making it possible to compile with `1.36` please reach out.
Is `1.56`. If you need any help making it possible to compile with `1.36` please reach out.

## Supported platforms
- Linux
Expand Down
60 changes: 48 additions & 12 deletions src/unix.rs
Expand Up @@ -308,13 +308,25 @@ impl ThreadPriority {
#[cfg(any(target_os = "linux", target_os = "android"))]
return Ok(NICENESS_MAX as libc::c_int);

// On other systems there is no notion of using niceness
// for just threads but for whole processes instead.
#[cfg(not(any(target_os = "linux", target_os = "android")))]
Err(Error::Priority(
"This OS doesn't support specifying this thread priority with this policy.
// On macOS/iOS, it is allowed to specify the priority
// using sched params.
if cfg!(any(target_os = "macos", target_os = "ios")) {
let max_priority = unsafe { libc::sched_get_priority_max(policy.to_posix()) };
if max_priority < 0 {
Err(Error::OS(errno()))
} else {
Ok(max_priority)
}
} else if cfg!(not(any(target_os = "linux", target_os = "android"))) {
// On other systems there is no notion of using niceness
// for just threads but for whole processes instead.
return Err(Error::Priority(
"This OS doesn't support specifying this thread priority with this policy.
Consider changing the scheduling policy.",
))
));
} else {
return Err(Error::Priority("This OS is unsupported."));
}

Check failure on line 329 in src/unix.rs

View workflow job for this annotation

GitHub Actions / clippy

unreachable expression

error: unreachable expression --> src/unix.rs:313:17 | 309 | return Ok(NICENESS_MAX as libc::c_int); | -------------------------------------- any code following this expression is unreachable ... 313 | / if cfg!(any(target_os = "macos", target_os = "ios")) { 314 | | let max_priority = unsafe { libc::sched_get_priority_max(policy.to_posix()) }; 315 | | if max_priority < 0 { 316 | | Err(Error::OS(errno())) ... | 328 | | return Err(Error::Priority("This OS is unsupported.")); 329 | | } | |_________________^ unreachable expression | note: the lint level is defined here --> src/lib.rs:146:9 | 146 | #![deny(warnings)] | ^^^^^^^^ = note: `#[deny(unreachable_code)]` implied by `#[deny(warnings)]`
}
_ => {
let max_priority = unsafe { libc::sched_get_priority_max(policy.to_posix()) };
Expand All @@ -339,14 +351,24 @@ impl ThreadPriority {
{
Ok(NICENESS_MIN as libc::c_int)

Check failure on line 352 in src/unix.rs

View workflow job for this annotation

GitHub Actions / clippy

mismatched types

error[E0308]: mismatched types --> src/unix.rs:352:21 | 345 | / match policy { 346 | | #[cfg(any(target_os = "linux", target_os = "android"))] 347 | | ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Idle) => Ok(0), 348 | | ThreadSchedulePolicy::Normal(_) => { ... | 352 | | Ok(NICENESS_MIN as libc::c_int) | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Result<i32, _>` ... | 381 | | } 382 | | } | |_________- expected this to be `()` | = note: expected unit type `()` found enum `std::result::Result<i32, _>` help: you might have meant to return this value | 352 | return Ok(NICENESS_MIN as libc::c_int); | ++++++ +
}
// On other systems there is no notion of using niceness
// for just threads but for whole processes instead.
#[cfg(not(any(target_os = "linux", target_os = "android")))]
{
Err(Error::Priority(
// On macOS/iOS, it is allowed to specify the priority
// using sched params.
if cfg!(any(target_os = "macos", target_os = "ios")) {
let min_priority = unsafe { libc::sched_get_priority_min(policy.to_posix()) };
if min_priority < 0 {
Err(Error::OS(errno()))
} else {
Ok(min_priority)
}
} else if cfg!(not(any(target_os = "linux", target_os = "android"))) {
// On other systems there is no notion of using niceness
// for just threads but for whole processes instead.
return Err(Error::Priority(
"This OS doesn't support specifying this thread priority with this policy.
Consider changing the scheduling policy.",
))
));
} else {
return Err(Error::Priority("This OS is unsupported."));
}
}
_ => {
Expand Down Expand Up @@ -412,6 +434,20 @@ impl ThreadPriority {
ThreadSchedulePolicy::Realtime(_) => {
Self::to_allowed_value_for_policy(p as i32, policy).map(|v| v as u32)
}
// XNU and the derivatives allow to change the priority
// for the SCHED_OTHER policy.
// <https://www.usenix.org/legacy/publications/library/proceedings/bsdcon02/full_papers/gerbarg/gerbarg_html/index.html>
#[cfg(all(
any(target_os = "macos", target_os = "ios"),
not(target_arch = "wasm32")
))]
ThreadSchedulePolicy::Normal(_) => {
Self::to_allowed_value_for_policy(p as i32, policy).map(|v| v as u32)
}
#[cfg(not(all(
any(target_os = "macos", target_os = "ios"),
not(target_arch = "wasm32")
)))]
ThreadSchedulePolicy::Normal(_) => {
// Mapping a [0..100] priority into niceness [-20..20] needs reversing the ratio,
// as the lowest nice is actually the highest priority.
Expand Down

0 comments on commit 1f69973

Please sign in to comment.