Added thread manager

This commit is contained in:
Quentin Legot 2023-03-08 21:10:51 +01:00
parent 82731cabcf
commit 4ca85b54d4
6 changed files with 119 additions and 104 deletions

View File

@ -4,4 +4,5 @@ pub mod scheduler;
pub mod mgerror; pub mod mgerror;
pub mod system; pub mod system;
mod ucontext; mod ucontext;
mod synch; mod synch;
mod thread_manager;

View File

@ -1,12 +1,6 @@
use std::rc::Rc; use crate::simulator::machine::Machine;
use crate::{
kernel::{ use super::thread_manager::ThreadManager;
thread::Thread,
scheduler::Scheduler
},
utility::list::List,
simulator::machine::Machine
};
/// # System /// # System
/// ///
@ -19,26 +13,24 @@ use crate::{
/// - The thread to be destroyed next /// - The thread to be destroyed next
/// - The scheduler which acts upon these threads /// - The scheduler which acts upon these threads
#[derive(PartialEq)] #[derive(PartialEq)]
pub struct System { pub struct System<'a> {
g_machine: Machine, g_machine: Machine,
g_current_thread: Option<Thread>, thread_manager: ThreadManager<'a>
g_thread_to_be_destroyed: Option<Thread>,
g_alive: List<Rc<Thread>>,
g_scheduler: Scheduler
} }
impl System { impl<'a> System<'a> {
/// System constructor /// System constructor
pub fn new(machine: Machine, scheduler: Scheduler) -> Self { pub fn new(machine: Machine) -> System<'a> {
Self { Self {
g_machine: machine, g_machine: machine,
g_current_thread: None, thread_manager: ThreadManager::new()
g_thread_to_be_destroyed: None,
g_alive: List::new(),
g_scheduler: scheduler
} }
} }
pub fn freeze(&'a mut self) {
self.thread_manager.system.set(Option::Some(self));
}
// GETTERS // GETTERS
@ -49,28 +41,6 @@ impl System {
&mut self.g_machine &mut self.g_machine
} }
/// Currently running thread
pub fn get_g_current_thread(&mut self) -> &mut Option<Thread> {
&mut self.g_current_thread
}
/// Thread to be destroyed by [...]
///
/// TODO: Finish the comment with the relevant value
pub fn get_g_thread_to_be_destroyed(&mut self) -> &mut Option<Thread> {
&mut self.g_thread_to_be_destroyed
}
/// List of alive threads
pub fn get_g_alive(&mut self) -> &mut List<Rc<Thread>> {
&mut self.g_alive
}
/// Current scheduler
pub fn g_scheduler(&mut self) -> &mut Scheduler {
&mut self.g_scheduler
}
// Setters // Setters
/// Assign a machine to the system /// Assign a machine to the system
@ -78,21 +48,6 @@ impl System {
self.g_machine = machine self.g_machine = machine
} }
/// Set currently running thread
pub fn set_g_current_thread(&mut self, thread: Option<Thread>) {
self.g_current_thread = thread
}
/// Set thread to be destroyed next
pub fn set_g_thread_to_be_destroyed(&mut self, thread: Option<Thread>) {
self.g_thread_to_be_destroyed = thread
}
/// Set Scheduler which will manage the threads
pub fn set_g_scheduler(&mut self, scheduler: Scheduler) {
self.g_scheduler = scheduler
}
} }
#[derive(PartialEq)] #[derive(PartialEq)]

View File

