diff --git a/src/kernel/process.rs b/src/kernel/process.rs index 5ae7efc..a56883e 100644 --- a/src/kernel/process.rs +++ b/src/kernel/process.rs @@ -1,5 +1,5 @@ #[derive(PartialEq)] pub struct Process { - + pub num_thread: usize, } \ No newline at end of file diff --git a/src/kernel/scheduler.rs b/src/kernel/scheduler.rs index 7cfe0ee..d2670b3 100644 --- a/src/kernel/scheduler.rs +++ b/src/kernel/scheduler.rs @@ -3,7 +3,7 @@ use crate::kernel::thread::Thread; use crate::utility::system::{G_CURRENT_THREAD, G_THREAD_TO_BE_DESTROYED}; -struct Scheduler<> { +struct Scheduler { ready_list: List } diff --git a/src/kernel/thread.rs b/src/kernel/thread.rs index 922df0e..a4301f8 100644 --- a/src/kernel/thread.rs +++ b/src/kernel/thread.rs @@ -1,6 +1,7 @@ use super::process::Process; -use crate::{simulator::machine::{NUM_INT_REGS, NUM_FP_REGS}, utility::system::ObjectType}; +use crate::{simulator::machine::{NUM_INT_REGS, NUM_FP_REGS, STACK_REG}, utility::system::ObjectType}; +const SIMULATORSTACKSIZE: usize = 32 * 1024; #[derive(PartialEq)] struct SimulatorContext { @@ -42,10 +43,23 @@ impl Thread { } /// Start a thread, attaching it to a process - pub fn start(&self, owner: &Process, func: i64, arg: i64) -> i32 { + pub fn start(&mut self, owner: Process, func: i64, arg: i64) -> i32 { + 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!(); } + 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.int_registers[10] = arg; + self.thread_context.int_registers[STACK_REG] = initial_sp; + } + /// Wait for another thread to finish its execution pub fn join(&self, id_thread: &Thread) { todo!(); @@ -73,7 +87,7 @@ impl Thread { todo!(); } - pub fn init_simulator_context(&self, initial_pc_reg: i64, initial_sp: i64, arg: i64) { + pub fn init_simulator_context(&self, base_stack_addr: [i8; SIMULATORSTACKSIZE]) { todo!(); } @@ -97,4 +111,13 @@ impl Thread { self.name.clone() } +} + +impl Drop for Thread { + + fn drop(&mut self) { + self.object_type = ObjectType::InvalidType; + todo!(); + } + } \ No newline at end of file diff --git a/src/simulator/machine.rs b/src/simulator/machine.rs index a4f91af..6523628 100644 --- a/src/simulator/machine.rs +++ b/src/simulator/machine.rs @@ -4,6 +4,8 @@ use super::{decode::{Instruction, decode}}; use super::global::*; use std::fs::File; +pub const STACK_REG: usize = 2; + pub const NUM_INT_REGS: usize = 32; pub const NUM_FP_REGS: usize = 32; diff --git a/src/utility/system.rs b/src/utility/system.rs index 4c274ec..701e0b7 100644 --- a/src/utility/system.rs +++ b/src/utility/system.rs @@ -1,13 +1,16 @@ -use std::sync::{Mutex, RwLock}; +use std::sync::{RwLock, Arc}; use lazy_static::lazy_static; use crate::kernel::thread::Thread; + +use super::list::List; extern crate lazy_static; lazy_static! { 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: Arc>> = Arc::new(RwLock::new(List::new())); }