From 1dd8d9a68d9b04c361bc8f3fef7f6ebf61d3d052 Mon Sep 17 00:00:00 2001 From: B I Mohammed Abbas Date: Fri, 18 Oct 2024 17:20:34 +0530 Subject: [PATCH] Add Vxworks support --- src/unix.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/unix.rs b/src/unix.rs index f6aad7f..996e7c6 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -12,6 +12,8 @@ use libc::SCHED_NORMAL as SCHED_OTHER; 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; use libc::{SCHED_FIFO, SCHED_RR}; use crate::{Error, ThreadPriority, ThreadPriorityValue}; @@ -51,6 +53,8 @@ fn errno() -> libc::c_int { *libc::__errno_location() } else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))] { *libc::__error() + } else if #[cfg(target_os = "vxworks")] { + libc::errnoGet() } else { compile_error!("Your OS is probably not supported.") } @@ -67,6 +71,8 @@ fn set_errno(number: libc::c_int) { *libc::__errno_location() = number; } else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))] { *libc::__error() = number; + } else if #[cfg(target_os = "vxworks")] { + let _ = libc::errnoSet(number); } else { compile_error!("Your OS is probably not supported.") } @@ -171,6 +177,9 @@ pub enum RealtimeThreadSchedulePolicy { Fifo, /// A round-robin policy RoundRobin, + // Policy similar to Fifo + #[cfg(target_os = "vxworks")] + Sporadic, /// A deadline policy. Note, due to Linux expecting a pid_t and not a pthread_t, the given /// [ThreadId](struct.ThreadId) will be interpreted as a pid_t. This policy is NOT /// POSIX-compatible, so we only include it for linux targets. @@ -186,6 +195,8 @@ impl RealtimeThreadSchedulePolicy { match self { RealtimeThreadSchedulePolicy::Fifo => SCHED_FIFO, RealtimeThreadSchedulePolicy::RoundRobin => SCHED_RR, + #[cfg(target_os = "vxworks")] + RealtimeThreadSchedulePolicy::Sporadic => SCHED_SPORADIC, #[cfg(all( any(target_os = "linux", target_os = "android"), not(target_arch = "wasm32") @@ -284,6 +295,10 @@ impl ThreadSchedulePolicy { SCHED_RR => Ok(ThreadSchedulePolicy::Realtime( RealtimeThreadSchedulePolicy::RoundRobin, )), + #[cfg(target_os = "vxworks")] + SCHED_SPORADIC => Ok(ThreadSchedulePolicy::Realtime( + RealtimeThreadSchedulePolicy::Sporadic, + )), #[cfg(all( any(target_os = "linux", target_os = "android"), not(target_arch = "wasm32") @@ -346,8 +361,8 @@ impl ThreadPriority { PriorityPolicyEdgeValueType::Maximum => NICENESS_MAX as libc::c_int, }) } - } else if #[cfg(any(target_os = "macos", target_os = "ios"))] { - // macOS/iOS allows specifying the priority using sched params. + } else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "vxworks"))] { + // macOS/iOS and VxWorks allow specifying the priority using sched params. get_edge_priority(policy) } else { Err(Error::Priority( @@ -426,7 +441,7 @@ impl ThreadPriority { // for the SCHED_OTHER policy. // #[cfg(all( - any(target_os = "macos", target_os = "ios"), + any(target_os = "macos", target_os = "ios", target_os = "vxworks"), not(target_arch = "wasm32") ))] ThreadSchedulePolicy::Normal(_) => { @@ -571,10 +586,10 @@ pub fn set_thread_priority_and_policy( } _ => { let fixed_priority = priority.to_posix(policy)?; - // On macOS and iOS it is possible to set the priority + // 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")) + || 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. @@ -613,6 +628,7 @@ pub fn set_thread_priority_and_policy( // Normal priority threads adjust relative priority through niceness. set_errno(0); + #[cfg(not(target_os = "vxworks"))] let ret = unsafe { libc::setpriority(libc::PRIO_PROCESS, 0, fixed_priority) }; if ret != 0 { return Err(Error::OS(errno()));