feat(registry): checkpoint PLN-0176 foundations and validated partial integrations
This commit is contained in:
@@ -1,63 +1,59 @@
|
||||
use super::*;
|
||||
|
||||
impl VirtualMachine {
|
||||
pub(super) fn handle_safepoint(&mut self) {
|
||||
pub(super) fn handle_safepoint(&mut self) -> Result<(), ResourceFailure> {
|
||||
if self.gc_alloc_threshold > 0 {
|
||||
let live_now = self.heap.len();
|
||||
let since_last = live_now.saturating_sub(self.last_gc_live_count);
|
||||
if since_last >= self.gc_alloc_threshold {
|
||||
struct CollectRoots(Vec<prometeu_bytecode::HeapRef>);
|
||||
impl crate::roots::RootVisitor for CollectRoots {
|
||||
fn visit_heap_ref(&mut self, r: prometeu_bytecode::HeapRef) {
|
||||
self.0.push(r);
|
||||
}
|
||||
}
|
||||
let mut collector = CollectRoots(Vec::new());
|
||||
self.visit_roots(&mut collector);
|
||||
if let Some(cur) = self.current_coro {
|
||||
collector.0.push(cur);
|
||||
}
|
||||
let mut coro_roots = self.heap.suspended_coroutine_handles();
|
||||
collector.0.append(&mut coro_roots);
|
||||
self.heap.mark_from_roots(collector.0);
|
||||
// Parameters/locals are already in the operand buffer. Stream roots
|
||||
// without a duplicate collection; suspended contexts are scanned by Heap.
|
||||
let roots = self
|
||||
.operand_stack
|
||||
.iter()
|
||||
.chain(self.globals.iter())
|
||||
.filter_map(crate::roots::value_heap_ref)
|
||||
.chain(self.current_coro);
|
||||
self.heap.mark_session_roots(roots)?;
|
||||
self.heap.sweep();
|
||||
self.last_gc_live_count = self.heap.len();
|
||||
}
|
||||
}
|
||||
|
||||
self.current_tick = self.current_tick.wrapping_add(1);
|
||||
self.scheduler.wake_ready(self.current_tick);
|
||||
self.scheduler.wake_ready(self.current_tick)?;
|
||||
|
||||
let mut switched_out = false;
|
||||
if let Some(cur) = self.current_coro {
|
||||
if let Some(wake) = self.sleep_requested_until.take() {
|
||||
if let Some(wake) = self.sleep_requested_until {
|
||||
self.scheduler.sleep_until(cur, wake)?;
|
||||
self.sleep_requested_until = None;
|
||||
if let Some(co) = self.heap.coroutine_data_mut(cur) {
|
||||
co.pc = self.pc;
|
||||
co.stack = std::mem::take(&mut self.operand_stack);
|
||||
co.frames = std::mem::take(&mut self.call_stack);
|
||||
co.stack = self.operand_stack.take();
|
||||
co.frames = self.call_stack.take();
|
||||
co.state = CoroutineState::Sleeping;
|
||||
co.wake_tick = wake;
|
||||
}
|
||||
self.scheduler.sleep_until(cur, wake);
|
||||
self.current_coro = None;
|
||||
self.scheduler.clear_current();
|
||||
switched_out = true;
|
||||
} else if self.yield_requested {
|
||||
self.scheduler.enqueue_ready(cur)?;
|
||||
if let Some(co) = self.heap.coroutine_data_mut(cur) {
|
||||
co.pc = self.pc;
|
||||
co.stack = std::mem::take(&mut self.operand_stack);
|
||||
co.frames = std::mem::take(&mut self.call_stack);
|
||||
co.stack = self.operand_stack.take();
|
||||
co.frames = self.call_stack.take();
|
||||
co.state = CoroutineState::Ready;
|
||||
}
|
||||
self.scheduler.enqueue_ready(cur);
|
||||
self.current_coro = None;
|
||||
self.scheduler.clear_current();
|
||||
switched_out = true;
|
||||
} else if self.halted || self.pc >= self.program.rom.len() {
|
||||
if let Some(co) = self.heap.coroutine_data_mut(cur) {
|
||||
co.pc = self.pc;
|
||||
co.stack = std::mem::take(&mut self.operand_stack);
|
||||
co.frames = std::mem::take(&mut self.call_stack);
|
||||
co.stack = self.operand_stack.take();
|
||||
co.frames = self.call_stack.take();
|
||||
co.state = CoroutineState::Finished;
|
||||
}
|
||||
self.current_coro = None;
|
||||
@@ -70,8 +66,8 @@ impl VirtualMachine {
|
||||
if let Some(next) = self.scheduler.dequeue_next() {
|
||||
if let Some(co) = self.heap.coroutine_data_mut(next) {
|
||||
self.pc = co.pc;
|
||||
self.operand_stack = std::mem::take(&mut co.stack);
|
||||
self.call_stack = std::mem::take(&mut co.frames);
|
||||
self.operand_stack = co.stack.take();
|
||||
self.call_stack = co.frames.take();
|
||||
co.state = CoroutineState::Running;
|
||||
}
|
||||
self.current_coro = Some(next);
|
||||
@@ -84,6 +80,7 @@ impl VirtualMachine {
|
||||
}
|
||||
|
||||
self.yield_requested = false;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn visit_roots<V: RootVisitor + ?Sized>(&self, visitor: &mut V) {
|
||||
|
||||
Reference in New Issue
Block a user