Changed all reference to thread with an RefCell to enforce mutability
This commit is contained in:
parent
f586c56a0b
commit
e1ba3f6078
@ -1,31 +1,31 @@
|
||||
|
||||
/// Error enum, use it with Result<YourSucessStruct, **ErrorCode**>
|
||||
pub enum ErrorCode {
|
||||
INC_ERROR,
|
||||
OPENFILE_ERROR,
|
||||
EXEC_FILE_FORMAT_ERROR,
|
||||
OUT_OF_MEMORY,
|
||||
IncError,
|
||||
OpenfileError,
|
||||
ExecFileFormatError,
|
||||
OutOfMemory,
|
||||
|
||||
OUT_OF_DISK,
|
||||
ALREADY_IN_DIRECTORY,
|
||||
INEXIST_FILE_ERROR,
|
||||
INEXIST_DIRECTORY_ERROR,
|
||||
NOSPACE_IN_DIRECTORY,
|
||||
NOT_A_FILE,
|
||||
NOT_A_DIRECTORY,
|
||||
DIRECTORY_NOT_EMPTY,
|
||||
INVALID_COUNTER,
|
||||
OutOfDisk,
|
||||
AlreadyInDirectory,
|
||||
InexistFileError,
|
||||
InexistDirectoryError,
|
||||
NospaceInDirectory,
|
||||
NotAFile,
|
||||
NotADirectory,
|
||||
DirectoryNotEmpty,
|
||||
InvalidCounter,
|
||||
|
||||
/* Invalid typeId fields: */
|
||||
INVALID_SEMAPHORE_ID,
|
||||
INVALID_LOCK_ID,
|
||||
INVALID_CONDITION_ID,
|
||||
INVALID_FILE_ID,
|
||||
INVALID_THREAD_ID,
|
||||
InvalidSemaphoreId,
|
||||
InvalidLockId,
|
||||
InvalidConditionId,
|
||||
InvalidFileId,
|
||||
InvalidThreadId,
|
||||
|
||||
/* Other messages */
|
||||
WRONG_FILE_ENDIANESS,
|
||||
NO_ACIA,
|
||||
WrongFileEndianess,
|
||||
NoAcia,
|
||||
|
||||
NUMMSGERROR /* Must always be last */
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::utility::list::List;
|
||||
@ -7,7 +8,7 @@ use super::system::System;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub struct Scheduler {
|
||||
ready_list: List<Rc<Thread>>
|
||||
ready_list: List<Rc<RefCell<Thread>>>
|
||||
}
|
||||
|
||||
impl Scheduler {
|
||||
@ -28,7 +29,7 @@ impl Scheduler {
|
||||
/// ## Pamameter
|
||||
///
|
||||
/// **thread** is the thread to be put on the read list
|
||||
pub fn ready_to_run(&mut self, thread: Rc<Thread>) {
|
||||
pub fn ready_to_run(&mut self, thread: Rc<RefCell<Thread>>) {
|
||||
self.ready_list.push(thread);
|
||||
}
|
||||
|
||||
@ -38,7 +39,7 @@ impl Scheduler {
|
||||
/// Thread is removed from the ready list.
|
||||
///
|
||||
/// **return** Thread thread to be scheduled
|
||||
pub fn find_next_to_run(&mut self) -> Option<Rc<Thread>> {
|
||||
pub fn find_next_to_run(&mut self) -> Option<Rc<RefCell<Thread>>> {
|
||||
self.ready_list.pop()
|
||||
}
|
||||
|
||||
|
@ -2,26 +2,29 @@ use crate::utility::list::List;
|
||||
use crate::kernel::thread::Thread;
|
||||
use crate::simulator::interrupt::InterruptStatus::InterruptOff;
|
||||
use crate::simulator::machine::Machine;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
|
||||
use super::scheduler::Scheduler;
|
||||
use super::thread_manager::ThreadManager;
|
||||
|
||||
pub struct Semaphore{
|
||||
pub struct Semaphore<'t> {
|
||||
|
||||
counter:i32,
|
||||
waiting_queue:List<Rc<Thread>>
|
||||
waiting_queue:List<Rc<RefCell<Thread>>>,
|
||||
thread_manager: Rc<RefCell<ThreadManager<'t>>> // On s'assure que le tm vit plus longtemps que les semaphore avec le lifetime
|
||||
|
||||
}
|
||||
|
||||
impl Semaphore{
|
||||
impl<'t> Semaphore<'_> {
|
||||
|
||||
pub fn p(&mut self, current_thread: Rc<Thread>, machine: &mut Machine){
|
||||
pub fn p(&mut self, current_thread: Rc<RefCell<Thread>>, machine: &mut Machine){
|
||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||
self.counter -= 1;
|
||||
if self.counter < 0 {
|
||||
self.waiting_queue.push(Rc::clone(¤t_thread));
|
||||
current_thread.sleep();
|
||||
self.thread_manager.borrow_mut().thread_sleep(current_thread);
|
||||
}
|
||||
machine.interrupt.set_status(old_status);
|
||||
}
|
||||
@ -36,16 +39,17 @@ impl Semaphore{
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Lock{
|
||||
pub struct Lock<'t>{
|
||||
|
||||
owner: Rc<Thread>,
|
||||
waiting_queue:List<Rc<Thread>>,
|
||||
owner: Rc<RefCell<Thread>>,
|
||||
waiting_queue:List<Rc<RefCell<Thread>>>,
|
||||
thread_manager: Rc<RefCell<ThreadManager<'t>>>,
|
||||
free: bool
|
||||
|
||||
}
|
||||
|
||||
impl Lock {
|
||||
pub fn acquire(&mut self, machine: &mut Machine, current_thread: Rc<Thread>) {
|
||||
impl<'t> Lock<'_> {
|
||||
pub fn acquire(&mut self, machine: &mut Machine, current_thread: Rc<RefCell<Thread>>) {
|
||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||
|
||||
if self.free {
|
||||
@ -53,13 +57,13 @@ impl Lock {
|
||||
self.owner = current_thread;
|
||||
} else {
|
||||
self.waiting_queue.push(Rc::clone(¤t_thread));
|
||||
current_thread.sleep();
|
||||
self.thread_manager.borrow_mut().thread_sleep(current_thread);
|
||||
}
|
||||
|
||||
machine.interrupt.set_status(old_status);
|
||||
}
|
||||
|
||||
pub fn release(&mut self, machine: &mut Machine, scheduler: &mut Scheduler, current_thread: Rc<Thread>) {
|
||||
pub fn release(&mut self, machine: &mut Machine, scheduler: &mut Scheduler, current_thread: Rc<RefCell<Thread>>) {
|
||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||
|
||||
if self.is_held_by_current_thread(current_thread) {
|
||||
@ -74,24 +78,25 @@ impl Lock {
|
||||
machine.interrupt.set_status(old_status);
|
||||
}
|
||||
|
||||
pub fn is_held_by_current_thread(&mut self, current_thread: Rc<Thread>) -> bool {
|
||||
pub fn is_held_by_current_thread(&mut self, current_thread: Rc<RefCell<Thread>>) -> bool {
|
||||
Rc::ptr_eq(&self.owner, ¤t_thread)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Condition{
|
||||
pub struct Condition<'t>{
|
||||
|
||||
waiting_queue:List<Rc<Thread>>
|
||||
waiting_queue:List<Rc<RefCell<Thread>>>,
|
||||
thread_manager: Rc<RefCell<ThreadManager<'t>>>,
|
||||
|
||||
}
|
||||
|
||||
impl Condition {
|
||||
impl<'t> Condition<'_> {
|
||||
|
||||
pub fn wait(&mut self, machine: &mut Machine, current_thread: Rc<Thread>) {
|
||||
pub fn wait(&mut self, machine: &mut Machine, current_thread: Rc<RefCell<Thread>>) {
|
||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||
|
||||
self.waiting_queue.push(Rc::clone(¤t_thread));
|
||||
current_thread.sleep();
|
||||
self.thread_manager.borrow_mut().thread_sleep(current_thread);
|
||||
|
||||
machine.interrupt.set_status(old_status);
|
||||
}
|
||||
|
@ -69,10 +69,6 @@ impl Thread {
|
||||
// }
|
||||
}
|
||||
|
||||
pub fn sleep(&self) {
|
||||
unreachable!("Has been moved to thread manager");
|
||||
}
|
||||
|
||||
pub fn save_simulator_state(&self) {
|
||||
todo!();
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ pub const SIMULATORSTACKSIZE: usize = 32 * 1024;
|
||||
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_alive: List<Rc<RefCell<Thread>>>,
|
||||
pub g_scheduler: Scheduler,
|
||||
pub system: Cell<Option<&'a System<'a>>>
|
||||
}
|
||||
@ -28,21 +28,21 @@ impl<'a> ThreadManager<'a> {
|
||||
}
|
||||
|
||||
/// Start a thread, attaching it to a process
|
||||
pub fn start_thread(&mut self, mut thread: Thread, owner: Process, func_pc: i64, argument: i64) -> Result<(), ErrorCode> {
|
||||
thread.process = Option::Some(owner);
|
||||
pub fn start_thread(&mut self, thread: Rc<RefCell<Thread>>, owner: Process, func_pc: i64, argument: i64) -> Result<(), ErrorCode> {
|
||||
let mut thread_m = thread.borrow_mut();
|
||||
thread_m.process = Option::Some(owner);
|
||||
let ptr = 0; // todo addrspace
|
||||
thread.init_thread_context(func_pc, ptr, argument);
|
||||
thread_m.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_m));
|
||||
self.g_scheduler().ready_to_run(Rc::clone(&thread_m));
|
||||
thread_m.init_simulator_context(base_stack_addr);
|
||||
thread_m.process.as_mut().unwrap().num_thread += 1;
|
||||
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>) {
|
||||
pub fn thread_join(&mut self, id_thread: Rc<RefCell<Thread>>) {
|
||||
while self.get_g_alive().contains(&Rc::clone(&id_thread)) {
|
||||
self.thread_yield(Rc::clone(&id_thread));
|
||||
}
|
||||
@ -51,17 +51,17 @@ impl<'a> ThreadManager<'a> {
|
||||
/// 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, thread: Rc<Thread>) {
|
||||
pub fn thread_yield(&mut self, thread: Rc<RefCell<Thread>>) {
|
||||
todo!();
|
||||
}
|
||||
|
||||
/// Put the thread to sleep and relinquish the processor
|
||||
pub fn thread_sleep(&mut self, thread: Rc<Thread>) {
|
||||
pub fn thread_sleep(&mut self, thread: Rc<RefCell<Thread>>) {
|
||||
todo!();
|
||||
}
|
||||
|
||||
/// Finish the execution of the thread and prepare its deallocation
|
||||
pub fn thread_finish(&self, thread: Rc<Thread>) {
|
||||
pub fn thread_finish(&self, thread: Rc<RefCell<Thread>>) {
|
||||
todo!();
|
||||
}
|
||||
|
||||
@ -105,7 +105,7 @@ impl<'a> ThreadManager<'a> {
|
||||
}
|
||||
|
||||
/// List of alive threads
|
||||
pub fn get_g_alive(&mut self) -> &mut List<Rc<Thread>> {
|
||||
pub fn get_g_alive(&mut self) -> &mut List<Rc<RefCell<Thread>>> {
|
||||
&mut self.g_alive
|
||||
}
|
||||
|
||||
|
@ -59,7 +59,7 @@ impl UContextT {
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
stackBottom: Vec::default()
|
||||
stack_bottom: Vec::default()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user