Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/backend/libc/process/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
pub(crate) mod cpu_set;
#[cfg(not(windows))]
pub(crate) mod syscalls;
pub(crate) mod types;
Expand Down
96 changes: 0 additions & 96 deletions src/backend/libc/process/syscalls.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! libc syscalls supporting `rustix::process`.

#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
use super::types::RawCpuSet;
use crate::backend::c;
#[cfg(not(any(target_os = "wasi", target_os = "fuchsia")))]
use crate::backend::conv::borrowed_fd;
Expand All @@ -19,8 +17,6 @@ use crate::backend::conv::ret_discarded_char_ptr;
use crate::backend::conv::ret_infallible;
#[cfg(not(target_os = "wasi"))]
use crate::backend::conv::ret_pid_t;
#[cfg(linux_kernel)]
use crate::backend::conv::ret_u32;
#[cfg(all(feature = "alloc", not(target_os = "wasi")))]
use crate::backend::conv::ret_usize;
use crate::backend::conv::{ret, ret_c_int};
Expand All @@ -46,8 +42,6 @@ use crate::process::Signal;
target_os = "wasi"
)))]
use crate::process::Uid;
#[cfg(linux_kernel)]
use crate::process::{Cpuid, MembarrierCommand, MembarrierQuery};
#[cfg(not(any(target_os = "espidf", target_os = "vita", target_os = "wasi")))]
use crate::process::{RawPid, WaitOptions, WaitStatus};
#[cfg(not(any(
Expand All @@ -72,14 +66,6 @@ use {
super::super::conv::ret_owned_fd, crate::process::PidfdFlags, crate::process::PidfdGetfdFlags,
};

#[cfg(any(linux_kernel, target_os = "dragonfly"))]
#[inline]
pub(crate) fn sched_getcpu() -> usize {
let r = unsafe { libc::sched_getcpu() };
debug_assert!(r >= 0);
r as usize
}

#[cfg(feature = "fs")]
#[cfg(not(target_os = "wasi"))]
pub(crate) fn chdir(path: &CStr) -> io::Result<()> {
Expand All @@ -103,57 +89,6 @@ pub(crate) fn getcwd(buf: &mut [MaybeUninit<u8>]) -> io::Result<()> {
unsafe { ret_discarded_char_ptr(c::getcwd(buf.as_mut_ptr().cast(), buf.len())) }
}

// The `membarrier` syscall has a third argument, but it's only used when
// the `flags` argument is `MEMBARRIER_CMD_FLAG_CPU`.
#[cfg(linux_kernel)]
syscall! {
fn membarrier_all(
cmd: c::c_int,
flags: c::c_uint
) via SYS_membarrier -> c::c_int
}

#[cfg(linux_kernel)]
pub(crate) fn membarrier_query() -> MembarrierQuery {
// glibc does not have a wrapper for `membarrier`; [the documentation]
// says to use `syscall`.
//
// [the documentation]: https://man7.org/linux/man-pages/man2/membarrier.2.html#NOTES
const MEMBARRIER_CMD_QUERY: u32 = 0;
unsafe {
match ret_u32(membarrier_all(MEMBARRIER_CMD_QUERY as i32, 0)) {
Ok(query) => MembarrierQuery::from_bits_retain(query),
Err(_) => MembarrierQuery::empty(),
}
}
}

#[cfg(linux_kernel)]
pub(crate) fn membarrier(cmd: MembarrierCommand) -> io::Result<()> {
unsafe { ret(membarrier_all(cmd as i32, 0)) }
}

#[cfg(linux_kernel)]
pub(crate) fn membarrier_cpu(cmd: MembarrierCommand, cpu: Cpuid) -> io::Result<()> {
const MEMBARRIER_CMD_FLAG_CPU: u32 = 1;

syscall! {
fn membarrier_cpu(
cmd: c::c_int,
flags: c::c_uint,
cpu_id: c::c_int
) via SYS_membarrier -> c::c_int
}

unsafe {
ret(membarrier_cpu(
cmd as i32,
MEMBARRIER_CMD_FLAG_CPU,
bitcast!(cpu.as_raw()),
))
}
}

#[cfg(not(target_os = "wasi"))]
#[inline]
#[must_use]
Expand Down Expand Up @@ -189,37 +124,6 @@ pub(crate) fn getpgrp() -> Pid {
}
}

