Skip to content

Commit c85bec7

Browse files
committed
refactor!: use rustix
1 parent e2d2960 commit c85bec7

63 files changed

Lines changed: 1018 additions & 1752 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ synchrony = "0.1.7"
7979
thin-cell = "0.2.1"
8080
slotmap = "1.1.1"
8181
crossfire = "3.1.5"
82+
rustix = { version = "1.1.4", default-features = false }
8283

8384
[profile.bench]
8485
debug = true

compio-driver/Cargo.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ synchrony = { workspace = true, features = ["waker_slot"] }
3030
bitflags = { version = "2.11.0" }
3131
paste = { workspace = true }
3232
mod_use = "0.2.3"
33+
rustix = { workspace = true, features = ["net"] }
3334

3435
# Windows specific dependencies
3536
[target.'cfg(windows)'.dependencies]
@@ -53,16 +54,17 @@ compile_warning = { version = "0.1.0", optional = true }
5354
io-uring = { version = "0.7.0", optional = true }
5455
once_cell = { workspace = true, optional = true }
5556
polling = { version = "3.3.0", optional = true }
57+
rustix = { workspace = true, features = ["linux_5_11"] }
5658

5759
# Other platform dependencies
58-
[target.'cfg(all(not(target_os = "linux"), unix))'.dependencies]
60+
[target.'cfg(all(unix, not(target_os = "linux")))'.dependencies]
5961
polling = "3.3.0"
6062

6163
[target.'cfg(unix)'.dependencies]
6264
smallvec = { workspace = true, features = ["union"] }
6365
crossbeam-queue = { workspace = true }
6466
libc = { workspace = true }
65-
nix = { workspace = true, features = ["fs"] }
67+
rustix = { workspace = true, features = ["fs", "pipe"] }
6668

6769
[dev-dependencies]
6870
nix = { workspace = true, features = ["mman"] }
@@ -73,7 +75,8 @@ cfg_aliases = { workspace = true }
7375
[features]
7476
default = ["io-uring"]
7577
io-uring = [
76-
"nix/mman",
78+
"rustix/mm",
79+
"rustix/event",
7780
"dep:io-uring",
7881
"dep:once_cell",
7982
]

compio-driver/build.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@ fn main() {
44
cfg_aliases! {
55
// Feature
66
aio: { any(freebsd, solarish) },
7-
datasync: { any(
8-
target_os = "android",
9-
target_os = "freebsd",
10-
target_os = "fuchsia",
11-
target_os = "illumos",
12-
target_os = "linux",
13-
target_os = "netbsd"
14-
) },
7+
datasync: { all(unix, not(apple)) },
158

169
// Platform
10+
apple : { target_os = "macos" },
1711
linux_all: { any(target_os = "linux", target_os = "android") },
1812
gnulinux: { all(target_os = "linux", target_env = "gnu") },
1913
freebsd: { target_os = "freebsd" },

compio-driver/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ macro_rules! syscall {
5151
if res == -1 {
5252
Err(::std::io::Error::last_os_error())
5353
} else {
54-
Ok(res)
54+
Ok(res as usize)
5555
}
5656
}};
5757
}

compio-driver/src/sys/buffer_pool/iour.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::{
22
io,
3-
num::{NonZeroU16, NonZeroUsize},
4-
ptr::NonNull,
3+
num::NonZeroU16,
4+
ptr::{NonNull, null_mut},
55
slice,
66
sync::atomic::Ordering,
77
};
88

99
use io_uring::types::BufRingEntry;
10-
use nix::sys::mman::{MapFlags, ProtFlags, mmap_anonymous, munmap};
10+
use rustix::mm::{MapFlags, ProtFlags, mmap_anonymous, munmap};
1111
use synchrony::unsync::atomic::AtomicU16;
1212

