Skip to content

Commit 51ae406

Browse files
committed
replace derivative with educe
1 parent 540b816 commit 51ae406

File tree

15 files changed

+797
-757
lines changed

15 files changed

+797
-757
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mio = { version = "1.0", default-features = false }
2828

2929
cfg-if = "1.0.0"
3030
polling = "2.8.0"
31+
educe = "0.6.0"
3132

3233
libc = "0.2"
3334
rand = "0.8"
@@ -50,7 +51,6 @@ once_cell = "1"
5051
dashmap = "6"
5152
num_cpus = "1"
5253
uuid = "1"
53-
derivative = "2"
5454
tempfile = "3"
5555
cc = "1"
5656
syn = "2"

core/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ uuid = { workspace = true, features = [
2929
"v4",
3030
"fast-rng",
3131
], optional = true }
32-
derivative = { workspace = true, optional = true }
32+
educe = { workspace = true, optional = true }
3333
core_affinity = { workspace = true, optional = true }
3434
crossbeam-utils = { workspace = true, optional = true }
3535
psm.workspace = true
@@ -77,7 +77,7 @@ backtrace.workspace = true
7777
log = ["tracing", "tracing-subscriber", "time"]
7878

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

8282
# Provide preemptive scheduling implementation.
8383
# Enable for default.

core/src/co_pool/task.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
use crate::catch;
2-
use derivative::Derivative;
32

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

76
/// The task impls.
87
#[repr(C)]
9-
#[derive(Derivative)]
10-
#[derivative(Debug)]
8+
#[derive(educe::Educe)]
9+
#[educe(Debug)]
1110
pub struct Task<'t> {
1211
name: String,
13-
#[derivative(Debug = "ignore")]
12+
#[educe(Debug(ignore))]
1413
func: Box<dyn FnOnce(Option<usize>) -> Option<usize> + 't>,
1514
param: Option<usize>,
1615
}

core/src/common/timer.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::impl_display_by_debug;
2-
use derivative::Derivative;
32
use std::collections::{BTreeMap, VecDeque};
43
use std::ops::{Deref, DerefMut};
54
use std::sync::atomic::{AtomicUsize, Ordering};
@@ -62,11 +61,11 @@ impl_display_by_debug!(TimerEntry<T>);
6261

6362
/// A queue for managing multiple `TimerEntry`.
6463
#[repr(C)]
65-
#[derive(Derivative)]
66-
#[derivative(Debug, Eq, PartialEq)]
64+
#[derive(educe::Educe)]
65+
#[educe(Debug, Eq, PartialEq)]
6766
pub struct TimerList<T> {
6867
inner: BTreeMap<u64, TimerEntry<T>>,
69-
#[derivative(PartialEq = "ignore")]
68+
#[educe(PartialEq(ignore))]
7069
total: AtomicUsize,
7170
}
7271

core/src/coroutine/suspender.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::collections::VecDeque;
55
use std::time::Duration;
66

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

@@ -52,14 +53,13 @@ pub use korosensei::Suspender;
5253
#[cfg(feature = "korosensei")]
5354
mod korosensei {
5455
use corosensei::Yielder;
55-
use derivative::Derivative;
5656

5757
/// Ths suspender implemented for coroutine.
5858
#[repr(C)]
59-
#[derive(Derivative)]
60-
#[derivative(Debug)]
59+
#[derive(educe::Educe)]
60+
#[educe(Debug)]
6161
pub struct Suspender<'s, Param, Yield> {
62-
#[derivative(Debug = "ignore")]
62+
#[educe(Debug(ignore))]
6363
inner: &'s Yielder<Param, Yield>,
6464
}
6565

core/src/net/event_loop.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ use std::time::Duration;
1919

2020
cfg_if::cfg_if! {
2121
if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
22-
use libc::{epoll_event, iovec, msghdr, off_t, size_t, sockaddr, socklen_t, ssize_t};
22+
use libc::{epoll_event, iovec, msghdr, off_t, size_t, sockaddr, socklen_t};
2323
use dashmap::DashMap;
24+
use std::ffi::c_longlong;
2425
}
2526
}
2627

