diff --git a/src/kernel/system.rs b/src/kernel/system.rs index 5f04b2c..1fd2de9 100644 --- a/src/kernel/system.rs +++ b/src/kernel/system.rs @@ -2,11 +2,12 @@ use std::{sync::{RwLock, Arc}}; use lazy_static::lazy_static; -use crate::{kernel::{thread::Thread, scheduler::Scheduler}, utility::list::List}; +use crate::{kernel::{thread::Thread, scheduler::Scheduler}, utility::list::List, simulator::machine::Machine}; extern crate lazy_static; lazy_static! { + pub static ref G_MACHINE: RwLock = RwLock::new(Machine::_init_machine()); pub static ref G_CURRENT_THREAD: RwLock> = RwLock::new(Option::None); pub static ref G_THREAD_TO_BE_DESTROYED: RwLock> = RwLock::new(Option::None); pub static ref G_ALIVE: RwLock>> = RwLock::new(List::new()); diff --git a/src/kernel/thread.rs b/src/kernel/thread.rs index 000877a..0a163d0 100644 --- a/src/kernel/thread.rs +++ b/src/kernel/thread.rs @@ -1,7 +1,7 @@ use std::{sync::Arc}; use super::{process::Process, mgerror::ErrorCode, system::{ObjectType, G_ALIVE, G_SCHEDULER}, 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}, kernel::system::{G_MACHINE, G_THREAD_TO_BE_DESTROYED}}; const SIMULATORSTACKSIZE: usize = 32 * 1024; const STACK_FENCEPOST: u32 = 0xdeadbeef; @@ -116,7 +116,34 @@ impl Thread { } /// Finish the execution of the thread and prepare its deallocation - pub fn finish(&self) { + pub fn finish(mut self) { + match G_MACHINE.write() { + Ok(mut machine) => { + let old_status = machine.interrupt.set_status(crate::simulator::interrupt::InterruptStatus::InterruptOff); + match G_ALIVE.write() { + Ok(alive) => { + // todo alive.remove(T) à implémenter dans List + }, + Err(err) => { + panic!("RwLock is poisoned: {}", err); + } + } + match G_THREAD_TO_BE_DESTROYED.write() { + Ok(mut thread_to_be_destroyed) => { + thread_to_be_destroyed.replace(self); + }, + Err(err) => { + panic!("RwLock is poisoned: {}", err); + } + } + // self.sleep(); + machine.interrupt.set_status(old_status); + }, + Err(err) => { + panic!("RwLock is poisoned: {}", err); + } + } + todo!(); } diff --git a/src/kernel/ucontext.rs b/src/kernel/ucontext.rs index 89b847d..6217a1d 100644 --- a/src/kernel/ucontext.rs +++ b/src/kernel/ucontext.rs @@ -1,8 +1,6 @@ use std::mem::MaybeUninit; -use libc::{ucontext_t, getcontext, setcontext, makecontext}; - /// Safe wrapper for ucontext_t struct of linux-gnu libc /// /// setcontext and getcontext are unsafe function, this wrap unsafe libc functions @@ -13,7 +11,7 @@ use libc::{ucontext_t, getcontext, setcontext, makecontext}; #[derive(PartialEq)] pub struct UContextT { #[cfg(not(target_os = "windows"))] // struct non disponible sur la libc sur windows - pub buf: ucontext_t, + pub buf: lib::ucontext_t, pub stackBottom: Vec } @@ -22,7 +20,7 @@ impl UContextT { pub fn new() -> Self { let mut context = MaybeUninit::::uninit(); - unsafe { getcontext(context.as_mut_ptr()) }; + unsafe { lib::getcontext(context.as_mut_ptr()) }; Self { buf: unsafe { context.assume_init() }, stackBottom: Vec::default(), @@ -34,7 +32,7 @@ impl UContextT { /// Use `man getcontext` for more informations pub fn get_context(&mut self) -> i32 { unsafe { - getcontext(&mut self.buf) + lib::getcontext(&mut self.buf) } } @@ -43,13 +41,13 @@ impl UContextT { /// Use `man setcontext` for more informations pub fn set_context(&mut self) -> i32 { unsafe { - setcontext(&self.buf) + lib::setcontext(&self.buf) } } pub fn make_context(&mut self, func: extern "C" fn(), args: i32) { unsafe { - makecontext(&mut self.buf, func, args) + lib::makecontext(&mut self.buf, func, args) } } @@ -59,7 +57,9 @@ impl UContextT { impl UContextT { pub fn new() -> Self { - Self {} + Self { + stackBottom: Vec::default() + } } pub fn get_context(&mut self) { diff --git a/src/simulator/interrupt.rs b/src/simulator/interrupt.rs index 8f71885..8f08312 100644 --- a/src/simulator/interrupt.rs +++ b/src/simulator/interrupt.rs @@ -1,6 +1,6 @@ -struct Interrupt { +pub struct Interrupt { level: InterruptStatus } diff --git a/src/simulator/machine.rs b/src/simulator/machine.rs index 6523628..59ab1b8 100644 --- a/src/simulator/machine.rs +++ b/src/simulator/machine.rs @@ -1,6 +1,6 @@ use std::{ops::{Add, Sub}, io::Write}; -use super::{decode::{Instruction, decode}}; +use super::{decode::{Instruction, decode}, interrupt::Interrupt}; use super::global::*; use std::fs::File; @@ -71,7 +71,8 @@ pub struct Machine { pub int_reg : Register, pub fp_reg : Register, pub main_memory : [u8 ; MEM_SIZE], - pub shiftmask : [u64 ; 64] + pub shiftmask : [u64 ; 64], + pub interrupt: Interrupt, // futur taille à calculer int memSize = g_cfg->NumPhysPages * g_cfg->PageSize; //creer une struct cfg(configuration) qui s'initialise avec valeur dans un fichier cfg } @@ -95,7 +96,8 @@ impl Machine { int_reg : Register::::init(), fp_reg : Register::::init(), main_memory : [0 ; MEM_SIZE], - shiftmask + shiftmask, + interrupt: Interrupt::new() } }