Implement Thread::start and join

This commit is contained in:
Quentin Legot 2023-03-01 16:55:17 +01:00
parent 77e6d74b3b
commit 68ee179e12
6 changed files with 264 additions and 163 deletions

31
src/kernel/mgerror.rs Normal file
View File

@ -0,0 +1,31 @@
/// Error enum, use it with Result<YourSucessStruct, **ErrorCode**>
pub enum ErrorCode {
INC_ERROR,
OPENFILE_ERROR,
EXEC_FILE_FORMAT_ERROR,
OUT_OF_MEMORY,
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,
/* Invalid typeId fields: */
INVALID_SEMAPHORE_ID,
INVALID_LOCK_ID,
INVALID_CONDITION_ID,
INVALID_FILE_ID,
INVALID_THREAD_ID,
/* Other messages */
WRONG_FILE_ENDIANESS,
NO_ACIA,
NUMMSGERROR /* Must always be last */
}

View File

@ -1,3 +1,4 @@
mod process; mod process;
pub mod thread; pub mod thread;
mod scheduler; pub mod scheduler;
pub mod mgerror;

View File

@ -1,10 +1,12 @@
use std::sync::Arc;
use crate::utility::list::List; use crate::utility::list::List;
use crate::kernel::thread::Thread; use crate::kernel::thread::Thread;
use crate::utility::system::{G_CURRENT_THREAD, G_THREAD_TO_BE_DESTROYED}; use crate::utility::system::{G_CURRENT_THREAD, G_THREAD_TO_BE_DESTROYED};
struct Scheduler { pub struct Scheduler {
ready_list: List<Thread> ready_list: List<Arc<Thread>>
} }
impl Scheduler { impl Scheduler {
@ -25,8 +27,8 @@ impl Scheduler {
/// ## Pamameter /// ## Pamameter
/// ///
/// **thread** is the thread to be put on the read list /// **thread** is the thread to be put on the read list
pub fn ready_to_run(&mut self, thread: Thread) { pub fn ready_to_run(&mut self, thread: Arc<Thread>) {
self.ready_list.push_back(thread); self.ready_list.push(thread);
} }
/// Return the next thread to be scheduled onto the CPU. /// Return the next thread to be scheduled onto the CPU.
@ -35,8 +37,8 @@ impl Scheduler {
/// Thread is removed from the ready list. /// Thread is removed from the ready list.
/// ///
/// **return** Thread thread to be scheduled /// **return** Thread thread to be scheduled
pub fn find_next_to_run(&mut self) -> Option<Thread> { pub fn find_next_to_run(&mut self) -> Option<Arc<Thread>> {
self.ready_list.pop_back() self.ready_list.pop()
} }
/// Dispatch the CPU to next_thread. Save the state of the old thread /// Dispatch the CPU to next_thread. Save the state of the old thread

View File

@ -1,5 +1,7 @@
use super::process::Process; use std::sync::Arc;
use crate::{simulator::machine::{NUM_INT_REGS, NUM_FP_REGS, STACK_REG}, utility::system::ObjectType};
use super::{process::Process, mgerror::ErrorCode};
use crate::{simulator::machine::{NUM_INT_REGS, NUM_FP_REGS, STACK_REG}, utility::system::{ObjectType, G_ALIVE, G_SCHEDULER}, kernel::scheduler};
const SIMULATORSTACKSIZE: usize = 32 * 1024; const SIMULATORSTACKSIZE: usize = 32 * 1024;
@ -43,15 +45,31 @@ impl Thread {
} }
/// Start a thread, attaching it to a process /// Start a thread, attaching it to a process
pub fn start(&mut self, owner: Process, func: i64, arg: i64) -> i32 { pub fn start(mut self, owner: Process, func: i64, arg: i64) -> Result<(), ErrorCode> {
self.process = Option::Some(owner); self.process = Option::Some(owner);
let ptr = 0; // todo addrspace let ptr = 0; // todo addrspace
self.init_thread_context(func, ptr, arg); self.init_thread_context(func, ptr, arg);
let base_stack_addr: [i8; SIMULATORSTACKSIZE] = [0; SIMULATORSTACKSIZE]; // todo AllocBoundedArray let base_stack_addr: [i8; SIMULATORSTACKSIZE] = [0; SIMULATORSTACKSIZE]; // todo AllocBoundedArray
self.init_simulator_context(base_stack_addr); self.init_simulator_context(base_stack_addr);
self.process.as_mut().unwrap().num_thread += 1; self.process.as_mut().unwrap().num_thread += 1;
match G_ALIVE.write() {
todo!(); Ok(mut alive) => {
let this = Arc::new(self);
alive.push(Arc::clone(&this));
match G_SCHEDULER.write() {
Ok(mut scheduler) => {
scheduler.ready_to_run(Arc::clone(&this));
},
Err(err) => {
panic!("RwLock poisonned, {}", err);
}
}
},
Err(err) => {
panic!("RwLock poisonned, {}", err);
}
}
Result::Ok(())
} }
fn init_thread_context(&mut self, initial_pc_reg: i64, initial_sp: i64, arg: i64) { fn init_thread_context(&mut self, initial_pc_reg: i64, initial_sp: i64, arg: i64) {
@ -60,11 +78,24 @@ impl Thread {
self.thread_context.int_registers[STACK_REG] = initial_sp; self.thread_context.int_registers[STACK_REG] = initial_sp;
} }
/// Wait for another thread to finish its execution fn init_simulator_context(&self, base_stack_addr: [i8; SIMULATORSTACKSIZE]) {
pub fn join(&self, id_thread: &Thread) {
todo!(); todo!();
} }
/// Wait for another thread to finish its execution
pub fn join(&self, id_thread: Arc<Thread>) {
match G_ALIVE.write() {
Ok(alive) => {
while alive.contains(&Arc::clone(&id_thread)) {
self.t_yield();
}
},
Err(err) => {
panic!("RwLock poisonned, {}", err)
}
}
}
/// Relinquish the CPU if any other thread is runnable. /// Relinquish the CPU if any other thread is runnable.
/// ///
/// Cannot use yield as a function name -> reserved name in rust /// Cannot use yield as a function name -> reserved name in rust
@ -87,10 +118,6 @@ impl Thread {
todo!(); todo!();
} }
pub fn init_simulator_context(&self, base_stack_addr: [i8; SIMULATORSTACKSIZE]) {
todo!();
}
pub fn save_processor_state(&self) { pub fn save_processor_state(&self) {
todo!(); todo!();
} }

View File

@ -1,178 +1,217 @@
use std::{cell::RefCell, rc::Rc};
/// Definition of an element of the list pub struct List<T: PartialEq> {
///
/// Contain one stored item and the previous/next element of the list
struct ListNode<T> {
item: T,
next: Link<T>,
prev: Link<T>,
}
impl<T> ListNode<T> {
fn new(item: T) -> Self {
Self {
item,
next: None,
prev: None,
}
}
}
type Link<T> = Option<Rc<RefCell<ListNode<T>>>>;
/// Defintion of the generic linked list
#[derive(Default)]
pub struct DoublyLinkedList<T> {
head: Link<T>, head: Link<T>,
tail: Link<T>,
size: usize,
} }
impl<T> DoublyLinkedList<T> { type Link<T> = Option<Box<Node<T>>>;
struct Node<T> {
elem: T,
next: Link<T>,
}
impl<T: PartialEq> List<T> {
pub fn new() -> Self { pub fn new() -> Self {
Self { List { head: None }
head: None,
tail: None,
size: 0,
}
} }
pub fn is_empty(&self) -> bool { /// Push an item at the end of the list
self.len() == 0 pub fn push(&mut self, elem: T) {
let new_node = Box::new(Node {
elem: elem,
next: self.head.take(),
});
self.head = Some(new_node);
} }
pub fn len(&self) -> usize { /// Retrieve and remove the item at the end of the list.
self.size ///
} /// Return None if list is empty
pub fn pop(&mut self) -> Option<T> {
/// Add the item at the end of the list self.head.take().map(|node| {
pub fn push_back(&mut self, item: T) { self.head = node.next;
let node = Rc::new(RefCell::new(ListNode::new(item))); node.elem
if let Some(prev_tail) = self.tail.take() {
prev_tail.borrow_mut().next = Some(Rc::clone(&node));
node.borrow_mut().prev = Some(prev_tail);
self.tail = Some(node);
self.size += 1;
} else {
self.head = Some(Rc::clone(&node));
self.tail = Some(node);
self.size = 1;
}
}
/// Add the item at the start of the list
pub fn push_front(&mut self, item: T) {
let node = Rc::new(RefCell::new(ListNode::new(item)));
if let Some(prev_head) = self.head.take() {
prev_head.borrow_mut().prev = Some(Rc::clone(&node));
node.borrow_mut().next = Some(prev_head);
self.head = Some(node);
self.size += 1;
} else {
self.head = Some(Rc::clone(&node));
self.tail = Some(node);
self.size = 1;
}
}
/// Retrieve and remove the item at the end of the list
pub fn pop_back(&mut self) -> Option<T> {
self.tail.take().map(|prev_tail| {
self.size -= 1;
match prev_tail.borrow_mut().prev.take() {
Some(node) => {
node.borrow_mut().next = None;
self.tail = Some(node);
}
None => {
self.head.take();
}
}
Rc::try_unwrap(prev_tail).ok().unwrap().into_inner().item
}) })
} }
/// Retrieve and remove the item at the start of the list /// Retrieve without removing the item at the end of the list
pub fn pop_front(&mut self) -> Option<T> { ///
self.head.take().map(|prev_head| { /// Return None if list is empty
self.size -= 1; pub fn peek(&self) -> Option<&T> {
match prev_head.borrow_mut().next.take() { self.head.as_ref().map(|node| {
Some(node) => { &node.elem
node.borrow_mut().prev = None;
self.head = Some(node);
}
None => {
self.tail.take();
}
}
Rc::try_unwrap(prev_head).ok().unwrap().into_inner().item
}) })
} }
/// Retrieve without removing the item at the end of the list as mutable
///
/// Return None if lsit is empty
pub fn peek_mut(&mut self) -> Option<&mut T> {
self.head.as_mut().map(|node| {
&mut node.elem
})
}
/// Search for an element in the list
///
/// Return **bool** true if the list contains the element, false otherwise
///
/// Worst case complexity of this function is O(n)
pub fn contains(&self, elem: &T) -> bool {
let mut iter = self.iter();
let element = iter.next();
while element.is_some() {
if element.unwrap() == elem {
return true;
}
}
false
}
pub fn into_iter(self) -> IntoIter<T> {
IntoIter(self)
}
pub fn iter(&self) -> Iter<'_, T> {
Iter { next: self.head.as_deref() }
}
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
IterMut { next: self.head.as_deref_mut() }
}
} }
impl<T> Drop for DoublyLinkedList<T> { impl<T: PartialEq> Drop for List<T> {
/// list destructor, safely desallocate smart pointer Rc
fn drop(&mut self) { fn drop(&mut self) {
while let Some(node) = self.head.take() { let mut cur_link = self.head.take();
let _ = node.borrow_mut().prev.take(); while let Some(mut boxed_node) = cur_link {
self.head = node.borrow_mut().next.take(); cur_link = boxed_node.next.take();
} }
self.tail.take();
} }
} }
impl<T> IntoIterator for DoublyLinkedList<T> { pub struct IntoIter<T: PartialEq>(List<T>);
type Item = <ListIterator<T> as Iterator>::Item;
type IntoIter = ListIterator<T>; impl<T: PartialEq> Iterator for IntoIter<T> {
fn into_iter(self) -> Self::IntoIter {
Self::IntoIter::new(self)
}
}
pub struct ListIterator<T> {
list: DoublyLinkedList<T>,
}
impl<T> ListIterator<T> {
fn new(list: DoublyLinkedList<T>) -> Self {
Self { list }
}
}
impl<T> Iterator for ListIterator<T> {
type Item = T; type Item = T;
fn next(&mut self) -> Option<Self::Item> {
// access fields of a tuple struct numerically
self.0.pop()
}
}
pub struct Iter<'a, T> {
next: Option<&'a Node<T>>,
}
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
self.next.map(|node| {
self.next = node.next.as_deref();
&node.elem
})
}
}
pub struct IterMut<'a, T> {
next: Option<&'a mut Node<T>>,
}
impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.list.pop_front() self.next.take().map(|node| {
self.next = node.next.as_deref_mut();
&mut node.elem
})
} }
} }
impl<T> DoubleEndedIterator for ListIterator<T> {
fn next_back(&mut self) -> Option<Self::Item> {
self.list.pop_back()
}
}
pub type List<T> = DoublyLinkedList<T>;
pub type ListInt = List<i32>;
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::List;
use super::DoublyLinkedList;
#[test] #[test]
fn test_list_push() { fn basics() {
let mut list = DoublyLinkedList::new(); let mut list = List::new();
list.push_back(5);
list.push_front(45); // Check empty list behaves right
assert_eq!(list.pop_front().unwrap(), 45); assert_eq!(list.pop(), None);
assert_eq!(list.pop_front().unwrap(), 5);
// Populate list
list.push(1);
list.push(2);
list.push(3);
// Check normal removal
assert_eq!(list.pop(), Some(3));
assert_eq!(list.pop(), Some(2));
// Push some more just to make sure nothing's corrupted
list.push(4);
list.push(5);
// Check normal removal
assert_eq!(list.pop(), Some(5));
assert_eq!(list.pop(), Some(4));
// Check exhaustion
assert_eq!(list.pop(), Some(1));
assert_eq!(list.pop(), None);
} }
#[test]
fn peek() {
let mut list = List::new();
assert_eq!(list.peek(), None);
assert_eq!(list.peek_mut(), None);
list.push(1); list.push(2); list.push(3);
assert_eq!(list.peek(), Some(&3));
assert_eq!(list.peek_mut(), Some(&mut 3));
list.peek_mut().map(|value| {
*value = 42
});
assert_eq!(list.peek(), Some(&42));
assert_eq!(list.pop(), Some(42));
}
#[test]
fn into_iter() {
let mut list = List::new();
list.push(1); list.push(2); list.push(3);
let mut iter = list.into_iter();
assert_eq!(iter.next(), Some(3));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), None);
}
#[test]
fn iter() {
let mut list = List::new();
list.push(1); list.push(2); list.push(3);
let mut iter = list.iter();
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&1));
}
#[test]
fn iter_mut() {
let mut list = List::new();
list.push(1); list.push(2); list.push(3);
let mut iter = list.iter_mut();
assert_eq!(iter.next(), Some(&mut 3));
assert_eq!(iter.next(), Some(&mut 2));
assert_eq!(iter.next(), Some(&mut 1));
}
} }

View File

@ -1,8 +1,8 @@
use std::sync::{RwLock, Arc}; use std::{sync::{RwLock, Arc}};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use crate::kernel::thread::Thread; use crate::kernel::{thread::Thread, scheduler::Scheduler};
use super::list::List; use super::list::List;
extern crate lazy_static; extern crate lazy_static;
@ -10,7 +10,8 @@ extern crate lazy_static;
lazy_static! { lazy_static! {
pub static ref G_CURRENT_THREAD: RwLock<Option<Thread>> = RwLock::new(Option::None); pub static ref G_CURRENT_THREAD: RwLock<Option<Thread>> = RwLock::new(Option::None);
pub static ref G_THREAD_TO_BE_DESTROYED: RwLock<Option<Thread>> = RwLock::new(Option::None); pub static ref G_THREAD_TO_BE_DESTROYED: RwLock<Option<Thread>> = RwLock::new(Option::None);
// pub static ref G_ALIVE: Arc<RwLock<List<Thread>>> = Arc::new(RwLock::new(List::new())); pub static ref G_ALIVE: RwLock<List<Arc<Thread>>> = RwLock::new(List::new());
pub static ref G_SCHEDULER: RwLock<Scheduler> = RwLock::new(Scheduler::new());
} }