Merge branch 'thread_scheduler' of gitlab.istic.univ-rennes1.fr:simpleos/burritos into thread_scheduler
This commit is contained in:
commit
99be85acf5
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -6,16 +6,9 @@ version = 3
|
|||||||
name = "burritos"
|
name = "burritos"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"lazy_static",
|
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "lazy_static"
|
|
||||||
version = "1.4.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.139"
|
version = "0.2.139"
|
||||||
|
@ -4,5 +4,4 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
lazy_static = "1.4.0"
|
|
||||||
libc = { version = "0.2.139", features = ["extra_traits"] }
|
libc = { version = "0.2.139", features = ["extra_traits"] }
|
||||||
|
@ -3,4 +3,5 @@ pub mod thread;
|
|||||||
pub mod scheduler;
|
pub mod scheduler;
|
||||||
pub mod mgerror;
|
pub mod mgerror;
|
||||||
pub mod system;
|
pub mod system;
|
||||||
mod ucontext;
|
mod ucontext;
|
||||||
|
mod synch;
|
43
src/kernel/synch.rs
Normal file
43
src/kernel/synch.rs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
use crate::utility::list::List;
|
||||||
|
use crate::kernel::thread::Thread;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use crate::simulator::interrupt::InterruptStatus::InterruptOff;
|
||||||
|
use crate::simulator::machine::Machine;
|
||||||
|
|
||||||
|
pub struct Semaphore{
|
||||||
|
|
||||||
|
counter:i32,
|
||||||
|
waiting_queue:List<Rc<Thread>>
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Semaphore{
|
||||||
|
|
||||||
|
pub fn p(&mut self, current_thread:Rc<Thread>, machine: &mut Machine){
|
||||||
|
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||||
|
self.counter-=1;
|
||||||
|
if self.counter < 0 {
|
||||||
|
self.waiting_queue.push(Rc::clone(¤t_thread));
|
||||||
|
current_thread.sleep();
|
||||||
|
}
|
||||||
|
machine.interrupt.set_status(old_status);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn v(&mut self, current_thread:Rc<Thread>, machine: &mut Machine){
|
||||||
|
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||||
|
self.counter-=1;
|
||||||
|
if self.waiting_queue.peek() == None {
|
||||||
|
self.waiting_queue.push(Rc::clone(¤t_thread));
|
||||||
|
current_thread.sleep();
|
||||||
|
}
|
||||||
|
machine.interrupt.set_status(old_status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Lock{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Condition{
|
||||||
|
|
||||||
|
}
|
@ -1,19 +1,98 @@
|
|||||||
use std::{sync::{RwLock, Arc}};
|
use std::rc::Rc;
|
||||||
|
use crate::{
|
||||||
|
kernel::{
|
||||||
|
thread::Thread,
|
||||||
|
scheduler::Scheduler
|
||||||
|
},
|
||||||
|
utility::list::List,
|
||||||
|
simulator::machine::Machine
|
||||||
|
};
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
/// # System
|
||||||
|
///
|
||||||
use crate::{kernel::{thread::Thread, scheduler::Scheduler}, utility::list::List, simulator::machine::Machine};
|
/// This structure represents the state of the threads running on the operating system.
|
||||||
|
/// It contains references to the following:
|
||||||
extern crate lazy_static;
|
///
|
||||||
|
/// - The simulated machine
|
||||||
lazy_static! {
|
/// - The current running thread
|
||||||
pub static ref G_MACHINE: RwLock<Machine> = RwLock::new(Machine::_init_machine());
|
/// - The list of active threads
|
||||||
pub static ref G_CURRENT_THREAD: RwLock<Option<Thread>> = RwLock::new(Option::None);
|
/// - The thread to be destroyed next
|
||||||
pub static ref G_THREAD_TO_BE_DESTROYED: RwLock<Option<Thread>> = RwLock::new(Option::None);
|
/// - The scheduler which acts upon these threads
|
||||||
pub static ref G_ALIVE: RwLock<List<Arc<Thread>>> = RwLock::new(List::new());
|
pub struct System {
|
||||||
pub static ref G_SCHEDULER: RwLock<Scheduler> = RwLock::new(Scheduler::new());
|
g_machine: Machine,
|
||||||
|
g_current_thread: Option<Thread>,
|
||||||
|
g_thread_to_be_destroyed: Option<Thread>,
|
||||||
|
g_alive: List<Rc<Thread>>,
|
||||||
|
g_scheduler: Scheduler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl System {
|
||||||
|
|
||||||
|
/// System constructor
|
||||||
|
pub fn new(machine: Machine, scheduler: Scheduler) -> Self {
|
||||||
|
Self {
|
||||||
|
g_machine: machine,
|
||||||
|
g_current_thread: None,
|
||||||
|
g_thread_to_be_destroyed: None,
|
||||||
|
g_alive: List::new(),
|
||||||
|
g_scheduler: scheduler
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GETTERS
|
||||||
|
|
||||||
|
/// Returns the Machine
|
||||||
|
///
|
||||||
|
/// Useful to access RAM, devices, ...
|
||||||
|
pub fn get_g_machine(&mut self) -> &mut Machine {
|
||||||
|
&mut self.g_machine
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Currently running thread
|
||||||
|
pub fn get_g_current_thread(&mut self) -> &mut Option<Thread> {
|
||||||
|
&mut self.g_current_thread
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Thread to be destroyed by [...]
|
||||||
|
///
|
||||||
|
/// TODO: Finish the comment with the relevant value
|
||||||
|
pub fn get_g_thread_to_be_destroyed(&mut self) -> &mut Option<Thread> {
|
||||||
|
&mut self.g_thread_to_be_destroyed
|
||||||
|
}
|
||||||
|
|
||||||
|
/// List of alive threads
|
||||||
|
pub fn get_g_alive(&mut self) -> &mut List<Rc<Thread>> {
|
||||||
|
&mut self.g_alive
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Current scheduler
|
||||||
|
pub fn g_scheduler(&mut self) -> &mut Scheduler {
|
||||||
|
&mut self.g_scheduler
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setters
|
||||||
|
|
||||||
|
/// Assign a machine to the system
|
||||||
|
pub fn set_g_machine(&mut self, machine: Machine) {
|
||||||
|
self.g_machine = machine
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set currently running thread
|
||||||
|
pub fn set_g_current_thread(&mut self, thread: Option<Thread>) {
|
||||||
|
self.g_current_thread = thread
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set thread to be destroyed next
|
||||||
|
pub fn set_g_thread_to_be_destroyed(&mut self, thread: Option<Thread>) {
|
||||||
|
self.g_thread_to_be_destroyed = thread
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set Scheduler which will manage the threads
|
||||||
|
pub fn set_g_scheduler(&mut self, scheduler: Scheduler) {
|
||||||
|
self.g_scheduler = scheduler
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
pub enum ObjectType {
|
pub enum ObjectType {
|
||||||
|
15
src/main.rs
15
src/main.rs
@ -2,15 +2,14 @@ mod simulator;
|
|||||||
mod kernel;
|
mod kernel;
|
||||||
pub mod utility;
|
pub mod utility;
|
||||||
|
|
||||||
|
use kernel::{
|
||||||
|
scheduler::Scheduler,
|
||||||
|
system::System
|
||||||
|
};
|
||||||
use simulator::machine::Machine;
|
use simulator::machine::Machine;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut m = Machine::_init_machine();
|
let machine = Machine::_init_machine();
|
||||||
m.main_memory[4] = 43;
|
let scheduler = Scheduler::new();
|
||||||
m.main_memory[5] = 150;
|
let system = System::new(machine, scheduler);
|
||||||
let a : u8 = 128;
|
|
||||||
let b : i8 = a as i8;
|
|
||||||
let c : u8 = b as u8;
|
|
||||||
println!("aaa {c}");
|
|
||||||
println!("read_memory : {}", Machine::read_memory(&mut m, 2, 4));
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user