Skip to content

Commit bb5f2c0

Browse files
authored
code polish (#329)
2 parents 7572bdd + d6607c0 commit bb5f2c0

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

core/src/common/macros.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ macro_rules! impl_current_for {
9898
/// Init the current.
9999
pub(crate) fn init_current(current: &Self) {
100100
$name.with(|s| {
101-
s.borrow_mut()
101+
s.try_borrow_mut()
102+
.unwrap_or_else(|_| panic!("init {} current failed", stringify!($name)))
102103
.push_front(core::ptr::from_ref(current).cast::<std::ffi::c_void>());
103104
});
104105
}
@@ -108,15 +109,20 @@ macro_rules! impl_current_for {
108109
#[allow(unreachable_pub)]
109110
pub fn current<'current>() -> Option<&'current Self> {
110111
$name.with(|s| {
111-
s.borrow()
112+
s.try_borrow()
113+
.unwrap_or_else(|_| panic!("get {} current failed", stringify!($name)))
112114
.front()
113115
.map(|ptr| unsafe { &*(*ptr).cast::<Self>() })
114116
})
115117
}
116118

117119
/// Clean the current.
118120
pub(crate) fn clean_current() {
119-
$name.with(|s| _ = s.borrow_mut().pop_front());
121+
$name.with(|s| {
122+
_ = s.try_borrow_mut()
123+
.unwrap_or_else(|_| panic!("clean {} current failed", stringify!($name)))
124+
.pop_front();
125+
});
120126
}
121127
}
122128
};

core/src/coroutine/suspender.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,21 @@ impl<Param, Yield> Suspender<'_, Param, Yield> {
1818
/// Delay the execution of the coroutine with an arg until `timestamp`.
1919
pub fn until_with(&self, arg: Yield, timestamp: u64) -> Param {
2020
TIMESTAMP.with(|s| {
21-
s.borrow_mut().push_front(timestamp);
21+
s.try_borrow_mut()
22+
.unwrap_or_else(|_| panic!("init TIMESTAMP current failed"))
23+
.push_front(timestamp);
2224
});
2325
self.suspend_with(arg)
2426
}
2527

2628
pub(crate) fn timestamp() -> u64 {
27-
TIMESTAMP.with(|s| s.borrow_mut().pop_front()).unwrap_or(0)
29+
TIMESTAMP
30+
.with(|s| {
31+
s.try_borrow_mut()
32+
.unwrap_or_else(|_| panic!("get TIMESTAMP current failed"))
33+
.pop_front()
34+
})
35+
.unwrap_or(0)
2836
}
2937
}
3038

deny.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ allow = [
33
"Apache-2.0",
44
"Apache-2.0 WITH LLVM-exception",
55
"Unlicense",
6-
"Unicode-DFS-2016",
6+
"Unicode-3.0",
77
"MIT"
88
]
99
confidence-threshold = 0.95

open-coroutine/src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,16 @@ pub fn task<P: 'static, R: 'static, F: FnOnce(P) -> R>(f: F, param: P) -> JoinHa
113113
unsafe {
114114
let ptr = &mut *((input as *mut c_void).cast::<(F, P)>());
115115
let data = std::ptr::read_unaligned(ptr);
116-
let result: &'static mut R = Box::leak(Box::new((data.0)(data.1)));
116+
let result: &'static mut std::io::Result<R> = Box::leak(Box::new(
117+
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (data.0)(data.1)))
118+
.map_err(|e| {
119+
Error::new(
120+
ErrorKind::Other,
121+
e.downcast_ref::<&'static str>()
122+
.map_or("task failed without message", |msg| *msg),
123+
)
124+
}),
125+
));
117126
std::ptr::from_mut(result).cast::<c_void>() as usize
118127
}
119128
}
@@ -140,7 +149,7 @@ impl<R> JoinHandle<R> {
140149
match ptr.cmp(&0) {
141150
Ordering::Less => Err(Error::new(ErrorKind::Other, "timeout join failed")),
142151
Ordering::Equal => Ok(None),
143-
Ordering::Greater => Ok(Some(std::ptr::read_unaligned(ptr as *mut R))),
152+
Ordering::Greater => Ok(Some((*Box::from_raw(ptr as *mut std::io::Result<R>))?)),
144153
}
145154
}
146155
}
@@ -151,7 +160,7 @@ impl<R> JoinHandle<R> {
151160
match ptr.cmp(&0) {
152161
Ordering::Less => Err(Error::new(ErrorKind::Other, "join failed")),
153162
Ordering::Equal => Ok(None),
154-
Ordering::Greater => Ok(Some(std::ptr::read_unaligned(ptr as *mut R))),
163+
Ordering::Greater => Ok(Some((*Box::from_raw(ptr as *mut std::io::Result<R>))?)),
155164
}
156165
}
157166
}

0 commit comments

Comments
 (0)