Skip to content
Merged
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
12 changes: 9 additions & 3 deletions core/src/common/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ macro_rules! impl_current_for {
/// Init the current.
pub(crate) fn init_current(current: &Self) {
$name.with(|s| {
s.borrow_mut()
s.try_borrow_mut()
.unwrap_or_else(|_| panic!("init {} current failed", stringify!($name)))
.push_front(core::ptr::from_ref(current).cast::<std::ffi::c_void>());
});
}
Expand All @@ -108,15 +109,20 @@ macro_rules! impl_current_for {
#[allow(unreachable_pub)]
pub fn current<'current>() -> Option<&'current Self> {
$name.with(|s| {
s.borrow()
s.try_borrow()
.unwrap_or_else(|_| panic!("get {} current failed", stringify!($name)))
.front()
.map(|ptr| unsafe { &*(*ptr).cast::<Self>() })
})
}

/// Clean the current.
pub(crate) fn clean_current() {
$name.with(|s| _ = s.borrow_mut().pop_front());
$name.with(|s| {
_ = s.try_borrow_mut()
.unwrap_or_else(|_| panic!("clean {} current failed", stringify!($name)))
.pop_front();
});
}
}
};
Expand Down
12 changes: 10 additions & 2 deletions core/src/coroutine/suspender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ impl<Param, Yield> Suspender<'_, Param, Yield> {
/// Delay the execution of the coroutine with an arg until `timestamp`.
pub fn until_with(&self, arg: Yield, timestamp: u64) -> Param {
TIMESTAMP.with(|s| {
s.borrow_mut().push_front(timestamp);
s.try_borrow_mut()
.unwrap_or_else(|_| panic!("init TIMESTAMP current failed"))
.push_front(timestamp);
});
self.suspend_with(arg)
}

pub(crate) fn timestamp() -> u64 {
TIMESTAMP.with(|s| s.borrow_mut().pop_front()).unwrap_or(0)
TIMESTAMP
.with(|s| {
s.try_borrow_mut()
.unwrap_or_else(|_| panic!("get TIMESTAMP current failed"))
.pop_front()
})
.unwrap_or(0)
}
}

Expand Down
2 changes: 1 addition & 1 deletion deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ allow = [
"Apache-2.0",
"Apache-2.0 WITH LLVM-exception",
"Unlicense",
"Unicode-DFS-2016",
"Unicode-3.0",
"MIT"
]
confidence-threshold = 0.95
Expand Down
15 changes: 12 additions & 3 deletions open-coroutine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,16 @@ pub fn task<P: 'static, R: 'static, F: FnOnce(P) -> R>(f: F, param: P) -> JoinHa
unsafe {
let ptr = &mut *((input as *mut c_void).cast::<(F, P)>());
let data = std::ptr::read_unaligned(ptr);
let result: &'static mut R = Box::leak(Box::new((data.0)(data.1)));
let result: &'static mut std::io::Result<R> = Box::leak(Box::new(
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (data.0)(data.1)))
.map_err(|e| {
Error::new(
ErrorKind::Other,
e.downcast_ref::<&'static str>()
.map_or("task failed without message", |msg| *msg),
)
}),
));
std::ptr::from_mut(result).cast::<c_void>() as usize
}
}
Expand All @@ -140,7 +149,7 @@ impl<R> JoinHandle<R> {
match ptr.cmp(&0) {
Ordering::Less => Err(Error::new(ErrorKind::Other, "timeout join failed")),
Ordering::Equal => Ok(None),
Ordering::Greater => Ok(Some(std::ptr::read_unaligned(ptr as *mut R))),
Ordering::Greater => Ok(Some((*Box::from_raw(ptr as *mut std::io::Result<R>))?)),
}
}
}
Expand All @@ -151,7 +160,7 @@ impl<R> JoinHandle<R> {
match ptr.cmp(&0) {
Ordering::Less => Err(Error::new(ErrorKind::Other, "join failed")),
Ordering::Equal => Ok(None),
Ordering::Greater => Ok(Some(std::ptr::read_unaligned(ptr as *mut R))),
Ordering::Greater => Ok(Some((*Box::from_raw(ptr as *mut std::io::Result<R>))?)),
}
}
}
Expand Down
Loading