Skip to content

Commit e8c8c2c

Browse files
committed
Add VxWorks support in unix module
1 parent 77360b7 commit e8c8c2c

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/unix.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use libc::SCHED_NORMAL as SCHED_OTHER;
1212
use libc::SCHED_OTHER;
1313
#[cfg(any(target_os = "linux", target_os = "android"))]
1414
use libc::{SCHED_BATCH, SCHED_IDLE};
15+
#[cfg(target_os = "vxworks")]
16+
use libc::SCHED_SPORADIC;
1517
use libc::{SCHED_FIFO, SCHED_RR};
1618

1719
use crate::{Error, ThreadPriority, ThreadPriorityValue};
@@ -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,9 @@ pub enum RealtimeThreadSchedulePolicy {
171177
Fifo,
172178
/// A round-robin policy
173179
RoundRobin,
180+
// Policy similar to Fifo
181+
#[cfg(target_os = "vxworks")]
182+
Sporadic,
174183
/// A deadline policy. Note, due to Linux expecting a pid_t and not a pthread_t, the given
175184
/// [ThreadId](struct.ThreadId) will be interpreted as a pid_t. This policy is NOT
176185
/// POSIX-compatible, so we only include it for linux targets.
@@ -186,6 +195,8 @@ impl RealtimeThreadSchedulePolicy {
186195
match self {
187196
RealtimeThreadSchedulePolicy::Fifo => SCHED_FIFO,
188197
RealtimeThreadSchedulePolicy::RoundRobin => SCHED_RR,
198+
#[cfg(target_os = "vxworks")]
199+
RealtimeThreadSchedulePolicy::Sporadic => SCHED_SPORADIC,
189200
#[cfg(all(
190201
any(target_os = "linux", target_os = "android"),
191202
not(target_arch = "wasm32")
@@ -284,6 +295,10 @@ impl ThreadSchedulePolicy {
284295
SCHED_RR => Ok(ThreadSchedulePolicy::Realtime(
285296
RealtimeThreadSchedulePolicy::RoundRobin,
286297
)),
298+
#[cfg(target_os = "vxworks")]
299+
SCHED_SPORADIC => Ok(ThreadSchedulePolicy::Realtime(
300+
RealtimeThreadSchedulePolicy::Sporadic,
301+
)),
287302
#[cfg(all(
288303
any(target_os = "linux", target_os = "android"),
289304
not(target_arch = "wasm32")
@@ -346,8 +361,8 @@ impl ThreadPriority {
346361
PriorityPolicyEdgeValueType::Maximum => NICENESS_MAX as libc::c_int,
347362
})
348363
}
349-
} else if #[cfg(any(target_os = "macos", target_os = "ios"))] {
350-
// macOS/iOS allows specifying the priority using sched params.
364+
} else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "vxworks"))] {
365+
// macOS/iOS and VxWorks allow specifying the priority using sched params.
351366
get_edge_priority(policy)
352367
} else {
353368
Err(Error::Priority(
@@ -426,7 +441,7 @@ impl ThreadPriority {
426441
// for the SCHED_OTHER policy.
427442
// <https://www.usenix.org/legacy/publications/library/proceedings/bsdcon02/full_papers/gerbarg/gerbarg_html/index.html>
428443
#[cfg(all(
429-
any(target_os = "macos", target_os = "ios"),
444+
any(target_os = "macos", target_os = "ios", target_os = "vxworks"),
430445
not(target_arch = "wasm32")
431446
))]
432447
ThreadSchedulePolicy::Normal(_) => {
@@ -571,10 +586,10 @@ pub fn set_thread_priority_and_policy(
571586
}
572587
_ => {
573588
let fixed_priority = priority.to_posix(policy)?;
574-
// On macOS and iOS it is possible to set the priority
589+
// On VxWorks, macOS and iOS it is possible to set the priority
575590
// this way.
576591
if matches!(policy, ThreadSchedulePolicy::Realtime(_))
577-
|| cfg!(any(target_os = "macos", target_os = "ios"))
592+
|| cfg!(any(target_os = "macos", target_os = "ios", target_os = "vxworks"))
578593
{
579594
// If the policy is a realtime one, the priority is set via
580595
// pthread_setschedparam.
@@ -613,6 +628,9 @@ pub fn set_thread_priority_and_policy(
613628

614629
// Normal priority threads adjust relative priority through niceness.
615630
set_errno(0);
631+
// VxWorks does not have setpriority function call. Control never
632+
// reaches this statement because of the cfg condition in if
633+
#[cfg(not(target_os = "vxworks"))]
616634
let ret = unsafe { libc::setpriority(libc::PRIO_PROCESS, 0, fixed_priority) };
617635
if ret != 0 {
618636
return Err(Error::OS(errno()));

0 commit comments

Comments
 (0)