Skip to content
Closed
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: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mio = { version = "1.0", default-features = false }

cfg-if = "1.0.0"
polling = "2.8.0"
educe = "0.6.0"

libc = "0.2"
rand = "0.8"
Expand All @@ -50,7 +51,6 @@ once_cell = "1"
dashmap = "6"
num_cpus = "1"
uuid = "1"
derivative = "2"
tempfile = "3"
cc = "1"
syn = "2"
Expand Down
4 changes: 2 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ uuid = { workspace = true, features = [
"v4",
"fast-rng",
], optional = true }
derivative = { workspace = true, optional = true }
educe = { workspace = true, optional = true }
core_affinity = { workspace = true, optional = true }
crossbeam-utils = { workspace = true, optional = true }
psm.workspace = true
Expand Down Expand Up @@ -77,7 +77,7 @@ backtrace.workspace = true
log = ["tracing", "tracing-subscriber", "time"]

# low-level raw coroutine
korosensei = ["corosensei", "uuid", "nix/pthread", "derivative"]
korosensei = ["corosensei", "uuid", "nix/pthread", "educe"]

# Provide preemptive scheduling implementation.
# Enable for default.
Expand Down
7 changes: 3 additions & 4 deletions core/src/co_pool/task.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use crate::catch;
use derivative::Derivative;

/// 做C兼容时会用到
pub type UserTaskFunc = extern "C" fn(usize) -> usize;

/// The task impls.
#[repr(C)]
#[derive(Derivative)]
#[derivative(Debug)]
#[derive(educe::Educe)]
#[educe(Debug)]
pub struct Task<'t> {
name: String,
#[derivative(Debug = "ignore")]
#[educe(Debug(ignore))]
func: Box<dyn FnOnce(Option<usize>) -> Option<usize> + 't>,
param: Option<usize>,
}
Expand Down
7 changes: 3 additions & 4 deletions core/src/common/timer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::impl_display_by_debug;
use derivative::Derivative;
use std::collections::{BTreeMap, VecDeque};
use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicUsize, Ordering};
Expand Down Expand Up @@ -62,11 +61,11 @@ impl_display_by_debug!(TimerEntry<T>);

/// A queue for managing multiple `TimerEntry`.
#[repr(C)]
#[derive(Derivative)]
#[derivative(Debug, Eq, PartialEq)]
#[derive(educe::Educe)]
#[educe(Debug, Eq, PartialEq)]
pub struct TimerList<T> {
inner: BTreeMap<u64, TimerEntry<T>>,
#[derivative(PartialEq = "ignore")]
#[educe(PartialEq(ignore))]
total: AtomicUsize,
}

Expand Down
8 changes: 4 additions & 4 deletions core/src/coroutine/suspender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::collections::VecDeque;
use std::time::Duration;

thread_local! {
#[allow(clippy::missing_const_for_thread_local)]
static TIMESTAMP: RefCell<VecDeque<u64>> = const { RefCell::new(VecDeque::new()) };
}

Expand Down Expand Up @@ -52,14 +53,13 @@ pub use korosensei::Suspender;
#[cfg(feature = "korosensei")]
mod korosensei {
use corosensei::Yielder;
use derivative::Derivative;

/// Ths suspender implemented for coroutine.
#[repr(C)]
#[derive(Derivative)]
#[derivative(Debug)]
#[derive(educe::Educe)]
#[educe(Debug)]
pub struct Suspender<'s, Param, Yield> {
#[derivative(Debug = "ignore")]
#[educe(Debug(ignore))]
inner: &'s Yielder<Param, Yield>,
}

Expand Down
42 changes: 26 additions & 16 deletions core/src/net/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cfg_if::cfg_if! {
if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
use libc::{epoll_event, iovec, msghdr, off_t, size_t, sockaddr, socklen_t, ssize_t};
use dashmap::DashMap;
use std::ffi::c_longlong;
}
}

