Skip to content

Commit 4e0f070

Browse files
VxWorks support (#45)
* VxWorks dependencies added * Add VxWorks support in unix module Update src/unix.rs for vxWorks tests Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 1135f2e commit 4e0f070

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ bitflags = "2"
2424
[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]
2525
libc = ">=0.2.123"
2626

27+
[target.'cfg(target_os = "vxworks")'.dependencies]
28+
libc = ">=0.2.161"
29+
2730
[target.'cfg(windows)'.dependencies]
2831
libc = ">=0.2.123"
2932
winapi = { version = "0.3", features = ["errhandlingapi", "processthreadsapi", "winnt", "minwindef", "winbase"] }

src/lib.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
target_os = "dragonfly",
153153
target_os = "freebsd",
154154
target_os = "openbsd",
155+
target_os = "vxworks",
155156
target_os = "netbsd",
156157
target_os = "android",
157158
target_arch = "wasm32",
@@ -167,6 +168,7 @@ use std::time::Duration;
167168
target_os = "dragonfly",
168169
target_os = "freebsd",
169170
target_os = "openbsd",
171+
target_os = "vxworks",
170172
target_os = "netbsd",
171173
target_os = "android",
172174
target_arch = "wasm32",
@@ -244,19 +246,23 @@ impl std::error::Error for Error {}
244246
pub struct ThreadPriorityValue(u8);
245247
impl ThreadPriorityValue {
246248
/// The maximum value for a thread priority.
247-
pub const MAX: u8 = 99;
249+
pub const MAX: u8 = if cfg!(target_os = "vxworks") { 255 } else { 99 };
248250
/// The minimum value for a thread priority.
249251
pub const MIN: u8 = 0;
250252
}
251253

252254
impl std::convert::TryFrom<u8> for ThreadPriorityValue {
253-
type Error = &'static str;
255+
type Error = String;
254256

255257
fn try_from(value: u8) -> Result<Self, Self::Error> {
256258
if (Self::MIN..=Self::MAX).contains(&value) {
257259
Ok(Self(value))
258260
} else {
259-
Err("The value is not in the range of [0;99]")
261+
Err(format!(
262+
"The value is not in the range of [{}; {}]",
263+
Self::MIN,
264+
Self::MAX
265+
))
260266
}
261267
}
262268
}
@@ -808,7 +814,7 @@ pub trait ThreadScopeExt<'scope> {
808814
}
809815