@@ -36,7 +37,7 @@ pub(crate) struct EventLoop<'e> {
3637
operator: crate::net::operator::Operator<'e>,
3738
#[allow(clippy::type_complexity)]
3839
#[cfg(all(target_os = "linux", feature = "io_uring"))]
39-
syscall_wait_table: DashMap<usize, Arc<(Mutex<Option<ssize_t>>, Condvar)>>,
40+
syscall_wait_table: DashMap<usize, Arc<(Mutex<Option<c_longlong>>, Condvar)>>,
4041
selector: Poller,
4142
pool: CoroutinePool<'e>,
4243
phantom_data: PhantomData<&'e EventLoop<'e>>,
@@ -223,7 +224,27 @@ impl<'e> EventLoop<'e> {
223224
}
224225
}
225226

226-
#[cfg(all(target_os = "linux", feature = "io_uring"))]
227+
cfg_if::cfg_if! {
228+
if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
229+
left_time = self.adapt_io_uring(left_time)?;
230+
}
231+
}
232+
233+
// use epoll/kevent/iocp
234+
let mut events = Events::with_capacity(1024);
235+
self.selector.select(&mut events, left_time)?;
236+
#[allow(clippy::explicit_iter_loop)]
237+
for event in events.iter() {
238+
let token = event.get_token();
239+
if event.readable() || event.writable() {
240+
unsafe { self.resume(token) };
241+
}
242+
}
243+
Ok(())
244+
}
245+
246+
#[cfg(all(target_os = "linux", feature = "io_uring"))]
247+
fn adapt_io_uring(&self, mut left_time: Option<Duration>) -> std::io::Result<Option<Duration>> {
227248
if crate::net::operator::support_io_uring() {
228249
// use io_uring
229250
let (count, mut cq, left) = self.operator.select(left_time, 0)?;
@@ -234,7 +255,7 @@ impl<'e> EventLoop<'e> {
234255
continue;
235256
}
236257
// resolve completed read/write tasks
237-
let result = cqe.result() as ssize_t;
258+
let result = cqe.result() as c_longlong;
238259
eprintln!("io_uring finish {token} {result}");
239260
if let Some((_, pair)) = self.syscall_wait_table.remove(&token) {
240261
let (lock, cvar) = &*pair;
@@ -249,18 +270,7 @@ impl<'e> EventLoop<'e> {
249270
left_time = Some(left.unwrap_or(Duration::ZERO));
250271
}
251272
}
252-
253-
// use epoll/kevent/iocp
254-
let mut events = Events::with_capacity(1024);
255-
self.selector.select(&mut events, left_time)?;
256-
#[allow(clippy::explicit_iter_loop)]
257-
for event in events.iter() {
258-
let token = event.get_token();
259-
if event.readable() || event.writable() {
260-
unsafe { self.resume(token) };
261-
}
262-
}
263-
Ok(())
273+
Ok(left_time)
264274
}
265275

266276
#[allow(clippy::unused_self)]
@@ -404,7 +414,7 @@ macro_rules! impl_io_uring {
404414
pub(super) fn $syscall(
405415
&self,
406416
$($arg: $arg_type),*
407-
) -> std::io::Result<Arc<(Mutex<Option<ssize_t>>, Condvar)>> {
417+
) -> std::io::Result<Arc<(Mutex<Option<c_longlong>>, Condvar)>> {
408418
let token = EventLoop::token(Syscall::$syscall);
409419
self.operator.$syscall(token, $($arg, )*)?;
410420
let arc = Arc::new((Mutex::new(None), Condvar::new()));

core/src/net/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ pub type UserFunc = extern "C" fn(usize) -> usize;
1616

1717
cfg_if::cfg_if! {
1818
if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
19-
use libc::{epoll_event, iovec, msghdr, off_t, size_t, sockaddr, socklen_t, ssize_t};
20-
use std::ffi::c_void;
19+
use libc::{epoll_event, iovec, msghdr, off_t, size_t, sockaddr, socklen_t};
20+
use std::ffi::{c_longlong, c_void};
2121
}
2222
}
2323

@@ -247,7 +247,7 @@ macro_rules! impl_io_uring {
247247
#[allow(missing_docs)]
248248
pub fn $syscall(
249249
$($arg: $arg_type),*
250-
) -> std::io::Result<Arc<(Mutex<Option<ssize_t>>, Condvar)>> {
250+
) -> std::io::Result<Arc<(Mutex<Option<c_longlong>>, Condvar)>> {
251251
Self::event_loop().$syscall($($arg, )*)
252252
}
253253
}

0 commit comments

Comments
 (0)