First iteration (not complete) of a test in thread_manager

This commit is contained in:
Quentin Legot 2023-03-22 18:30:31 +01:00
parent 6edb88f337
commit 6d0477153b
4 changed files with 31 additions and 6 deletions

4
Cargo.lock generated
View File

@ -11,6 +11,6 @@ dependencies = [
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.139" version = "0.2.140"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"

View File

@ -5,3 +5,6 @@ edition = "2021"
[dependencies] [dependencies]
libc = { version = "0.2.139", features = ["extra_traits"] } libc = { version = "0.2.139", features = ["extra_traits"] }
[registries.crates-io]
protocol = "sparse"

View File

@ -16,7 +16,7 @@ macro_rules! get_new_thread {
pub struct ThreadContext { pub struct ThreadContext {
pub int_registers: [i64; NUM_INT_REGS], pub int_registers: [i64; NUM_INT_REGS],
pub float_registers: [f32; NUM_FP_REGS], pub float_registers: [f32; NUM_FP_REGS],
pc: i64, pub pc: i64,
} }
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]

View File

@ -84,7 +84,7 @@ impl ThreadManager {
} }
/// Start a thread, attaching it to a process /// Start a thread, attaching it to a process
pub fn start_thread(&mut self, thread: Rc<RefCell<Thread>>, owner: Process, func_pc: i64, argument: i64) -> Result<(), ErrorCode> { pub fn start_thread(&mut self, thread: Rc<RefCell<Thread>>, owner: Process, func_pc: i64, argument: i64) {
let mut thread_m = thread.borrow_mut(); let mut thread_m = thread.borrow_mut();
assert_eq!(thread_m.process, Option::None); assert_eq!(thread_m.process, Option::None);
thread_m.process = Option::Some(owner); thread_m.process = Option::Some(owner);
@ -95,7 +95,6 @@ impl ThreadManager {
thread_m.process.as_mut().unwrap().num_thread += 1; thread_m.process.as_mut().unwrap().num_thread += 1;
self.get_g_alive().push(Rc::clone(&thread)); self.get_g_alive().push(Rc::clone(&thread));
self.ready_to_run(Rc::clone(&thread)); self.ready_to_run(Rc::clone(&thread));
Result::Ok(())
} }
/// Wait for another thread to finish its execution /// Wait for another thread to finish its execution
@ -193,7 +192,30 @@ impl ThreadManager {
#[cfg(test)] #[cfg(test)]
mod 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");
}
} }