Skip to content

Commit

Permalink
feat:iouring操作封装
Browse files Browse the repository at this point in the history
  • Loading branch information
SyhanLiu committed Dec 20, 2023
1 parent 87cd6dc commit 42e92cc
Show file tree
Hide file tree
Showing 20 changed files with 378 additions and 289 deletions.
71 changes: 0 additions & 71 deletions example/learn_test.rs

This file was deleted.

4 changes: 2 additions & 2 deletions shlrt/src/buf/io_buf.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops;
use super::Slice;
use crate::buf::slice::SliceMut;
use core::ops::Bound;
use std::ops;

pub unsafe trait IoBuf: Unpin + 'static {
/// 返回读缓冲区的指针
Expand Down Expand Up @@ -273,4 +273,4 @@ fn parse_range(range: impl ops::RangeBounds<usize>, end: usize) -> (usize, usize
Bound::Unbounded => end,
};
(begin, end)
}
}
9 changes: 5 additions & 4 deletions shlrt/src/buf/io_vec_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ unsafe impl IoVecBuf for Vec<libc::iovec> {

impl From<Vec<Vec<u8>>> for VecBuf {
fn from(vv: Vec<Vec<u8>>) -> Self {
let iovecs = vv.iter().map(|v| {
libc::iovec{
let iovecs = vv
.iter()
.map(|v| libc::iovec {
iov_base: v.as_ptr() as *mut c_void,
iov_len: v.len(),
}
}).collect();
})
.collect();
Self { iovecs, raw: vv }
}
}
Expand Down
6 changes: 4 additions & 2 deletions shlrt/src/buf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ pub(crate) use vec_wrapper::{read_vec_meta, write_vec_meta};

pub(crate) fn deref(buf: &impl IoBuf) -> &[u8] {
/// 强转为切片引用
unsafe { std::slice::from_raw_parts(buf.read_ptr(), buf.bytes_init()) }
}
unsafe {
std::slice::from_raw_parts(buf.read_ptr(), buf.bytes_init())
}
}
2 changes: 1 addition & 1 deletion shlrt/src/buf/raw_buf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::ptr::null;
use super::{IoBuf, IoBufMut, IoVecBuf, IoVecBufMut};
use std::ptr::null;

/// 要保证ptr指向的内存区一定可用
pub struct RawBuf {
Expand Down
2 changes: 1 addition & 1 deletion shlrt/src/buf/slice.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{IoBuf, IoBufMut, IoVecBuf, IoVecBufMut};
use std::ops;
use super::{IoVecBuf, IoVecBufMut, IoBuf, IoBufMut};

/// 使用 [IoBuf::slice] 创建Slice
pub struct SliceMut<T> {
Expand Down
4 changes: 3 additions & 1 deletion shlrt/src/buf/vec_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ impl IoVecMeta {
return;
}
std::cmp::Ordering::Greater => {
unsafe { let _ = iovec.iov_base.add(used); };
unsafe {
let _ = iovec.iov_base.add(used);
};
iovec.iov_len -= used;
self.offset = offset;
return;
Expand Down
23 changes: 23 additions & 0 deletions shlrt/src/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::marker::PhantomData;
use std::ptr::NonNull;

pub struct RuntimeBuilder {
/// iouring中的entry数量
entries: Option<usize>,
uring_builder: io_uring::Builder,
}

impl Default for RuntimeBuilder {
fn default() -> Self {
RuntimeBuilder::new()
}
}

impl RuntimeBuilder {
pub fn new() -> Self {
Self {
entries: None,
uring_builder: io_uring::IoUring::builder(),
}
}
}
10 changes: 8 additions & 2 deletions shlrt/src/driver/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use io_uring;
use std::io;
use std::mem::ManuallyDrop;
use std::time::Duration;
use crate::driver::uring::UringInner;
use crate::scoped_thread_local;

mod op;
pub(crate) mod shared_fd;
mod uring;
mod util;

scoped_thread_local!(pub(crate) static CURRENT: Inner);

/// Core driver trait.
pub trait Driver {
Expand All @@ -29,4 +33,6 @@ pub trait Driver {
// thread_local!(pub(crate) static CURRENT: uring::ThreadLocalUring = uring::ThreadLocalUring{
// uring: std::rc::Rc::new(std::cell::UnsafeCell::new(std::ptr::null())),
// });
// TODO 写TMD的宏
// TODO 写TMD的宏

pub(crate) struct Inner(std::rc::Rc<std::cell::UnsafeCell<UringInner>>);
5 changes: 2 additions & 3 deletions shlrt/src/driver/op.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::driver;
use crate::driver::uring::Uring;
use std::io;
use crate::driver::Inner;

mod accept;

/// 封装io_uring的operation
pub(crate) struct Op<T: 'static> {
// 所属的io_uring
pub(super) driver: std::rc::Rc<std::cell::UnsafeCell<Uring>>,
pub(super) driver: Inner,
// 所属的Op队列
pub(super) index: usize,
// op操作包含的data信息
Expand Down
Loading

0 comments on commit 42e92cc

Please sign in to comment.