feat: migrate to parking_lot mutex and add system control architecture
This commit is contained in:
parent
14060e15e5
commit
54144d10e9
29 changed files with 2387 additions and 1130 deletions
|
|
@ -6,9 +6,10 @@
|
|||
|
||||
#![cfg(test)]
|
||||
|
||||
use parking_lot::Mutex;
|
||||
use rusty_emu::gui::GuiApp;
|
||||
use rusty_emu::system_config::{ConfigurableSystem, SystemFactory};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
|
|
@ -36,7 +37,8 @@ mod gui_integration_test_utils {
|
|||
/// Note: This function acquires the lock internally. Do NOT call it while holding the lock.
|
||||
pub fn simulate_gui_updates(system: &Arc<Mutex<ConfigurableSystem>>) {
|
||||
// Simulate the kind of updates that would happen in the GUI thread
|
||||
if let Ok(system_guard) = system.lock() {
|
||||
let system_guard = system.lock();
|
||||
{
|
||||
// Simulate some system activity
|
||||
let _ = system_guard.is_running(); // Access system state
|
||||
}
|
||||
|
|
@ -48,14 +50,19 @@ mod gui_integration_test_utils {
|
|||
|
||||
// Spawn a thread that accesses the system
|
||||
let handle = thread::spawn(move || {
|
||||
// Try to acquire the lock - return true if successful (regardless of is_running value)
|
||||
system_clone.lock().is_ok()
|
||||
// Try to acquire the lock - return true if successful (parking_lot::lock always succeeds)
|
||||
let _guard = system_clone.lock();
|
||||
true
|
||||
});
|
||||
|
||||
// Main thread also accesses the system
|
||||
let main_thread_result = system.lock().is_ok();
|
||||
// Main thread also accesses the system - but release lock before joining to avoid deadlock
|
||||
{
|
||||
let _guard = system.lock();
|
||||
// Access the system while holding the lock
|
||||
}
|
||||
let main_thread_result = true;
|
||||
|
||||
// Wait for spawned thread
|
||||
// Wait for spawned thread (lock is released, so no deadlock)
|
||||
let spawned_thread_result = handle.join().unwrap_or(false);
|
||||
|
||||
// Both threads should be able to acquire the lock without deadlock
|
||||
|
|
@ -150,7 +157,8 @@ mod gui_system_integration_tests {
|
|||
thread::spawn(move || {
|
||||
// Simulate GUI update pattern
|
||||
for _ in 0..10 {
|
||||
if let Ok(_system_guard) = system_clone.lock() {
|
||||
let _system_guard = system_clone.lock();
|
||||
{
|
||||
// Simulate some processing time
|
||||
thread::sleep(Duration::from_micros(10));
|
||||
}
|
||||
|
|
@ -196,7 +204,7 @@ mod gui_system_integration_tests {
|
|||
// Test the polling mechanism that GUI would use
|
||||
for i in 0..10 {
|
||||
{
|
||||
let system_guard = system.lock().unwrap();
|
||||
let system_guard = system.lock();
|
||||
let is_running = system_guard.is_running();
|
||||
|
||||
// Simulate state updates that would happen in GUI
|
||||
|
|
@ -221,7 +229,7 @@ mod gui_system_integration_tests {
|
|||
// In a real implementation, this would test actual memory access
|
||||
|
||||
{
|
||||
let system_guard = system.lock().unwrap();
|
||||
let system_guard = system.lock();
|
||||
// Test that system provides memory information
|
||||
let _ = system_guard.get_system_info(); // Access system info
|
||||
} // system_guard is dropped here
|
||||
|
|
@ -241,7 +249,7 @@ mod gui_system_integration_tests {
|
|||
|
||||
// Test register state structure for GUI integration
|
||||
{
|
||||
let _system_guard = system.lock().unwrap();
|
||||
let _system_guard = system.lock();
|
||||
// In a real implementation, this would access actual CPU registers
|
||||
// For now, we test the GUI state structure
|
||||
}
|
||||
|
|
@ -262,7 +270,7 @@ mod gui_system_integration_tests {
|
|||
|
||||
// Test component state monitoring for GUI
|
||||
{
|
||||
let system_guard = system.lock().unwrap();
|
||||
let system_guard = system.lock();
|
||||
let components = system_guard.get_components();
|
||||
|
||||
// Test that components are accessible (GUI would monitor these)
|
||||
|
|
@ -434,18 +442,19 @@ mod gui_error_handling_tests {
|
|||
// Hold the lock for a short time to simulate contention
|
||||
let system_clone = Arc::clone(&system);
|
||||
let lock_handle = thread::spawn(move || {
|
||||
if let Ok(_system_guard) = system_clone.lock() {
|
||||
let _system_guard = system_clone.lock();
|
||||
{
|
||||
thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
});
|
||||
|
||||
// Try to access from main thread (should not deadlock)
|
||||
let access_result = system.try_lock().is_ok();
|
||||
let access_result = system.try_lock().is_some();
|
||||
|
||||
lock_handle.join().unwrap();
|
||||
|
||||
// Should be able to access after the lock is released
|
||||
assert!(access_result || system.try_lock().is_ok());
|
||||
assert!(access_result || system.try_lock().is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -481,7 +490,7 @@ mod gui_real_time_tests {
|
|||
|
||||
for i in 0..20 {
|
||||
{
|
||||
let _system_guard = system.lock().unwrap();
|
||||
let _system_guard = system.lock();
|
||||
// Simulate GUI state polling
|
||||
update_count += 1;
|
||||
|
||||
|
|
@ -535,7 +544,7 @@ mod gui_real_time_tests {
|
|||
// For now, we test the data structures are compatible
|
||||
|
||||
{
|
||||
let _system_guard = system.lock().unwrap();
|
||||
let _system_guard = system.lock();
|
||||
// Test memory data structure compatibility
|
||||
}
|
||||
|
||||
|
|
@ -559,7 +568,7 @@ mod gui_real_time_tests {
|
|||
let system = create_test_system();
|
||||
|
||||
{
|
||||
let _system_guard = system.lock().unwrap();
|
||||
let _system_guard = system.lock();
|
||||
}
|
||||
|
||||
let mut gui_state = rusty_emu::gui::state::GuiState::new();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue