Skip to content

Commit 0e800ef

Browse files
authored
code polish (#331)
2 parents fcc05d0 + 52f058e commit 0e800ef

File tree

9 files changed

+134
-68
lines changed

9 files changed

+134
-68
lines changed

core/src/common/ci.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
use std::time::{Duration, Instant};
2+
13
/// just for CI
24
pub fn init() {
35
let _ = std::thread::spawn(|| {
46
// exit after 600 seconds, just for CI
5-
std::thread::sleep(std::time::Duration::from_secs(600));
7+
let sleep_time = Duration::from_secs(600);
8+
let start_time = Instant::now();
9+
std::thread::sleep(sleep_time);
10+
let cost = Instant::now().saturating_duration_since(start_time);
11+
assert!(cost >= sleep_time, "CI time consumption less than expected");
612
std::process::exit(-1);
713
});
814
}

core/src/common/macros.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ macro_rules! impl_current_for {
8989
),+)?
9090
) => {
9191
thread_local! {
92-
static $name: std::cell::RefCell<std::collections::VecDeque<*const std::ffi::c_void>> = const { std::cell::RefCell::new(std::collections::VecDeque::new()) };
92+
static $name: std::cell::RefCell<std::collections::VecDeque<*const std::ffi::c_void>> =
93+
const { std::cell::RefCell::new(std::collections::VecDeque::new()) };
9394
}
9495

9596
impl$(<$($generic1 $( : $trait_tt1 $( + $trait_tt2)*)?),+>)? $struct_name$(<$($generic1),+>)?
@@ -99,7 +100,14 @@ macro_rules! impl_current_for {
99100
pub(crate) fn init_current(current: &Self) {
100101
$name.with(|s| {
101102
s.try_borrow_mut()
102-
.unwrap_or_else(|_| panic!("init {} current failed", stringify!($name)))
103+
.unwrap_or_else(|e| {
104+
panic!(
105+
"thread:{} init {} current failed with {}",
106+
std::thread::current().name().unwrap_or("unknown"),
107+
stringify!($name),
108+
e
109+
)
110+
})
103111
.push_front(core::ptr::from_ref(current).cast::<std::ffi::c_void>());
104112
});
105113
}
@@ -110,7 +118,14 @@ macro_rules! impl_current_for {
110118
pub fn current<'current>() -> Option<&'current Self> {
111119
$name.with(|s| {
112120
s.try_borrow()
113-
.unwrap_or_else(|_| panic!("get {} current failed", stringify!($name)))
121+
.unwrap_or_else(|e| {
122+
panic!(
123+
"thread:{} get {} current failed with {}",
124+
std::thread::current().name().unwrap_or("unknown"),
125+
stringify!($name),
126+
e
127+
)
128+
})
114129
.front()
115130
.map(|ptr| unsafe { &*(*ptr).cast::<Self>() })
116131
})
@@ -120,7 +135,14 @@ macro_rules! impl_current_for {
120135
pub(crate) fn clean_current() {
121136
$name.with(|s| {
122137
_ = s.try_borrow_mut()
123-
.unwrap_or_else(|_| panic!("clean {} current failed", stringify!($name)))
138+
.unwrap_or_else(|e| {
139+
panic!(
140+
"thread:{} clean {} current failed with {}",
141+
std::thread::current().name().unwrap_or("unknown"),
142+
stringify!($name),
143+
e
144+
)
145+
})
124146
.pop_front();
125147
});
126148
}

core/src/coroutine/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::fmt::{Debug, Formatter};
77
use std::ops::Deref;
88

99
/// Coroutine suspender abstraction and impl.
10-
#[allow(dead_code)]
1110
pub mod suspender;
1211

1312
/// Coroutine local abstraction.

core/src/coroutine/suspender.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ impl<Param, Yield> Suspender<'_, Param, Yield> {
1919
pub fn until_with(&self, arg: Yield, timestamp: u64) -> Param {
2020
TIMESTAMP.with(|s| {
2121
s.try_borrow_mut()
22-
.unwrap_or_else(|_| panic!("init TIMESTAMP current failed"))
22+
.unwrap_or_else(|e| {
23+
panic!(
24+
"thread:{} init TIMESTAMP current failed with {}",
25+
std::thread::current().name().unwrap_or("unknown"),
26+
e
27+
)
28+
})
2329
.push_front(timestamp);
2430
});
2531
self.suspend_with(arg)
@@ -29,7 +35,13 @@ impl<Param, Yield> Suspender<'_, Param, Yield> {
2935
TIMESTAMP
3036
.with(|s| {
3137
s.try_borrow_mut()
32-
.unwrap_or_else(|_| panic!("get TIMESTAMP current failed"))
38+
.unwrap_or_else(|e| {
39+
panic!(
40+
"thread:{} get TIMESTAMP current failed with {}",
41+
std::thread::current().name().unwrap_or("unknown"),
42+
e
43+
)
44+
})
3345
.pop_front()
3446
})
3547
.unwrap_or(0)

core/src/net/event_loop.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ impl<'e> EventLoop<'e> {
272272
Ok(left_time)
273273
}
274274

275-
#[allow(clippy::unused_self)]
276275
unsafe fn resume(&self, token: usize) {
277276
if COROUTINE_TOKENS.remove(&token).is_none() {
278277
return;

core/src/net/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ pub type UserFunc = extern "C" fn(usize) -> usize;
2323

2424
mod selector;
2525

26-
#[allow(
27-
clippy::cast_possible_truncation,
28-
clippy::cast_sign_loss,
29-
clippy::too_many_arguments
30-
)]
26+
#[allow(clippy::too_many_arguments)]
3127
#[cfg(all(target_os = "linux", feature = "io_uring"))]
3228
mod operator;
3329

core/src/net/operator/linux/mod.rs

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl Operator<'_> {
205205
self,
206206
SUPPORT_POLL_ADD,
207207
PollAdd,
208-
PollAdd::new(Fd(fd), flags as u32)
208+
PollAdd::new(Fd(fd), flags.try_into().expect("flags overflow"))
209209
.build()
210210
.user_data(user_data as u64)
211211
)
@@ -458,10 +458,14 @@ impl Operator<'_> {
458458
self,
459459
SUPPORT_RECV,
460460
Recv,
461-
Recv::new(Fd(fd), buf.cast::<u8>(), len as u32)
462-
.flags(flags)
463-
.build()
464-
.user_data(user_data as u64)
461+
Recv::new(
462+
Fd(fd),
463+
buf.cast::<u8>(),
464+
len.try_into().expect("len overflow")
465+
)
466+
.flags(flags)
467+
.build()
468+
.user_data(user_data as u64)
465469
)
466470
}
467471

@@ -476,9 +480,13 @@ impl Operator<'_> {
476480
self,
477481
SUPPORT_READ,
478482
Read,
479-
Read::new(Fd(fd), buf.cast::<u8>(), count as u32)
480-
.build()
481-
.user_data(user_data as u64)
483+
Read::new(
484+
Fd(fd),
485+
buf.cast::<u8>(),
486+
count.try_into().expect("count overflow")
487+
)
488+
.build()
489+
.user_data(user_data as u64)
482490
)
483491
}
484492

@@ -494,10 +502,14 @@ impl Operator<'_> {
494502
self,
495503
SUPPORT_READ,
496504
Read,
497-
Read::new(Fd(fd), buf.cast::<u8>(), count as u32)
498-
.offset(offset as u64)
499-
.build()
500-
.user_data(user_data as u64)
505+
Read::new(
506+
Fd(fd),
507+
buf.cast::<u8>(),
508+
count.try_into().expect("count overflow")
509+
)
510+
.offset(offset.try_into().expect("offset overflow"))
511+
.build()
512+
.user_data(user_data as u64)
501513
)
502514
}
503515

