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
5 changes: 2 additions & 3 deletions core/src/co_pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ impl Drop for CoroutinePool<'_> {
self.get_running_size(),
"There are still tasks in progress !"
);
assert_eq!(
0,
self.task_queue.len(),
assert!(
self.task_queue.is_empty(),
"There are still tasks to be carried out !"
);
}
Expand Down
29 changes: 24 additions & 5 deletions core/src/coroutine/korosensei.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::coroutine::StackInfo;
use corosensei::stack::Stack;
use corosensei::trap::TrapHandlerRegs;
use corosensei::CoroutineResult;
use std::cell::{Cell, RefCell};
use std::cell::{Cell, RefCell, UnsafeCell};
use std::collections::VecDeque;
use std::ffi::c_longlong;
use std::fmt::Debug;
Expand All @@ -29,7 +29,7 @@ pub struct Coroutine<'c, Param, Yield, Return> {
pub(crate) name: String,
inner: corosensei::Coroutine<Param, Yield, Result<Return, &'static str>, PooledStack>,
pub(crate) state: Cell<CoroutineState<Yield, Return>>,
pub(crate) stack_infos: RefCell<VecDeque<StackInfo>>,
stack_infos: UnsafeCell<VecDeque<StackInfo>>,
pub(crate) listeners: VecDeque<&'c dyn Listener<Yield, Return>>,
pub(crate) local: CoroutineLocal<'c>,
pub(crate) priority: Option<c_longlong>,
Expand Down Expand Up @@ -291,6 +291,25 @@ impl<'c, Param, Yield, Return> Coroutine<'c, Param, Yield, Return> {
self.listeners.push_back(listener);
}

pub(crate) fn stack_infos_ref(&self) -> &VecDeque<StackInfo> {
unsafe {
self.stack_infos
.get()
.as_ref()
.expect("StackInfo not init !")
}
}

#[allow(clippy::mut_from_ref)]
pub(crate) fn stack_infos_mut(&self) -> &mut VecDeque<StackInfo> {
unsafe {
self.stack_infos
.get()
.as_mut()
.expect("StackInfo not init !")
}
}

/// Grows the call stack if necessary.
///
/// This function is intended to be called at manually instrumented points in a program where
Expand All @@ -314,12 +333,12 @@ impl<'c, Param, Yield, Return> Coroutine<'c, Param, Yield, Return> {
return Ok(callback());
}
return stack_pool.allocate(stack_size).map(|stack| {
co.stack_infos.borrow_mut().push_back(StackInfo {
co.stack_infos_mut().push_back(StackInfo {
stack_top: stack.base().get(),
stack_bottom: stack.limit().get(),
});
let r = corosensei::on_stack(stack, callback);
_ = co.stack_infos.borrow_mut().pop_back();
_ = co.stack_infos_mut().pop_back();
r
});
}
Expand Down Expand Up @@ -380,7 +399,7 @@ where
{
let stack_size = stack_size.max(crate::common::page_size());
let stack = MemoryPool::get_instance().allocate(stack_size)?;
let stack_infos = RefCell::new(VecDeque::from([StackInfo {
let stack_infos = UnsafeCell::new(VecDeque::from([StackInfo {
stack_top: stack.base().get(),
stack_bottom: stack.limit().get(),
}]));
Expand Down
10 changes: 5 additions & 5 deletions core/src/coroutine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ impl<'c, Param, Yield, Return> Coroutine<'c, Param, Yield, Return> {
///
/// This can only be done safely in coroutine.
pub unsafe fn remaining_stack(&self) -> usize {
let current_ptr = psm::stack_pointer() as usize;
current_ptr - self.stack_infos.borrow().back().unwrap().stack_bottom
let current_sp = psm::stack_pointer() as usize;
current_sp - self.stack_infos_ref().back().unwrap().stack_bottom
}

/// Queries the current stack info of this coroutine.
///
/// The first used stack index is 0 and increases with usage.
pub fn stack_infos(&self) -> VecDeque<StackInfo> {
self.stack_infos.borrow().clone()
self.stack_infos_ref().clone()
}

/// Checks whether the stack pointer at the point where a trap occurred is
Expand All @@ -126,7 +126,7 @@ impl<'c, Param, Yield, Return> Coroutine<'c, Param, Yield, Return> {
/// The result of this function is only meaningful if the coroutine has not
/// been dropped yet.
pub fn stack_ptr_in_bounds(&self, stack_ptr: u64) -> bool {
for info in self.stack_infos.borrow().iter() {
for info in self.stack_infos_ref() {
if info.stack_bottom as u64 <= stack_ptr && stack_ptr < info.stack_top as u64 {
return true;
}
Expand Down Expand Up @@ -202,7 +202,7 @@ where
f.debug_struct("Coroutine")
.field("name", &self.name())
.field("state", &self.state())
.field("stack_infos", &self.stack_infos)
.field("stack_infos", &self.stack_infos())
.field("local", &self.local)
.field("priority", &self.priority)
.finish()
Expand Down
4 changes: 2 additions & 2 deletions core/src/coroutine/stack_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl MemoryPool {
}

pub(crate) fn allocate(&self, stack_size: usize) -> std::io::Result<PooledStack> {
let heap = unsafe { self.pool.get().as_mut().expect("StackPool is not unique") };
let heap = unsafe { self.pool.get().as_mut().expect("MemoryPool is not unique") };
// find min stack
let mut not_use = Vec::new();
while let Some(stack) = heap.peek() {
Expand Down Expand Up @@ -265,7 +265,7 @@ impl MemoryPool {
/// Clean the expired stack.
#[allow(dead_code)]
pub(crate) fn clean(&self) {
let heap = unsafe { self.pool.get().as_mut().expect("StackPool is not unique") };
let heap = unsafe { self.pool.get().as_mut().expect("MemoryPool is not unique") };
let mut maybe_free = Vec::new();
while let Some(stack) = heap.peek() {
if Rc::strong_count(&stack.stack) > 1 {
Expand Down
Loading