Implement Thread::start and join
This commit is contained in:
31
src/kernel/mgerror.rs
Normal file
31
src/kernel/mgerror.rs
Normal 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 */
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
mod process;
|
||||
pub mod thread;
|
||||
mod scheduler;
|
||||
pub mod scheduler;
|
||||
pub mod mgerror;
|
@ -1,10 +1,12 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::utility::list::List;
|
||||
use crate::kernel::thread::Thread;
|
||||
use crate::utility::system::{G_CURRENT_THREAD, G_THREAD_TO_BE_DESTROYED};
|
||||
|
||||
|
||||
struct Scheduler {
|
||||
ready_list: List<Thread>
|
||||
pub struct Scheduler {
|
||||
ready_list: List<Arc<Thread>>
|
||||
}
|
||||
|
||||
impl Scheduler {
|
||||
@ -25,8 +27,8 @@ impl Scheduler {
|
||||
/// ## Pamameter
|
||||
///
|
||||
/// **thread** is the thread to be put on the read list
|
||||
pub fn ready_to_run(&mut self, thread: Thread) {
|
||||
self.ready_list.push_back(thread);
|
||||
pub fn ready_to_run(&mut self, thread: Arc<Thread>) {
|
||||
self.ready_list.push(thread);
|
||||
}
|
||||
|
||||
/// Return the next thread to be scheduled onto the CPU.
|
||||
@ -35,8 +37,8 @@ impl Scheduler {
|
||||
/// Thread is removed from the ready list.
|
||||
///
|
||||
/// **return** Thread thread to be scheduled
|
||||
pub fn find_next_to_run(&mut self) -> Option<Thread> {
|
||||
self.ready_list.pop_back()
|
||||
pub fn find_next_to_run(&mut self) -> Option<Arc<Thread>> {
|
||||
self.ready_list.pop()
|
||||
}
|
||||
|
||||
/// Dispatch the CPU to next_thread. Save the state of the old thread
|
||||
|
@ -1,5 +1,7 @@
|
||||
use super::process::Process;
|
||||
use crate::{simulator::machine::{NUM_INT_REGS, NUM_FP_REGS, STACK_REG}, utility::system::ObjectType};
|
||||
use std::sync::Arc;
|
||||
|
||||
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;
|
||||
|
||||
@ -43,15 +45,31 @@ impl Thread {
|
||||
}
|
||||
|
||||
/// 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);
|
||||
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;
|
||||
|
||||
todo!();
|
||||
match G_ALIVE.write() {
|
||||
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) {
|
||||
@ -60,11 +78,24 @@ impl Thread {
|
||||
self.thread_context.int_registers[STACK_REG] = initial_sp;
|
||||
}
|
||||
|
||||
/// Wait for another thread to finish its execution
|
||||
pub fn join(&self, id_thread: &Thread) {
|
||||
fn init_simulator_context(&self, base_stack_addr: [i8; SIMULATORSTACKSIZE]) {
|
||||
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.
|
||||
///
|
||||
/// Cannot use yield as a function name -> reserved name in rust
|
||||
@ -87,10 +118,6 @@ impl Thread {
|
||||
todo!();
|
||||
}
|
||||
|
||||
pub fn init_simulator_context(&self, base_stack_addr: [i8; SIMULATORSTACKSIZE]) {
|
||||
todo!();
|
||||
}
|
||||
|
||||
pub fn save_processor_state(&self) {
|
||||
todo!();
|
||||
}
|
||||
|
Reference in New Issue
Block a user