Skip to content

Commit

Permalink
VxWorks Add tests and fix thread priority
Browse files Browse the repository at this point in the history
  • Loading branch information
biabbas committed Oct 30, 2024
1 parent fca7c77 commit 45f18da
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,23 @@ impl std::error::Error for Error {}
pub struct ThreadPriorityValue(u8);
impl ThreadPriorityValue {
/// The maximum value for a thread priority.
pub const MAX: u8 = 99;
pub const MAX: u8 = if cfg!(target_os = "vxworks") { 255 } else { 99 };
/// The minimum value for a thread priority.
pub const MIN: u8 = 0;
}

impl std::convert::TryFrom<u8> for ThreadPriorityValue {
type Error = &'static str;
type Error = String;

fn try_from(value: u8) -> Result<Self, Self::Error> {
if (Self::MIN..=Self::MAX).contains(&value) {
Ok(Self(value))
} else {
Err("The value is not in the range of [0;99]")
Err(format!(
"The value is not in the range of [{}; {}]",
Self::MIN,
Self::MAX
))
}
}
}
Expand Down Expand Up @@ -810,7 +814,7 @@ pub trait ThreadScopeExt<'scope> {
}

#[rustversion::since(1.63)]
impl<'scope, 'env> ThreadScopeExt<'scope> for std::thread::Scope<'scope, 'env> {
impl<'scope> ThreadScopeExt<'scope> for std::thread::Scope<'scope, '_> {
fn spawn_with_priority<F, T>(
&'scope self,
priority: ThreadPriority,
Expand Down
16 changes: 12 additions & 4 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use std::convert::TryFrom;
use libc::SCHED_NORMAL as SCHED_OTHER;
#[cfg(not(target_os = "android"))]
use libc::SCHED_OTHER;
#[cfg(any(target_os = "linux", target_os = "android"))]
use libc::{SCHED_BATCH, SCHED_IDLE};
#[cfg(target_os = "vxworks")]
use libc::SCHED_SPORADIC;
#[cfg(any(target_os = "linux", target_os = "android"))]
use libc::{SCHED_BATCH, SCHED_IDLE};
use libc::{SCHED_FIFO, SCHED_RR};

use crate::{Error, ThreadPriority, ThreadPriorityValue};
Expand Down Expand Up @@ -590,7 +590,11 @@ pub fn set_thread_priority_and_policy(
// On VxWorks, 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", target_os = "vxworks"))
|| cfg!(any(
target_os = "macos",
target_os = "ios",
target_os = "vxworks"
))
{
// If the policy is a realtime one, the priority is set via
// pthread_setschedparam.
Expand All @@ -614,7 +618,11 @@ pub fn set_thread_priority_and_policy(
} else {
//VxWorks does not have set priority function
#[cfg(target_os = "vxworks")]
unsafe fn setpriority( _which: u32, _who: u32, _priority: libc::c_int) -> libc::c_int {
unsafe fn setpriority(
_which: u32,
_who: u32,
_priority: libc::c_int,
) -> libc::c_int {
panic!("Set priority function not supported by vxworks");
}

Expand Down
2 changes: 2 additions & 0 deletions tests/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fn get_and_set_priority_with_normal_policies(
#[cfg(any(
target_os = "macos",
target_os = "openbsd",
target_os = "vxworks",
target_os = "freebsd",
target_os = "netbsd"
))]
Expand Down Expand Up @@ -107,6 +108,7 @@ fn set_priority_with_normal_policy_but_with_invalid_value(#[case] policy: Thread
#[cfg(any(
target_os = "macos",
target_os = "openbsd",
target_os = "vxworks",
target_os = "freebsd",
target_os = "netbsd"
))]
Expand Down

0 comments on commit 45f18da

Please sign in to comment.