Skip to content

Commit

Permalink
support query stack info (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
loongs-zhang authored Nov 7, 2024
2 parents 5cc32e1 + ae65b19 commit 2c004cb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
17 changes: 12 additions & 5 deletions core/src/coroutine/korosensei.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::common::constants::CoroutineState;
use crate::coroutine::listener::Listener;
use crate::coroutine::local::CoroutineLocal;
use crate::coroutine::suspender::Suspender;
use crate::coroutine::StackInfo;
use corosensei::stack::{DefaultStack, Stack};
use corosensei::trap::TrapHandlerRegs;
use corosensei::CoroutineResult;
Expand All @@ -27,7 +28,7 @@ pub struct Coroutine<'c, Param, Yield, Return> {
inner: corosensei::Coroutine<Param, Yield, Result<Return, &'static str>, DefaultStack>,
pub(crate) state: Cell<CoroutineState<Yield, Return>>,
pub(crate) stack_size: usize,
pub(crate) stack_bottom: RefCell<VecDeque<usize>>,
pub(crate) stack_infos: RefCell<VecDeque<StackInfo>>,
pub(crate) listeners: VecDeque<&'c dyn Listener<Yield, Return>>,
pub(crate) local: CoroutineLocal<'c>,
}
Expand Down Expand Up @@ -327,9 +328,12 @@ impl<'c, Param, Yield, Return> Coroutine<'c, Param, Yield, Return> {
return Ok(callback());
}
return DefaultStack::new(stack_size).map(|stack| {
co.stack_bottom.borrow_mut().push_front(stack.limit().get());
co.stack_infos.borrow_mut().push_back(StackInfo {
stack_top: stack.base().get(),
stack_bottom: stack.limit().get(),
});
let r = corosensei::on_stack(stack, callback);
_ = co.stack_bottom.borrow_mut().pop_front();
_ = co.stack_infos.borrow_mut().pop_back();
r
});
}
Expand Down Expand Up @@ -362,7 +366,10 @@ where
{
let stack_size = stack_size.max(crate::common::page_size());
let stack = DefaultStack::new(stack_size)?;
let stack_bottom = RefCell::new(VecDeque::from([stack.limit().get()]));
let stack_infos = RefCell::new(VecDeque::from([StackInfo {
stack_top: stack.base().get(),
stack_bottom: stack.limit().get(),
}]));
let co_name = name.clone().leak();
let inner = corosensei::Coroutine::with_stack(stack, move |y, p| {
catch!(
Expand All @@ -382,7 +389,7 @@ where
name,
inner,
stack_size,
stack_bottom,
stack_infos,
state: Cell::new(CoroutineState::Ready),
listeners: VecDeque::default(),
local: CoroutineLocal::default(),
Expand Down
22 changes: 21 additions & 1 deletion core/src/coroutine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::common::constants::CoroutineState;
use crate::coroutine::listener::Listener;
use crate::coroutine::local::CoroutineLocal;
use crate::{impl_current_for, impl_display_by_debug, impl_for_named};
use std::collections::VecDeque;
use std::fmt::{Debug, Formatter};
use std::ops::Deref;

Expand Down Expand Up @@ -41,6 +42,18 @@ macro_rules! co {
};
}

/// The coroutine stack information.
#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct StackInfo {
/// The base address of the stack. This is the highest address
/// since stacks grow downwards on most modern architectures.
pub stack_top: usize,
/// The maximum limit address of the stack. This is the lowest address
/// since stacks grow downwards on most modern architectures.
pub stack_bottom: usize,
}

/// Coroutine state abstraction and impl.
mod state;

Expand Down Expand Up @@ -74,7 +87,14 @@ 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_bottom.borrow().front().copied().unwrap()
current_ptr - self.stack_infos.borrow().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()
}

/// Grows the call stack if necessary.
Expand Down

0 comments on commit 2c004cb

Please sign in to comment.