#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
#[inline]
pub(crate) fn sched_getaffinity(pid: Option<Pid>, cpuset: &mut RawCpuSet) -> io::Result<()> {
unsafe {
ret(c::sched_getaffinity(
Pid::as_raw(pid) as _,
core::mem::size_of::<RawCpuSet>(),
cpuset,
))
}
}

#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
#[inline]
pub(crate) fn sched_setaffinity(pid: Option<Pid>, cpuset: &RawCpuSet) -> io::Result<()> {
unsafe {
ret(c::sched_setaffinity(
Pid::as_raw(pid) as _,
core::mem::size_of::<RawCpuSet>(),
cpuset,
))
}
}

#[inline]
pub(crate) fn sched_yield() {
unsafe {
let _ = c::sched_yield();
}
}

#[cfg(not(target_os = "wasi"))]
#[cfg(feature = "fs")]
#[inline]
Expand Down
61 changes: 7 additions & 54 deletions src/backend/libc/process/types.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,12 @@
#[cfg(not(any(target_os = "espidf", target_os = "vita")))]
#[cfg(not(any(
target_os = "espidf",
target_os = "fuchsia",
target_os = "redox",
target_os = "vita",
target_os = "wasi"
)))]
use crate::backend::c;

/// A command for use with [`membarrier`] and [`membarrier_cpu`].
///
/// For `MEMBARRIER_CMD_QUERY`, see [`membarrier_query`].
///
/// [`membarrier`]: crate::process::membarrier
/// [`membarrier_cpu`]: crate::process::membarrier_cpu
/// [`membarrier_query`]: crate::process::membarrier_query
#[cfg(linux_kernel)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(u32)]
pub enum MembarrierCommand {
/// `MEMBARRIER_CMD_GLOBAL`
#[doc(alias = "Shared")]
#[doc(alias = "MEMBARRIER_CMD_SHARED")]
Global = c::MEMBARRIER_CMD_GLOBAL as u32,
/// `MEMBARRIER_CMD_GLOBAL_EXPEDITED`
GlobalExpedited = c::MEMBARRIER_CMD_GLOBAL_EXPEDITED as u32,
/// `MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED`
RegisterGlobalExpedited = c::MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED as u32,
/// `MEMBARRIER_CMD_PRIVATE_EXPEDITED`
PrivateExpedited = c::MEMBARRIER_CMD_PRIVATE_EXPEDITED as u32,
/// `MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED`
RegisterPrivateExpedited = c::MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED as u32,
/// `MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE`
PrivateExpeditedSyncCore = c::MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE as u32,
/// `MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE`
RegisterPrivateExpeditedSyncCore =
c::MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE as u32,
/// `MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ` (since Linux 5.10)
PrivateExpeditedRseq = c::MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ as u32,
/// `MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ` (since Linux 5.10)
RegisterPrivateExpeditedRseq = c::MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ as u32,
}

/// A resource value for use with [`getrlimit`], [`setrlimit`], and
/// [`prlimit`].
///
Expand Down Expand Up @@ -149,24 +121,5 @@ impl Resource {
pub const Rss: Self = Self::As;
}

/// A CPU identifier as a raw integer.
#[cfg(linux_kernel)]
pub type RawCpuid = u32;
#[cfg(freebsdlike)]
pub type RawId = c::id_t;

#[cfg(any(linux_kernel, target_os = "fuchsia"))]
pub(crate) type RawCpuSet = c::cpu_set_t;
#[cfg(freebsdlike)]
pub(crate) type RawCpuSet = c::cpuset_t;

#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
#[inline]
pub(crate) fn raw_cpu_set_new() -> RawCpuSet {
let mut set = unsafe { core::mem::zeroed() };
super::cpu_set::CPU_ZERO(&mut set);
set
}

#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
pub(crate) const CPU_SETSIZE: usize = c::CPU_SETSIZE as usize;
File renamed without changes.
3 changes: 3 additions & 0 deletions src/backend/libc/thread/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
pub(crate) mod cpu_set;
#[cfg(linux_kernel)]
pub(crate) mod futex;
#[cfg(not(windows))]
pub(crate) mod syscalls;
pub(crate) mod types;
99 changes: 97 additions & 2 deletions src/backend/libc/thread/syscalls.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//! libc syscalls supporting `rustix::thread`.

