Skip to content

Commit

Permalink
chore: freebsd
Browse files Browse the repository at this point in the history
Signed-off-by: usamoi <usamoi@outlook.com>
  • Loading branch information
usamoi committed Dec 28, 2023
1 parent e075091 commit 83e00e2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/c/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ fn main() {
println!("cargo:rerun-if-changed=src/c.h");
println!("cargo:rerun-if-changed=src/c.c");
cc::Build::new()
.compiler("/usr/bin/clang-16")
.compiler("clang-16")
.file("./src/c.c")
.opt_level(3)
.debug(true)
Expand Down
2 changes: 0 additions & 2 deletions crates/c/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(linkage)]

mod c;

#[allow(unused_imports)]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ unsafe extern "C" fn _PG_init() {
}
}

#[cfg(not(any(target_os = "linux", target_os = "macos")))]
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "freebsd")))]
compile_error!("Target is not supported.");

#[cfg(not(target_endian = "little"))]
Expand Down
66 changes: 63 additions & 3 deletions src/utils/os.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use rustix::fd::{AsFd, OwnedFd};
use rustix::mm::{MapFlags, ProtFlags};
use std::io::ErrorKind;
use std::sync::atomic::AtomicU32;
use std::sync::LazyLock;

#[cfg(target_os = "linux")]
static SUPPORT_MEMFD: LazyLock<bool> = LazyLock::new(|| {
static SUPPORT_MEMFD: std::sync::LazyLock<bool> = std::sync::LazyLock::new(|| {
use rustix::fs::MemfdFlags;
use std::io::ErrorKind;
match rustix::fs::memfd_create(".memfd.VECTORS.SUPPORT", MemfdFlags::empty()) {
Ok(_) => true,
Err(e) if e.kind() == ErrorKind::Unsupported => false,
Expand Down Expand Up @@ -139,3 +138,64 @@ pub unsafe fn mmap_populate(len: usize, fd: impl AsFd) -> std::io::Result<*mut l
)?)
}
}

#[cfg(target_os = "freebsd")]
pub unsafe fn futex_wait(futex: &AtomicU32, value: u32) {
let ptr: *const AtomicU32 = futex;
unsafe {
libc::_umtx_op(
ptr as *mut libc::c_void,
libc::UMTX_OP_WAIT_UINT,
value as libc::c_ulong,
core::ptr::null_mut(),
core::ptr::null_mut(),
);
};
}

#[cfg(target_os = "freebsd")]
pub unsafe fn futex_wake(futex: &AtomicU32) {
let ptr: *const AtomicU32 = futex;
unsafe {
libc::_umtx_op(
ptr as *mut libc::c_void,
libc::UMTX_OP_WAKE,
i32::MAX as libc::c_ulong,
core::ptr::null_mut(),
core::ptr::null_mut(),
);
};
}

#[cfg(target_os = "freebsd")]
pub fn memfd_create() -> std::io::Result<OwnedFd> {
use rustix::fs::Mode;
use rustix::fs::OFlags;
let name = format!(
".shm.VECTORS.{:x}.{:x}",
std::process::id(),
rand::random::<u32>()
);
let fd = rustix::fs::open(
&name,
OFlags::RDWR | OFlags::CREATE | OFlags::EXCL,
Mode::RUSR | Mode::WUSR,
)?;
rustix::fs::unlink(&name)?;
Ok(fd)
}

#[cfg(target_os = "freebsd")]
pub unsafe fn mmap_populate(len: usize, fd: impl AsFd) -> std::io::Result<*mut libc::c_void> {
use std::ptr::null_mut;
unsafe {
Ok(rustix::mm::mmap(
null_mut(),
len,
ProtFlags::READ | ProtFlags::WRITE,
MapFlags::SHARED,
fd,
0,
)?)
}
}

0 comments on commit 83e00e2

Please sign in to comment.