diff --git a/Cargo.lock b/Cargo.lock index e3bcbe3..9bb9ed5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,6 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.139" +version = "0.2.140" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" +checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c" diff --git a/Cargo.toml b/Cargo.toml index 1d66884..95cb216 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,6 @@ edition = "2021" [dependencies] libc = { version = "0.2.139", features = ["extra_traits"] } + +[registries.crates-io] +protocol = "sparse" \ No newline at end of file diff --git a/src/kernel/thread.rs b/src/kernel/thread.rs index 370c1e2..92f8986 100644 --- a/src/kernel/thread.rs +++ b/src/kernel/thread.rs @@ -16,7 +16,7 @@ macro_rules! get_new_thread { pub struct ThreadContext { pub int_registers: [i64; NUM_INT_REGS], pub float_registers: [f32; NUM_FP_REGS], - pc: i64, + pub pc: i64, } #[derive(PartialEq, Debug)] diff --git a/src/kernel/thread_manager.rs b/src/kernel/thread_manager.rs index fc0a614..0786fa1 100644 --- a/src/kernel/thread_manager.rs +++ b/src/kernel/thread_manager.rs @@ -84,7 +84,7 @@ impl ThreadManager { } /// Start a thread, attaching it to a process - pub fn start_thread(&mut self, thread: Rc>, owner: Process, func_pc: i64, argument: i64) -> Result<(), ErrorCode> { + pub fn start_thread(&mut self, thread: Rc>, owner: Process, func_pc: i64, argument: i64) { let mut thread_m = thread.borrow_mut(); assert_eq!(thread_m.process, Option::None); thread_m.process = Option::Some(owner); @@ -95,7 +95,6 @@ impl ThreadManager { thread_m.process.as_mut().unwrap().num_thread += 1; self.get_g_alive().push(Rc::clone(&thread)); self.ready_to_run(Rc::clone(&thread)); - Result::Ok(()) } /// Wait for another thread to finish its execution @@ -193,7 +192,30 @@ impl ThreadManager { #[cfg(test)] mod test { + use std::{rc::Rc, cell::RefCell}; - + use crate::{simulator::machine::Machine, kernel::{system::System, thread::Thread, process::Process}}; + + #[test] + fn test_thread_context() { + let mut machine = Machine::init_machine(); + let mut system = System::default(); + let thread1 = Thread::new("th1"); + let thread1 = Rc::new(RefCell::new(thread1)); + system.get_thread_manager().get_g_alive().push(Rc::clone(&thread1)); + let owner = Process { num_thread: 0 }; + system.get_thread_manager().start_thread(Rc::clone(&thread1), owner, thread1_func as i64, 0); + assert_eq!(thread1.borrow_mut().thread_context.pc, thread1_func as i64); + let to_run = system.get_thread_manager().find_next_to_run().unwrap(); + assert_eq!(to_run, Rc::clone(&thread1)); + assert!(system.get_thread_manager().get_g_alive().contains(&Rc::clone(&thread1))); + system.get_thread_manager().switch_to(&mut machine, Rc::clone(&to_run)); + + println!("{:#?}", thread1.borrow_mut().thread_context); + } + + fn thread1_func() { + println!("Hello"); + } } \ No newline at end of file