1313
use crate::{
@@ -22,7 +22,7 @@ pub(in crate::sys) struct BufControl {
2222
/// Number of entries
2323
len: NonZeroU16,
2424
/// Total size of the mmap
25-
size: NonZeroUsize,
25+
size: usize,
2626
}
2727

2828
assert_not_impl!(BufControl, Send);
@@ -49,12 +49,13 @@ impl BufControl {
4949
let driver = driver.as_iour_mut().expect("Should be iour");
5050

5151
let len = NonZeroU16::new(bufs.len() as u16).expect("Empty buffers");
52-
let size = NonZeroUsize::new(len.get() as usize * size_of::<BufRingEntry>())
53-
.expect("Shouldn't overflow");
52+
let size = len.get() as usize * size_of::<BufRingEntry>();
5453

55-
let prot = ProtFlags::PROT_READ | ProtFlags::PROT_WRITE;
56-
let mflags = MapFlags::MAP_PRIVATE;
57-
let ptr = unsafe { mmap_anonymous(None, size, prot, mflags) }?.cast::<BufRingEntry>();
54+
let prot = ProtFlags::READ | ProtFlags::WRITE;
55+
let mflags = MapFlags::PRIVATE;
56+
let ptr = NonNull::new(unsafe { mmap_anonymous(null_mut(), size, prot, mflags) }?)
57+
.expect("mmap failed")
58+
.cast::<BufRingEntry>();
5859

5960
let mut this = Self { ptr, len, size };
6061

@@ -113,7 +114,7 @@ impl BufControl {
113114
.inner()
114115
.submitter()
115116
.unregister_buf_ring(Self::BUF_GROUP)?;
116-
unsafe { munmap(self.ptr.cast(), self.size.get()) }?;
117+
unsafe { munmap(self.ptr.cast().as_ptr(), self.size) }?;
117118

118119
Ok(())
119120
}

compio-driver/src/sys/driver/iocp/op.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub unsafe trait OpCode {
1616
///
1717
/// Caller must guarantee that during the lifetime of `ctrl`, `Self` is
1818
/// unmoved and valid.
19-
unsafe fn init(&mut self, ctrl: &mut Self::Control);
19+
unsafe fn init(&mut self, _: &mut Self::Control) {}
2020

2121
/// Determines that the operation is really overlapped defined by
2222
/// Windows API. If not, the driver will try to operate it in

compio-driver/src/sys/driver/iour/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use crate::{
3434
AsyncifyPool, DriverType, Entry, ProactorBuilder,
3535
key::{BorrowedKey, ErasedKey},
3636
panic::catch_unwind_io,
37-
syscall,
3837
};
3938

4039
/// Low-level driver of io-uring.

compio-driver/src/sys/driver/iour/notify.rs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use rustix::event::{EventfdFlags, eventfd};
2+
13
use super::*;
24

35
#[derive(Debug)]
@@ -8,40 +10,36 @@ pub(super) struct Notifier {
810
impl Notifier {
911
/// Create a new notifier.
1012
pub fn new() -> io::Result<Self> {
11-
let fd = syscall!(libc::eventfd(0, libc::EFD_CLOEXEC | libc::EFD_NONBLOCK))?;
12-
let fd = unsafe { OwnedFd::from_raw_fd(fd) };
13+
let fd = eventfd(0, EventfdFlags::CLOEXEC | EventfdFlags::NONBLOCK)?;
14+
1315
Ok(Self {
1416
notify: Arc::new(Notify::new(fd)),
1517
})
1618
}
1719

1820
pub fn clear(&self) -> io::Result<()> {
19-
loop {
20-
let mut buffer = [0u64];
21-
let res = syscall!(libc::read(
22-
self.as_raw_fd(),
23-
buffer.as_mut_ptr().cast(),
24-
std::mem::size_of::<u64>()
25-
));
26-
match res {
27-
Ok(len) => {
28-
debug_assert_eq!(len, std::mem::size_of::<u64>() as _);
29-
break Ok(());
30-
}
31-
// Clear the next time:)
32-
Err(e) if e.kind() == io::ErrorKind::WouldBlock => break Ok(()),
33-
// Just like read_exact
34-
Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
35-
Err(e) => break Err(e),
36-
}
37-
}
21+
const LEN: usize = std::mem::size_of::<u64>();
22+
23+
let mut buffer = [0u8; LEN];
24+
25+
let res = poll_io(|| rustix::io::read(self, &mut buffer))?;
26+
27+
debug_assert!(matches!(res, Poll::Pending | Poll::Ready(LEN)));
28+
29+
Ok(())
3830
}
3931

4032
pub fn waker(&self) -> Waker {
4133
Waker::from(self.notify.clone())
4234
}
4335
}
4436

37+
impl AsFd for Notifier {
38+
fn as_fd(&self) -> BorrowedFd<'_> {
39+
self.notify.fd.as_fd()
40+
}
41+
}
42+
4543
impl AsRawFd for Notifier {
4644
fn as_raw_fd(&self) -> RawFd {
4745
self.notify.fd.as_raw_fd()
@@ -61,12 +59,8 @@ impl Notify {
6159

6260
/// Notify the inner driver.
6361
pub fn notify(&self) -> io::Result<()> {
64-
let data = 1u64;
65-
syscall!(libc::write(
66-
self.fd.as_raw_fd(),
67-
&data as *const _ as *const _,
68-
std::mem::size_of::<u64>(),
69-
))?;
62+
rustix::io::write(&self.fd, &u64::to_be_bytes(1))?;
63+
7064
Ok(())
7165
}
7266
}

compio-driver/src/sys/driver/iour/op.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub unsafe trait OpCode {
3333
///
3434
/// Caller must guarantee that during the lifetime of `ctrl`, `Self` is
3535
/// unmoved and valid.
36-
unsafe fn init(&mut self, ctrl: &mut Self::Control);
36+
unsafe fn init(&mut self, _: &mut Self::Control) {}
3737

3838
/// Create submission entry.
3939
fn create_entry(&mut self, _: &mut Self::Control) -> OpEntry;

compio-driver/src/sys/driver/poll/mod.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ use crate::{
1818
sys::{extra::PollExtra, prelude::*},
1919
};
2020

21-
/// Meta of AIO operations.
22-
#[cfg(aio)]
23-
#[derive(Debug, Clone, Copy)]
24-
#[doc(hidden)]
25-
pub struct AioControl {
26-
/// Pointer of the control block.
27-
pub aiocbp: NonNull<libc::aiocb>,
28-
/// The aio_* submit function.
29-
pub submit: unsafe extern "C" fn(*mut libc::aiocb) -> i32,
30-
}
31-
3221
#[derive(Debug, Default)]
3322
struct FdQueue {
3423
read_queue: VecDeque<ErasedKey>,
@@ -330,7 +319,7 @@ impl Driver {
330319
Poll::Pending
331320
}
332321
#[cfg(aio)]
333-
Decision::Aio(AioControl { mut aiocbp, submit }) => {
322+
Decision::Aio(AioArg { mut aiocbp, submit }) => {
334323
let aiocb = unsafe { aiocbp.as_mut() };
335324
let user_data = key.as_raw();
336325
#[cfg(freebsd)]
@@ -491,9 +480,7 @@ impl Driver {
491480
trace!("op {} is not completed", key.as_raw());
492481
continue;
493482
}
494-
_ => {
495-
syscall!(libc::aio_return(aiocbp.as_ptr())).map(|res| res as usize)
496-
}
483+
_ => syscall!(libc::aio_return(aiocbp.as_ptr())),
497484
};
498485
let key = unsafe { ErasedKey::from_raw(event.key) };
499486
Entry::new(key, res).notify()

0 commit comments

Comments
 (0)