#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
use super::types::RawCpuSet;
use crate::backend::c;
use crate::backend::conv::ret;
use crate::io;
#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
use crate::pid::Pid;
#[cfg(not(any(
apple,
freebsdlike,
Expand All @@ -15,6 +19,8 @@ use crate::io;
target_os = "wasi",
)))]
use crate::thread::ClockId;
#[cfg(linux_kernel)]
use crate::thread::{Cpuid, MembarrierCommand, MembarrierQuery};
#[cfg(not(target_os = "redox"))]
use crate::thread::{NanosleepRelativeResult, Timespec};
#[cfg(all(target_env = "gnu", fix_y2038))]
Expand All @@ -30,9 +36,8 @@ use core::mem::MaybeUninit;
use core::sync::atomic::AtomicU32;
#[cfg(linux_kernel)]
use {
crate::backend::conv::{borrowed_fd, ret_c_int, ret_usize},
crate::backend::conv::{borrowed_fd, ret_c_int, ret_u32, ret_usize},
crate::fd::BorrowedFd,
crate::pid::Pid,
crate::thread::futex,
crate::utils::as_mut_ptr,
};
Expand Down Expand Up @@ -646,3 +651,93 @@ pub(crate) fn setgroups_thread(groups: &[crate::ugid::Gid]) -> io::Result<()> {
}
ret(unsafe { setgroups(groups.len(), groups.as_ptr().cast()) })
}

#[cfg(any(linux_kernel, target_os = "dragonfly"))]
#[inline]
pub(crate) fn sched_getcpu() -> usize {
let r = unsafe { libc::sched_getcpu() };
debug_assert!(r >= 0);
r as usize
}

#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
#[inline]
pub(crate) fn sched_getaffinity(pid: Option<Pid>, cpuset: &mut RawCpuSet) -> io::Result<()> {
unsafe {
ret(c::sched_getaffinity(
Pid::as_raw(pid) as _,
core::mem::size_of::<RawCpuSet>(),
cpuset,
))
}
}

#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
#[inline]
pub(crate) fn sched_setaffinity(pid: Option<Pid>, cpuset: &RawCpuSet) -> io::Result<()> {
unsafe {
ret(c::sched_setaffinity(
Pid::as_raw(pid) as _,
core::mem::size_of::<RawCpuSet>(),
cpuset,
))
}
}

#[inline]
pub(crate) fn sched_yield() {
unsafe {
let _ = c::sched_yield();
}
}

// The `membarrier` syscall has a third argument, but it's only used when
// the `flags` argument is `MEMBARRIER_CMD_FLAG_CPU`.
#[cfg(linux_kernel)]
syscall! {
fn membarrier_all(
cmd: c::c_int,
flags: c::c_uint
) via SYS_membarrier -> c::c_int
}

#[cfg(linux_kernel)]
pub(crate) fn membarrier_query() -> MembarrierQuery {
// glibc does not have a wrapper for `membarrier`; [the documentation]
// says to use `syscall`.
//
// [the documentation]: https://man7.org/linux/man-pages/man2/membarrier.2.html#NOTES
const MEMBARRIER_CMD_QUERY: u32 = 0;
unsafe {
match ret_u32(membarrier_all(MEMBARRIER_CMD_QUERY as i32, 0)) {
Ok(query) => MembarrierQuery::from_bits_retain(query),
Err(_) => MembarrierQuery::empty(),
}
}
}

#[cfg(linux_kernel)]
pub(crate) fn membarrier(cmd: MembarrierCommand) -> io::Result<()> {
unsafe { ret(membarrier_all(cmd as i32, 0)) }
}

#[cfg(linux_kernel)]
pub(crate) fn membarrier_cpu(cmd: MembarrierCommand, cpu: Cpuid) -> io::Result<()> {
const MEMBARRIER_CMD_FLAG_CPU: u32 = 1;

syscall! {
fn membarrier_cpu(
cmd: c::c_int,
flags: c::c_uint,
cpu_id: c::c_int
) via SYS_membarrier -> c::c_int
}

unsafe {
ret(membarrier_cpu(
cmd as i32,
MEMBARRIER_CMD_FLAG_CPU,
bitcast!(cpu.as_raw()),
))
}
}
Loading