810816
#[rustversion::since(1.63)]
811-
impl<'scope, 'env> ThreadScopeExt<'scope> for std::thread::Scope<'scope, 'env> {
817+
impl<'scope> ThreadScopeExt<'scope> for std::thread::Scope<'scope, '_> {
812818
fn spawn_with_priority<F, T>(
813819
&'scope self,
814820
priority: ThreadPriority,

src/unix.rs

+41-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use std::convert::TryFrom;
1010
use libc::SCHED_NORMAL as SCHED_OTHER;
1111
#[cfg(not(target_os = "android"))]
1212
use libc::SCHED_OTHER;
13+
#[cfg(target_os = "vxworks")]
14+
use libc::SCHED_SPORADIC;
1315
#[cfg(any(target_os = "linux", target_os = "android"))]
1416
use libc::{SCHED_BATCH, SCHED_IDLE};
1517
use libc::{SCHED_FIFO, SCHED_RR};
@@ -51,6 +53,8 @@ fn errno() -> libc::c_int {
5153
*libc::__errno_location()
5254
} else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))] {
5355
*libc::__error()
56+
} else if #[cfg(target_os = "vxworks")] {
57+
libc::errnoGet()
5458
} else {
5559
compile_error!("Your OS is probably not supported.")
5660
}
@@ -67,6 +71,8 @@ fn set_errno(number: libc::c_int) {
6771
*libc::__errno_location() = number;
6872
} else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))] {
6973
*libc::__error() = number;
74+
} else if #[cfg(target_os = "vxworks")] {
75+
let _ = libc::errnoSet(number);
7076
} else {
7177
compile_error!("Your OS is probably not supported.")
7278
}
@@ -171,6 +177,10 @@ pub enum RealtimeThreadSchedulePolicy {
171177
Fifo,
172178
/// A round-robin policy
173179
RoundRobin,
180+
// Policy similar to Fifo
181+
/// A sporadic scheduling policy specific to VxWorks.
182+
#[cfg(target_os = "vxworks")]
183+
Sporadic,
174184
/// A deadline policy. Note, due to Linux expecting a pid_t and not a pthread_t, the given
175185
/// [ThreadId](struct.ThreadId) will be interpreted as a pid_t. This policy is NOT
176186
/// POSIX-compatible, so we only include it for linux targets.
@@ -186,6 +196,8 @@ impl RealtimeThreadSchedulePolicy {
186196
match self {
187197
RealtimeThreadSchedulePolicy::Fifo => SCHED_FIFO,
188198
RealtimeThreadSchedulePolicy::RoundRobin => SCHED_RR,
199+
#[cfg(target_os = "vxworks")]
200+
RealtimeThreadSchedulePolicy::Sporadic => SCHED_SPORADIC,
189201
#[cfg(all(
190202
any(target_os = "linux", target_os = "android"),
191203
not(target_arch = "wasm32")
@@ -284,6 +296,10 @@ impl ThreadSchedulePolicy {
284296
SCHED_RR => Ok(ThreadSchedulePolicy::Realtime(
285297
RealtimeThreadSchedulePolicy::RoundRobin,
286298
)),
299+
#[cfg(target_os = "vxworks")]
300+
SCHED_SPORADIC => Ok(ThreadSchedulePolicy::Realtime(
301+
RealtimeThreadSchedulePolicy::Sporadic,
302+
)),
287303
#[cfg(all(
288304
any(target_os = "linux", target_os = "android"),
289305
not(target_arch = "wasm32")
@@ -346,8 +362,8 @@ impl ThreadPriority {
346362
PriorityPolicyEdgeValueType::Maximum => NICENESS_MAX as libc::c_int,
347363
})
348364
}
349-
} else if #[cfg(any(target_os = "macos", target_os = "ios"))] {
350-
// macOS/iOS allows specifying the priority using sched params.
365+
} else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "vxworks"))] {
366+
// macOS/iOS and VxWorks allow specifying the priority using sched params.
351367
get_edge_priority(policy)
352368
} else {
353369
Err(Error::Priority(
@@ -426,14 +442,14 @@ impl ThreadPriority {
426442
// for the SCHED_OTHER policy.
427443
// <https://www.usenix.org/legacy/publications/library/proceedings/bsdcon02/full_papers/gerbarg/gerbarg_html/index.html>
428444
#[cfg(all(
429-
any(target_os = "macos", target_os = "ios"),
445+
any(target_os = "macos", target_os = "ios", target_os = "vxworks"),
430446
not(target_arch = "wasm32")
431447
))]
432448
ThreadSchedulePolicy::Normal(_) => {
433449
Self::to_allowed_value_for_policy(p as i32, policy).map(|v| v as u32)
434450
}
435451
#[cfg(not(all(
436-
any(target_os = "macos", target_os = "ios"),
452+
any(target_os = "macos", target_os = "ios", target_os = "vxworks"),
437453
not(target_arch = "wasm32")
438454
)))]
439455
ThreadSchedulePolicy::Normal(_) => {
@@ -571,10 +587,14 @@ pub fn set_thread_priority_and_policy(
571587
}
572588
_ => {
573589
let fixed_priority = priority.to_posix(policy)?;
574-
// On macOS and iOS it is possible to set the priority
590+
// On VxWorks, macOS and iOS it is possible to set the priority
575591
// this way.
576592
if matches!(policy, ThreadSchedulePolicy::Realtime(_))
577-
|| cfg!(any(target_os = "macos", target_os = "ios"))
593+
|| cfg!(any(
594+
target_os = "macos",
595+
target_os = "ios",
596+
target_os = "vxworks"
597+
))
578598
{
579599
// If the policy is a realtime one, the priority is set via
580600
// pthread_setschedparam.
@@ -596,6 +616,20 @@ pub fn set_thread_priority_and_policy(
596616
e => Err(Error::OS(e)),
597617
}
598618
} else {
619+
//VxWorks does not have set priority function
620+
#[cfg(target_os = "vxworks")]
621+
unsafe fn setpriority(
622+
_which: u32,
623+
_who: u32,
624+
_priority: libc::c_int,
625+
) -> libc::c_int {
626+
set_errno(libc::ENOSYS);
627+
-1
628+
}
629+
630+
#[cfg(not(target_os = "vxworks"))]
631+
use libc::setpriority;
632+
599633
// Normal priority threads must be set with static priority 0.
600634
let params = ScheduleParams { sched_priority: 0 }.into_posix();
601635

@@ -613,7 +647,7 @@ pub fn set_thread_priority_and_policy(
613647

614648
// Normal priority threads adjust relative priority through niceness.
615649
set_errno(0);
616-
let ret = unsafe { libc::setpriority(libc::PRIO_PROCESS, 0, fixed_priority) };
650+
let ret = unsafe { setpriority(libc::PRIO_PROCESS, 0, fixed_priority) };
617651
if ret != 0 {
618652
return Err(Error::OS(errno()));
619653
}

tests/unix.rs

+13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ fn get_and_set_priority_with_normal_policies(
4646
#[cfg(any(
4747
target_os = "macos",
4848
target_os = "openbsd",
49+
target_os = "vxworks",
4950
target_os = "freebsd",
5051
target_os = "netbsd"
5152
))]
@@ -64,9 +65,20 @@ fn get_and_set_priority_with_normal_policies(
6465
#[case(ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Idle), 0..=0)]
6566
#[cfg(target_os = "linux")]
6667
#[case(ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Batch), -20..=19)]
68+
#[cfg(not(target_os = "vxworks"))]
6769
#[case(ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Other), -20..=19)]
70+
#[cfg(not(target_os = "vxworks"))]
6871
#[case(ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::Fifo), 0..=99)]
72+
#[cfg(not(target_os = "vxworks"))]
6973
#[case(ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::RoundRobin), 0..=99)]
74+
#[cfg(target_os = "vxworks")]
75+
#[case(ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Other), 0.=255)]
76+
#[cfg(target_os = "vxworks")]
77+
#[case(ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::Fifo), 0..=255)]
78+
#[cfg(target_os = "vxworks")]
79+
#[case(ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::RoundRobin), 0..=255)]
80+
#[cfg(target_os = "vxworks")]
81+
#[case(ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::Sporadic), 0..=255)]
7082
fn check_min_and_max_priority_values(
7183
#[case] policy: ThreadSchedulePolicy,
7284
#[case] posix_range: std::ops::RangeInclusive<i32>,
@@ -107,6 +119,7 @@ fn set_priority_with_normal_policy_but_with_invalid_value(#[case] policy: Thread
107119
#[cfg(any(
108120
target_os = "macos",
109121
target_os = "openbsd",
122+
target_os = "vxworks",
110123
target_os = "freebsd",
111124
target_os = "netbsd"
112125
))]

0 commit comments

Comments
 (0)