fix: use try_lock in data accessors to prevent component mutex deadlock

This commit is contained in:
Jan-Peter von Hunnius 2026-02-27 07:51:37 +01:00
parent d1cc176cc7
commit 14060e15e5

View file

@ -700,10 +700,12 @@ impl ConfigurableSystem {
/// Get CPU register data from Intel 4004 CPU
/// Returns: CpuRegisterData if Intel 4004 CPU is found, None otherwise
/// Uses try_lock() to avoid blocking when component is busy
pub fn get_cpu_register_data(&self) -> Option<CpuRegisterData> {
// Look for Intel 4004 CPU components
for (_name, component) in &self.components {
if let Ok(comp) = component.lock() {
match component.try_lock() {
Ok(comp) => {
// Try to downcast to Intel4004
if let Some(cpu) = comp.as_any().downcast_ref::<Intel4004>() {
let mut index_registers = [0u8; 16];
@ -720,6 +722,15 @@ impl ConfigurableSystem {
});
}
}
Err(std::sync::TryLockError::WouldBlock) => {
// Component is busy, continue to next component
continue;
}
Err(std::sync::TryLockError::Poisoned(_)) => {
// Handle poisoned mutex - continue to next component
continue;
}
}
}
None
}
@ -727,11 +738,13 @@ impl ConfigurableSystem {
/// Get RAM data from Intel 4002 RAM chips
/// Parameters: max_rows - Maximum number of rows to return per bank
/// Returns: Vector of (chip_name, banks) where each bank is a vector of nibbles
/// Uses try_lock() to avoid blocking when component is busy
pub fn get_ram_data(&self, max_rows: usize) -> RamData {
let mut ram_data = Vec::new();
for (name, component) in &self.components {
if let Ok(comp) = component.lock() {
match component.try_lock() {
Ok(comp) => {
// Try to downcast to Intel4002
if let Some(ram) = comp.as_any().downcast_ref::<Intel4002>() {
let mut banks = Vec::new();
@ -756,6 +769,15 @@ impl ConfigurableSystem {
ram_data.push((name.clone(), banks));
}
}
Err(std::sync::TryLockError::WouldBlock) => {
// Component is busy, skip to next component
continue;
}
Err(std::sync::TryLockError::Poisoned(_)) => {
// Handle poisoned mutex - skip to next component
continue;
}
}
}
ram_data
@ -764,11 +786,13 @@ impl ConfigurableSystem {
/// Get ROM data from Intel 4001 ROM chips
/// Parameters: max_rows - Maximum number of bytes to return per chip
/// Returns: Vector of (chip_name, bytes)
/// Uses try_lock() to avoid blocking when component is busy
pub fn get_rom_data(&self, max_rows: usize) -> RomData {
let mut rom_data = Vec::new();
for (name, component) in &self.components {
if let Ok(comp) = component.lock() {
match component.try_lock() {
Ok(comp) => {
// Try to downcast to Intel4001
if let Some(rom) = comp.as_any().downcast_ref::<Intel4001>() {
let mut data = Vec::new();
@ -782,6 +806,15 @@ impl ConfigurableSystem {
rom_data.push((name.clone(), data));
}
}
Err(std::sync::TryLockError::WouldBlock) => {
// Component is busy, skip to next component
continue;
}
Err(std::sync::TryLockError::Poisoned(_)) => {
// Handle poisoned mutex - skip to next component
continue;
}
}
}
rom_data