From a90c8e9e34ebe7142515d032f9f3a63b8bf6cf6f Mon Sep 17 00:00:00 2001 From: Victor Polevoy Date: Sun, 5 Mar 2023 16:52:33 +0100 Subject: [PATCH] Support iOS --- Cargo.toml | 4 ++-- README.md | 1 + src/lib.rs | 2 ++ src/unix.rs | 35 ++++++++++++++++++++++++++++++----- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5a28184..cb3f470 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "thread-priority" -version = "0.13.1" +version = "0.14.0" authors = ["Victor Polevoy "] description = "Library for managing threads priority and schedule policies" repository = "https://github.com/vityafx/thread-priority" @@ -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] diff --git a/README.md b/README.md index 5ecafa5..6bfbb2c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/lib.rs b/src/lib.rs index 32181fc..9060af6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -148,6 +148,7 @@ #[cfg(any( target_os = "linux", target_os = "macos", + target_os = "ios", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd", @@ -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", diff --git a/src/unix.rs b/src/unix.rs index b653c49..15802b1 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -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.") @@ -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.") @@ -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()) }; @@ -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()) }; @@ -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 {