@@ -512,7 +524,7 @@ impl Operator<'_> {
512524
self,
513525
SUPPORT_READV,
514526
Readv,
515-
Readv::new(Fd(fd), iov, iovcnt as u32)
527+
Readv::new(Fd(fd), iov, iovcnt.try_into().expect("iovcnt overflow"))
516528
.build()
517529
.user_data(user_data as u64)
518530
)
@@ -530,8 +542,8 @@ impl Operator<'_> {
530542
self,
531543
SUPPORT_READV,
532544
Readv,
533-
Readv::new(Fd(fd), iov, iovcnt as u32)
534-
.offset(offset as u64)
545+
Readv::new(Fd(fd), iov, iovcnt.try_into().expect("iovcnt overflow"))
546+
.offset(offset.try_into().expect("offset overflow"))
535547
.build()
536548
.user_data(user_data as u64)
537549
)
@@ -549,7 +561,7 @@ impl Operator<'_> {
549561
SUPPORT_RECVMSG,
550562
RecvMsg,
551563
RecvMsg::new(Fd(fd), msg)
552-
.flags(flags as u32)
564+
.flags(flags.try_into().expect("flags overflow"))
553565
.build()
554566
.user_data(user_data as u64)
555567
)
@@ -567,10 +579,14 @@ impl Operator<'_> {
567579
self,
568580
SUPPORT_SEND,
569581
Send,
570-
Send::new(Fd(fd), buf.cast::<u8>(), len as u32)
571-
.flags(flags)
572-
.build()
573-
.user_data(user_data as u64)
582+
Send::new(
583+
Fd(fd),
584+
buf.cast::<u8>(),
585+
len.try_into().expect("len overflow")
586+
)
587+
.flags(flags)
588+
.build()
589+
.user_data(user_data as u64)
574590
)
575591
}
576592