@ -1,6 +1,4 @@
use std::{sync::Arc, rc::Rc}; use super::{process::Process, system::ObjectType};
use super::{process::Process, mgerror::ErrorCode, system::{ObjectType, System}, ucontext::UContextT};
use crate::{simulator::machine::{NUM_INT_REGS, NUM_FP_REGS, STACK_REG}}; use crate::{simulator::machine::{NUM_INT_REGS, NUM_FP_REGS, STACK_REG}};
const SIMULATORSTACKSIZE: usize = 32 * 1024; const SIMULATORSTACKSIZE: usize = 32 * 1024;
@ -16,18 +14,17 @@ struct ThreadContext {
#[derive(PartialEq)] #[derive(PartialEq)]
pub struct Thread { pub struct Thread {
name: String, name: String,
process: Option<Process>, pub process: Option<Process>,
// simulation_context: UContextT, // simulation_context: UContextT,
thread_context: ThreadContext, thread_context: ThreadContext,
stack_pointer: i32, stack_pointer: i32,
object_type: ObjectType, object_type: ObjectType
system: Rc<System>
} }
impl Thread { impl Thread {
/// Thread constructor /// Thread constructor
pub fn new(name: String, system: Rc<System>) -> Self { pub fn new(name: String) -> Self {
Self { Self {
name, name,
process: None, process: None,
@ -39,31 +36,16 @@ impl Thread {
}, },
stack_pointer: 0, stack_pointer: 0,
object_type: ObjectType::ThreadType, object_type: ObjectType::ThreadType,
system
} }
} }
/// Start a thread, attaching it to a process pub fn init_thread_context(&mut self, initial_pc_reg: i64, initial_sp: i64, arg: i64) {
pub fn start(mut self, owner: Process, func: i64, arg: i64) -> Result<(), ErrorCode> {
self.process = Option::Some(owner);
let ptr = 0; // todo addrspace
self.init_thread_context(func, ptr, arg);
let base_stack_addr: [i8; SIMULATORSTACKSIZE] = [0; SIMULATORSTACKSIZE]; // todo AllocBoundedArray
self.init_simulator_context(base_stack_addr);
self.process.as_mut().unwrap().num_thread += 1;
let this = Rc::new(self);
self.system.get_g_alive().push(Rc::clone(&this));
self.system.g_scheduler().ready_to_run(Rc::clone(&this));
Result::Ok(())
}
fn init_thread_context(&mut self, initial_pc_reg: i64, initial_sp: i64, arg: i64) {
self.thread_context.pc = initial_pc_reg; self.thread_context.pc = initial_pc_reg;
self.thread_context.int_registers[10] = arg; self.thread_context.int_registers[10] = arg;
self.thread_context.int_registers[STACK_REG] = initial_sp; self.thread_context.int_registers[STACK_REG] = initial_sp;
} }
fn init_simulator_context(&self, base_stack_addr: [i8; SIMULATORSTACKSIZE]) { pub fn init_simulator_context(&self, base_stack_addr: [i8; SIMULATORSTACKSIZE]) {
// let res = self.simulation_context.get_context(); // let res = self.simulation_context.get_context();
// if res != 0 { // if res != 0 {
// panic!("getcontext returns non-zero value {}", res); // panic!("getcontext returns non-zero value {}", res);
@ -78,20 +60,6 @@ impl Thread {
// self.simulation_context.stackBottom[0] = STACK_FENCEPOST; // self.simulation_context.stackBottom[0] = STACK_FENCEPOST;
} }
/// Wait for another thread to finish its execution
pub fn join(&self, id_thread: Rc<Thread>) {
while self.system.get_g_alive().contains(&Rc::clone(&id_thread)) {
self.t_yield();
}
}
/// Relinquish the CPU if any other thread is runnable.
///
/// Cannot use yield as a function name -> reserved name in rust
pub fn t_yield(&self) {
todo!();
}
/// Put the thread to sleep and relinquish the processor /// Put the thread to sleep and relinquish the processor
pub fn sleep(&self) { pub fn sleep(&self) {
todo!(); todo!();

View File

@ -0,0 +1,95 @@
use std::{rc::Rc, cell::Cell};
use crate::utility::list::List;
use super::{scheduler::Scheduler, thread::Thread, system::System, mgerror::ErrorCode, process::Process};
const SIMULATORSTACKSIZE: usize = 32 * 1024;
#[derive(PartialEq)]
pub struct ThreadManager<'a> {
pub g_current_thread: Option<Thread>,
pub g_thread_to_be_destroyed: Option<Thread>,
pub g_alive: List<Rc<Thread>>,
pub g_scheduler: Scheduler,
pub system: Cell<Option<&'a System<'a>>>
}
impl<'a> ThreadManager<'a> {
pub fn new() -> Self {
Self {
g_current_thread: Option::None,
g_thread_to_be_destroyed: Option::None,
g_alive: List::new(),
g_scheduler: Scheduler::new(),
system: Cell::new(None)
}
}
/// Start a thread, attaching it to a process
pub fn start_thread(&mut self, thread: &mut Thread, owner: Process, func_pc: i64, argument: i64) -> Result<(), ErrorCode> {
thread.process = Option::Some(owner);
let ptr = 0; // todo addrspace
thread.init_thread_context(func_pc, ptr, argument);
let base_stack_addr: [i8; SIMULATORSTACKSIZE] = [0; SIMULATORSTACKSIZE]; // todo AllocBoundedArray
thread.init_simulator_context(base_stack_addr);
thread.process.as_mut().unwrap().num_thread += 1;
let thread_m = Rc::new(thread);
// self.get_g_alive().push(Rc::clone(thread));
// self.g_scheduler().ready_to_run(Rc::clone(&thread));
Result::Ok(())
}
/// Wait for another thread to finish its execution
pub fn thread_join(&mut self, id_thread: Rc<Thread>) {
while self.get_g_alive().contains(&Rc::clone(&id_thread)) {
self.thread_yield();
}
}
/// Relinquish the CPU if any other thread is runnable.
///
/// Cannot use yield as a function name -> reserved name in rust
pub fn thread_yield(&mut self) {
todo!();
}
/// Currently running thread
pub fn get_g_current_thread(&mut self) -> &mut Option<Thread> {
&mut self.g_current_thread
}
/// Thread to be destroyed by [...]
///
/// TODO: Finish the comment with the relevant value
pub fn get_g_thread_to_be_destroyed(&mut self) -> &mut Option<Thread> {
&mut self.g_thread_to_be_destroyed
}
/// List of alive threads
pub fn get_g_alive(&mut self) -> &mut List<Rc<Thread>> {
&mut self.g_alive
}
/// Current scheduler
pub fn g_scheduler(&mut self) -> &mut Scheduler {
&mut self.g_scheduler
}
/// Set currently running thread
pub fn set_g_current_thread(&mut self, thread: Option<Thread>) {
self.g_current_thread = thread
}
/// Set thread to be destroyed next
pub fn set_g_thread_to_be_destroyed(&mut self, thread: Option<Thread>) {
self.g_thread_to_be_destroyed = thread
}
/// Set Scheduler which will manage the threads
pub fn set_g_scheduler(&mut self, scheduler: Scheduler) {
self.g_scheduler = scheduler
}
}

View File

@ -1,5 +1,5 @@
use std::mem::MaybeUninit; // use std::mem::MaybeUninit;
/// Safe wrapper for ucontext_t struct of linux-gnu libc /// Safe wrapper for ucontext_t struct of linux-gnu libc
/// ///

View File

@ -2,14 +2,10 @@ mod simulator;
mod kernel; mod kernel;
pub mod utility; pub mod utility;
use kernel::{ use kernel::system::System;
scheduler::Scheduler,
system::System
};
use simulator::machine::Machine; use simulator::machine::Machine;
fn main() { fn main() {
let machine = Machine::_init_machine(); let machine = Machine::_init_machine();
let scheduler = Scheduler::new(); let _system = System::new(machine);
let system = System::new(machine, scheduler);
} }