forked from Rativel/BurritOS
Added thread manager
This commit is contained in:
committed by
François Autin
parent
1ac2e322cf
commit
6820086579
@@ -1,6 +1,4 @@
|
||||
use std::{sync::Arc, rc::Rc};
|
||||
|
||||
use super::{process::Process, mgerror::ErrorCode, system::{ObjectType, System}, ucontext::UContextT};
|
||||
use super::{process::Process, system::ObjectType};
|
||||
use crate::{simulator::machine::{NUM_INT_REGS, NUM_FP_REGS, STACK_REG}};
|
||||
|
||||
const SIMULATORSTACKSIZE: usize = 32 * 1024;
|
||||
@@ -16,18 +14,17 @@ struct ThreadContext {
|
||||
#[derive(PartialEq)]
|
||||
pub struct Thread {
|
||||
name: String,
|
||||
process: Option<Process>,
|
||||
pub process: Option<Process>,
|
||||
// simulation_context: UContextT,
|
||||
thread_context: ThreadContext,
|
||||
stack_pointer: i32,
|
||||
object_type: ObjectType,
|
||||
system: Rc<System>
|
||||
object_type: ObjectType
|
||||
}
|
||||
|
||||
impl Thread {
|
||||
|
||||
/// Thread constructor
|
||||
pub fn new(name: String, system: Rc<System>) -> Self {
|
||||
pub fn new(name: String) -> Self {
|
||||
Self {
|
||||
name,
|
||||
process: None,
|
||||
@@ -39,31 +36,16 @@ impl Thread {
|
||||
},
|
||||
stack_pointer: 0,
|
||||
object_type: ObjectType::ThreadType,
|
||||
system
|
||||
}
|
||||
}
|
||||
|
||||
/// Start a thread, attaching it to a process
|
||||
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;
|
||||
let this = Rc::new(self);
|
||||
self.system.get_g_alive().push(Rc::clone(&this));
|
||||
self.system.g_scheduler().ready_to_run(Rc::clone(&this));
|
||||
Result::Ok(())
|
||||
}
|
||||
|
||||
fn init_thread_context(&mut self, initial_pc_reg: i64, initial_sp: i64, arg: i64) {
|
||||
pub 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;
|
||||
}
|
||||
|
||||
fn init_simulator_context(&self, base_stack_addr: [i8; SIMULATORSTACKSIZE]) {
|
||||
pub fn init_simulator_context(&self, base_stack_addr: [i8; SIMULATORSTACKSIZE]) {
|
||||
// let res = self.simulation_context.get_context();
|
||||
// if res != 0 {
|
||||
// panic!("getcontext returns non-zero value {}", res);
|
||||
@@ -78,20 +60,6 @@ impl Thread {
|
||||
// self.simulation_context.stackBottom[0] = STACK_FENCEPOST;
|
||||
}
|
||||
|
||||
/// Wait for another thread to finish its execution
|
||||
pub fn join(&self, id_thread: Rc<Thread>) {
|
||||
while self.system.get_g_alive().contains(&Rc::clone(&id_thread)) {
|
||||
self.t_yield();
|
||||
}
|
||||
}
|
||||
|
||||
/// Relinquish the CPU if any other thread is runnable.
|
||||
///
|
||||
/// Cannot use yield as a function name -> reserved name in rust
|
||||
pub fn t_yield(&self) {
|
||||
todo!();
|
||||
}
|
||||
|
||||
/// Put the thread to sleep and relinquish the processor
|
||||
pub fn sleep(&self) {
|
||||
todo!();
|
||||
|
||||
Reference in New Issue
Block a user