Moving sc_new_thread to it own function

This commit is contained in:
Quentin Legot 2023-04-12 13:55:32 +02:00
parent 0d70751279
commit e8629b1ebf

View File

@ -169,38 +169,7 @@ fn syscall(machine: &mut Machine, system: &mut System) -> Result<MachineOk, Mach
},
SC_SEEK => todo!(),
SC_CLOSE => todo!(),
SC_NEW_THREAD => {
// Get the address of the string for the name of the thread
let name_addr = machine.read_int_register(10) as usize;
// Get the pointer of the function to be executed in the new thread
let func = machine.read_int_register(11);
// Get function parameters
let args = machine.read_int_register(12);
// get string name
let name_size = get_length_param(name_addr, machine);
let thread_name: String = get_string_param(name_addr, name_size, machine).into_iter().collect();
let n_thread = Thread::new(thread_name.as_str());
let n_thread = Rc::new(RefCell::new(n_thread));
let current_thread = match system.get_thread_manager().get_g_current_thread() {
Some(th) => {
Rc::clone(th)
},
None => {
return Err("Current thread is none")?;
}
};
let current_thread = current_thread.borrow_mut();
if let Some(process) = current_thread.get_process_owner() {
system.get_thread_manager().start_thread(n_thread, Rc::clone(&process), func as u64, current_thread.thread_context.int_registers[2] as u64, args);
// TODO changé la valeur de sp quand on supportera les addresses virtuels
// machine.write_fp_register(10, tid); // tid obtenu en faisant int32_t tid = g_object_addrs->AddObject(ptThread);
Ok(MachineOk::Ok)
} else {
return Err("Process owner of current thread is none")?;
}
},
SC_NEW_THREAD => sc_new_thread(machine, system),
SC_YIELD => todo!(),
SC_PERROR => todo!(),
SC_P => sc_p(machine, system),
@ -256,6 +225,38 @@ fn sc_sem_create(machine: &mut Machine) -> Result<(), Error> {
}
}
fn sc_new_thread(machine: &mut Machine, system: &mut System) -> Result<(), Error> {
// Get the address of the string for the name of the thread
let name_addr = machine.read_int_register(10) as usize;
// Get the pointer of the function to be executed in the new thread
let func = machine.read_int_register(11);
// Get function parameters
let args = machine.read_int_register(12);
// get string name
let name_size = get_length_param(name_addr, machine);
let thread_name: String = get_string_param(name_addr, name_size, machine).into_iter().collect();
let n_thread = Thread::new(thread_name.as_str());
let n_thread = Rc::new(RefCell::new(n_thread));
let current_thread = match system.get_thread_manager().get_g_current_thread() {
Some(th) => {
Rc::clone(th)
},
None => {
return Err("Current thread is none")?;
}
};
let current_thread = current_thread.borrow_mut();
if let Some(process) = current_thread.get_process_owner() {
system.get_thread_manager().start_thread(n_thread, Rc::clone(&process), func as u64, current_thread.thread_context.int_registers[2] as u64, args);
// TODO changé la valeur de sp quand on supportera les addresses virtuels
// machine.write_fp_register(10, tid); // tid obtenu en faisant int32_t tid = g_object_addrs->AddObject(ptThread);
Ok(MachineOk::Ok)
} else {
return Err("Process owner of current thread is none")?;
}
}
fn get_length_param(addr: usize, machine: & Machine) -> usize{
let mut i = 0;
let mut c = 1;