Expand All @@ -36,7 +37,7 @@ pub(crate) struct EventLoop<'e> {
operator: crate::net::operator::Operator<'e>,
#[allow(clippy::type_complexity)]
#[cfg(all(target_os = "linux", feature = "io_uring"))]
syscall_wait_table: DashMap<usize, Arc<(Mutex<Option<ssize_t>>, Condvar)>>,
syscall_wait_table: DashMap<usize, Arc<(Mutex<Option<c_longlong>>, Condvar)>>,
selector: Poller,
pool: CoroutinePool<'e>,
phantom_data: PhantomData<&'e EventLoop<'e>>,
Expand Down Expand Up @@ -223,7 +224,27 @@ impl<'e> EventLoop<'e> {
}
}

#[cfg(all(target_os = "linux", feature = "io_uring"))]
cfg_if::cfg_if! {
if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
left_time = self.adapt_io_uring(left_time)?;
}
}

// use epoll/kevent/iocp
let mut events = Events::with_capacity(1024);
self.selector.select(&mut events, left_time)?;
#[allow(clippy::explicit_iter_loop)]
for event in events.iter() {
let token = event.get_token();
if event.readable() || event.writable() {
unsafe { self.resume(token) };
}
}
Ok(())
}

#[cfg(all(target_os = "linux", feature = "io_uring"))]
fn adapt_io_uring(&self, mut left_time: Option<Duration>) -> std::io::Result<Option<Duration>> {
if crate::net::operator::support_io_uring() {
// use io_uring
let (count, mut cq, left) = self.operator.select(left_time, 0)?;
Expand All @@ -239,7 +260,7 @@ impl<'e> EventLoop<'e> {
if let Some((_, pair)) = self.syscall_wait_table.remove(&token) {
let (lock, cvar) = &*pair;
let mut pending = lock.lock().expect("lock failed");
*pending = Some(result);
*pending = Some(result.try_into().expect("result overflow"));
cvar.notify_one();
}
unsafe { self.resume(token) };
Expand All @@ -249,18 +270,7 @@ impl<'e> EventLoop<'e> {
left_time = Some(left.unwrap_or(Duration::ZERO));
}
}

// use epoll/kevent/iocp
let mut events = Events::with_capacity(1024);
self.selector.select(&mut events, left_time)?;
#[allow(clippy::explicit_iter_loop)]
for event in events.iter() {
let token = event.get_token();
if event.readable() || event.writable() {
unsafe { self.resume(token) };
}
}
Ok(())
Ok(left_time)
}

#[allow(clippy::unused_self)]
Expand Down Expand Up @@ -404,7 +414,7 @@ macro_rules! impl_io_uring {
pub(super) fn $syscall(
&self,
$($arg: $arg_type),*
) -> std::io::Result<Arc<(Mutex<Option<ssize_t>>, Condvar)>> {
) -> std::io::Result<Arc<(Mutex<Option<c_longlong>>, Condvar)>> {
let token = EventLoop::token(Syscall::$syscall);
self.operator.$syscall(token, $($arg, )*)?;
let arc = Arc::new((Mutex::new(None), Condvar::new()));
Expand Down
6 changes: 3 additions & 3 deletions core/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub type UserFunc = extern "C" fn(usize) -> usize;

cfg_if::cfg_if! {
if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
use libc::{epoll_event, iovec, msghdr, off_t, size_t, sockaddr, socklen_t, ssize_t};
use std::ffi::c_void;
use libc::{epoll_event, iovec, msghdr, off_t, size_t, sockaddr, socklen_t};
use std::ffi::{c_longlong, c_void};
}
}

Expand Down Expand Up @@ -247,7 +247,7 @@ macro_rules! impl_io_uring {
#[allow(missing_docs)]
pub fn $syscall(
$($arg: $arg_type),*
) -> std::io::Result<Arc<(Mutex<Option<ssize_t>>, Condvar)>> {
) -> std::io::Result<Arc<(Mutex<Option<c_longlong>>, Condvar)>> {
Self::event_loop().$syscall($($arg, )*)
}
}
Expand Down
Loading
Loading