feat: 添加全局 VM 池与 RAII 泄漏保护

This commit is contained in:
2026-06-05 22:25:22 +08:00
parent 20469c175c
commit fecdd981f9
4 changed files with 218 additions and 9 deletions
+30 -9
View File
@@ -7,6 +7,7 @@ use crate::code_forge::CodeForge;
use crate::prop_forge::PropForge;
use crate::shape_forge::ShapeForge;
use crate::string_forge::Interner;
use crate::vm_pool::{VmGuard, VmPool};
pub struct KernelConfig {
pub min_pool_size: usize,
@@ -58,10 +59,6 @@ impl Default for KernelConfig {
}
}
pub struct VmPool;
pub struct VmGuard<'p>(std::marker::PhantomData<&'p ()>);
pub struct OxideKernel {
pub config: KernelConfig,
pub string_forge: Arc<Interner>,
@@ -70,7 +67,7 @@ pub struct OxideKernel {
pub prop_forge: Arc<PropForge>,
pub builtin_world: Arc<BuiltinWorld>,
#[allow(dead_code)]
vm_pool: Mutex<Option<VmPool>>,
vm_pool: Mutex<Option<Arc<VmPool>>>,
}
impl OxideKernel {
@@ -92,6 +89,24 @@ impl OxideKernel {
}
}
pub fn init_vm_pool(self: &Arc<Self>) {
let pool = VmPool::new(
Arc::clone(self),
self.config.min_pool_size,
self.config.max_pool_size,
);
*self.vm_pool.lock().unwrap() = Some(pool);
}
pub fn spawn(&self) -> VmGuard {
self.vm_pool
.lock()
.unwrap()
.as_ref()
.expect("vm_pool not initialized — call init_vm_pool() first")
.spawn()
}
pub fn string_forge(&self) -> &Arc<Interner> {
&self.string_forge
}
@@ -115,10 +130,6 @@ impl OxideKernel {
pub fn config(&self) -> &KernelConfig {
&self.config
}
pub fn spawn(&self) -> VmGuard<'_> {
todo!("implemented in 07-07")
}
}
#[cfg(test)]
@@ -162,4 +173,14 @@ mod tests {
assert!(KernelConfig::full().warmup_builtin_ic);
assert_eq!(KernelConfig::full().max_pool_size, None);
}
#[test]
fn test_kernel_spawn() {
let kernel = Arc::new(OxideKernel::new(KernelConfig::minimal()));
kernel.init_vm_pool();
let guard = kernel.spawn();
drop(guard);
let guard2 = kernel.spawn();
drop(guard2);
}
}