From e422e657672362f1f7eaf8274e025f8c592d4a9a Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 28 Feb 2023 14:43:40 +0100 Subject: [PATCH] Add thread structure --- src/kernel/mod.rs | 1 + src/kernel/process.rs | 4 ++ src/kernel/thread.rs | 97 ++++++++++++++++++++++++++++++++++++++++ src/simulator/machine.rs | 3 ++ src/utility/mod.rs | 3 +- src/utility/system.rs | 9 ++++ 6 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 src/kernel/process.rs create mode 100644 src/utility/system.rs diff --git a/src/kernel/mod.rs b/src/kernel/mod.rs index 447d11e..bb7bfe3 100644 --- a/src/kernel/mod.rs +++ b/src/kernel/mod.rs @@ -1,2 +1,3 @@ +mod process; mod thread; mod scheduler; \ No newline at end of file diff --git a/src/kernel/process.rs b/src/kernel/process.rs new file mode 100644 index 0000000..fe973f4 --- /dev/null +++ b/src/kernel/process.rs @@ -0,0 +1,4 @@ + +pub struct Process { + +} \ No newline at end of file diff --git a/src/kernel/thread.rs b/src/kernel/thread.rs index e69de29..3b6cd83 100644 --- a/src/kernel/thread.rs +++ b/src/kernel/thread.rs @@ -0,0 +1,97 @@ +use super::process::Process; +use crate::{simulator::machine::{NUM_INT_REGS, NUM_FP_REGS}, utility::system::ObjectType}; + + +struct SimulatorContext { + // todo +} + +struct ThreadContext { + pub int_registers: [i64; NUM_INT_REGS], + pub float_registers: [i64; NUM_FP_REGS], + pc: i64, +} + +pub struct Thread { + name: String, + process: Option, + simulation_context: SimulatorContext, + thread_context: ThreadContext, + stack_pointer: i32, + object_type: ObjectType +} + +impl Thread { + + pub fn new(name: String) -> Self { + Self { + name, + process: None, + simulation_context: SimulatorContext { }, + thread_context: ThreadContext { + int_registers: [0; NUM_INT_REGS], + float_registers: [0; NUM_FP_REGS], + pc: 0 + }, + stack_pointer: 0, + object_type: ObjectType::THREAD_TYPE + } + } + + /// Start a thread, attaching it to a process + pub fn start(&self, owner: &Process, func: i64, arg: i64) -> i32 { + todo!(); + } + + /// Wait for another thread to finish its execution + pub fn join(&self, id_thread: &Thread) { + todo!(); + } + + /// 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!(); + } + + /// Finish the execution of the thread and prepare its deallocation + pub fn finish(&self) { + todo!(); + } + + /// Check if a thread has overflowed its stack + pub fn check_overflow(&self) { + todo!(); + } + + pub fn init_simulator_context(&self, initial_pc_reg: i64, initial_sp: i64, arg: i64) { + todo!(); + } + + pub fn save_processor_state(&self) { + todo!(); + } + + pub fn restore_processor_state(&self) { + todo!(); + } + + pub fn save_simulator_state(&self) { + todo!(); + } + + pub fn restore_simulator_state(&self) { + todo!(); + } + + pub fn get_name(&self) -> String { + self.name.clone() + } + +} \ No newline at end of file diff --git a/src/simulator/machine.rs b/src/simulator/machine.rs index f3f635b..a4f91af 100644 --- a/src/simulator/machine.rs +++ b/src/simulator/machine.rs @@ -4,6 +4,9 @@ use super::{decode::{Instruction, decode}}; use super::global::*; use std::fs::File; +pub const NUM_INT_REGS: usize = 32; +pub const NUM_FP_REGS: usize = 32; + /// doit disparaitre const MEM_SIZE : usize = 4096; diff --git a/src/utility/mod.rs b/src/utility/mod.rs index 651aed7..4b92101 100644 --- a/src/utility/mod.rs +++ b/src/utility/mod.rs @@ -1 +1,2 @@ -pub mod list; \ No newline at end of file +pub mod list; +pub mod system; \ No newline at end of file diff --git a/src/utility/system.rs b/src/utility/system.rs new file mode 100644 index 0000000..bbcb003 --- /dev/null +++ b/src/utility/system.rs @@ -0,0 +1,9 @@ + +pub enum ObjectType { + SEMAPHORE_TYPE, + LOCK_TYPE, + CONDITION_TYPE, + FILE_TYPE, + THREAD_TYPE, + INVALID_TYPE +} \ No newline at end of file