@@ -588,12 +604,16 @@ impl Operator<'_> {
588604
self,
589605
SUPPORT_SEND_ZC,
590606
SendZc,
591-
SendZc::new(Fd(fd), buf.cast::<u8>(), len as u32)
592-
.flags(flags)
593-
.dest_addr(addr)
594-
.dest_addr_len(addrlen)
595-
.build()
596-
.user_data(user_data as u64)
607+
SendZc::new(
608+
Fd(fd),
609+
buf.cast::<u8>(),
610+
len.try_into().expect("len overflow")
611+
)
612+
.flags(flags)
613+
.dest_addr(addr)
614+
.dest_addr_len(addrlen)
615+
.build()
616+
.user_data(user_data as u64)
597617
)
598618
}
599619

@@ -608,9 +628,13 @@ impl Operator<'_> {
608628
self,
609629
SUPPORT_WRITE,
610630
Write,
611-
Write::new(Fd(fd), buf.cast::<u8>(), count as u32)
612-
.build()
613-
.user_data(user_data as u64)
631+
Write::new(
632+
Fd(fd),
633+
buf.cast::<u8>(),
634+
count.try_into().expect("count overflow")
635+
)
636+
.build()
637+
.user_data(user_data as u64)
614638
)
615639
}
616640

@@ -626,10 +650,14 @@ impl Operator<'_> {
626650
self,
627651
SUPPORT_WRITE,
628652
Write,
629-
Write::new(Fd(fd), buf.cast::<u8>(), count as u32)
630-
.offset(offset as u64)
631-
.build()
632-
.user_data(user_data as u64)
653+
Write::new(
654+
Fd(fd),
655+
buf.cast::<u8>(),
656+
count.try_into().expect("count overflow")
657+
)
658+
.offset(offset.try_into().expect("offset overflow"))
659+
.build()
660+
.user_data(user_data as u64)
633661
)
634662
}
635663

@@ -644,7 +672,7 @@ impl Operator<'_> {
644672
self,
645673
SUPPORT_WRITEV,
646674
Writev,
647-
Writev::new(Fd(fd), iov, iovcnt as u32)
675+
Writev::new(Fd(fd), iov, iovcnt.try_into().expect("iovcnt overflow"))
648676
.build()
649677
.user_data(user_data as u64)
650678
)
@@ -662,8 +690,8 @@ impl Operator<'_> {
662690
self,
663691
SUPPORT_WRITEV,
664692
Writev,
665-
Writev::new(Fd(fd), iov, iovcnt as u32)
666-
.offset(offset as u64)
693+
Writev::new(Fd(fd), iov, iovcnt.try_into().expect("iovcnt overflow"))
694+
.offset(offset.try_into().expect("offset overflow"))
667695
.build()
668696
.user_data(user_data as u64)
669697
)
@@ -681,7 +709,7 @@ impl Operator<'_> {
681709
SUPPORT_SENDMSG,
682710
SendMsg,
683711
SendMsg::new(Fd(fd), msg)
684-
.flags(flags as u32)
712+
.flags(flags.try_into().expect("flags overflow"))
685713
.build()
686714
.user_data(user_data as u64)
687715
)

0 commit comments

Comments
 (0)