Skip to content

Commit

Permalink
Support iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
iddm committed Mar 5, 2023
1 parent fa86f97 commit a90c8e9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "thread-priority"
version = "0.13.1"
version = "0.14.0"
authors = ["Victor Polevoy <fx@thefx.co>"]
description = "Library for managing threads priority and schedule policies"
repository = "https://github.com/vityafx/thread-priority"
Expand All @@ -20,7 +20,7 @@ cfg-if = "1"
rustversion = "1"
bitflags = "1"

[target.'cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))'.dependencies]
[target.'cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))'.dependencies]
libc = ">=0.2.123"

[target.'cfg(windows)'.dependencies]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Is `1.46`. If you need any help making it possible to compile with `1.36` please
- OpenBSD
- NetBSD
- macOS
- iOS
- Windows

## Examples
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
#[cfg(any(
target_os = "linux",
target_os = "macos",
target_os = "ios",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "openbsd",
Expand All @@ -162,6 +163,7 @@ use std::time::Duration;
#[cfg(any(
target_os = "linux",
target_os = "macos",
target_os = "ios",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "openbsd",
Expand Down
35 changes: 30 additions & 5 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn errno() -> libc::c_int {
*libc::__errno()
} else if #[cfg(target_os = "linux")] {
*libc::__errno_location()
} else if #[cfg(any(target_os = "macos", target_os = "freebsd"))] {
} else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))] {
*libc::__error()
} else {
compile_error!("Your OS is probably not supported.")
Expand All @@ -65,7 +65,7 @@ fn set_errno(number: libc::c_int) {
*libc::__errno() = number;
} else if #[cfg(target_os = "linux")] {
*libc::__errno_location() = number;
} else if #[cfg(any(target_os = "macos", target_os = "freebsd"))] {
} else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))] {
*libc::__error() = number;
} else {
compile_error!("Your OS is probably not supported.")
Expand Down Expand Up @@ -305,7 +305,16 @@ impl ThreadPriority {
}
ThreadSchedulePolicy::Normal(_) => {
// Niceness can be used, from -20 to 19, where `-20` is the maximum.
Ok(NICENESS_MAX as libc::c_int)
#[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.
Consider changing the scheduling policy.",
))
}
_ => {
let max_priority = unsafe { libc::sched_get_priority_max(policy.to_posix()) };
Expand All @@ -326,7 +335,19 @@ impl ThreadPriority {
ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Idle) => Ok(0),
ThreadSchedulePolicy::Normal(_) => {
// Niceness can be used, from -20 to 19, where `-20` is the maximum.
Ok(NICENESS_MIN as libc::c_int)
#[cfg(any(target_os = "linux", target_os = "android"))]
{
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(
"This OS doesn't support specifying this thread priority with this policy.
Consider changing the scheduling policy.",
))
}
}
_ => {
let min_priority = unsafe { libc::sched_get_priority_min(policy.to_posix()) };
Expand Down Expand Up @@ -524,7 +545,11 @@ pub fn set_thread_priority_and_policy(
}
_ => {
let fixed_priority = priority.to_posix(policy)?;
if let ThreadSchedulePolicy::Realtime(_) = policy {
// On macOS and iOS it is possible to set the priority
// this way.
if matches!(policy, ThreadSchedulePolicy::Realtime(_))
|| cfg!(any(target_os = "macos", target_os = "ios"))
{
// If the policy is a realtime one, the priority is set via
// pthread_setschedparam.
let params = ScheduleParams {
Expand Down

0 comments on commit a90c8e9

Please sign in to comment.