2023-03-16 14:52:53 +01:00
|
|
|
use std::{rc::Rc, cell::{RefCell, Ref}};
|
2023-03-08 21:10:51 +01:00
|
|
|
|
2023-04-12 14:49:08 +02:00
|
|
|
use crate::{utility::{list::List, objaddr::ObjAddr}, simulator::{machine::{NUM_INT_REGS, NUM_FP_REGS, Machine}, interrupt::InterruptStatus, error::{MachineOk, MachineError}}};
|
2023-03-08 21:10:51 +01:00
|
|
|
|
2023-04-12 15:32:46 +02:00
|
|
|
use super::{thread::Thread, process::Process};
|
2023-03-08 21:10:51 +01:00
|
|
|
|
2023-03-09 12:08:33 +01:00
|
|
|
pub const SIMULATORSTACKSIZE: usize = 32 * 1024;
|
2023-03-08 21:10:51 +01:00
|
|
|
|
2023-03-14 15:16:40 +01:00
|
|
|
/// # Thread manager
|
|
|
|
///
|
|
|
|
/// An instance of this struct is responsible for managing threads on behalf of the system
|
2023-03-08 21:10:51 +01:00
|
|
|
#[derive(PartialEq)]
|
2023-03-13 21:47:06 +01:00
|
|
|
pub struct ThreadManager {
|
2023-03-14 15:16:40 +01:00
|
|
|
/// Current running thread
|
2023-03-13 20:55:46 +01:00
|
|
|
pub g_current_thread: Option<Rc<RefCell<Thread>>>,
|
2023-03-14 15:16:40 +01:00
|
|
|
/// The thread to be destroyed next
|
2023-03-13 20:55:46 +01:00
|
|
|
pub g_thread_to_be_destroyed: Option<Rc<RefCell<Thread>>>,
|
2023-03-14 15:16:40 +01:00
|
|
|
/// The list of alive threads
|
2023-03-09 14:00:42 +01:00
|
|
|
pub g_alive: List<Rc<RefCell<Thread>>>,
|
2023-03-16 14:52:53 +01:00
|
|
|
/// Thread in ready state waiting to become active
|
|
|
|
ready_list: List<Rc<RefCell<Thread>>>,
|
2023-04-12 14:49:08 +02:00
|
|
|
obj_addrs: ObjAddr
|
2023-03-08 21:10:51 +01:00
|
|
|
}
|
|
|
|
|
2023-03-13 21:47:06 +01:00
|
|
|
impl ThreadManager {
|
2023-03-08 21:10:51 +01:00
|
|
|
|
2023-03-14 15:16:40 +01:00
|
|
|
/// Thread manager constructor
|
2023-03-08 21:10:51 +01:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
g_current_thread: Option::None,
|
|
|
|
g_thread_to_be_destroyed: Option::None,
|
2023-03-21 22:40:49 +01:00
|
|
|
g_alive: List::default(),
|
|
|
|
ready_list: List::default(),
|
2023-04-12 14:49:08 +02:00
|
|
|
obj_addrs: ObjAddr::init(),
|
2023-03-16 14:52:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mark a thread as aready, but not necessarily running yet.
|
|
|
|
///
|
|
|
|
/// Put it in the ready list, for later scheduling onto the CPU.
|
|
|
|
///
|
|
|
|
/// ## Pamameter
|
|
|
|
///
|
|
|
|
/// **thread** is the thread to be put on the read list
|
|
|
|
pub fn ready_to_run(&mut self, thread: Rc<RefCell<Thread>>) {
|
|
|
|
self.ready_list.push(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the next thread to be scheduled onto the CPU.
|
|
|
|
/// If there are no ready threads, return Option::None
|
|
|
|
///
|
|
|
|
/// Thread is removed from the ready list.
|
|
|
|
///
|
|
|
|
/// **return** Thread thread to be scheduled
|
|
|
|
pub fn find_next_to_run(&mut self) -> Option<Rc<RefCell<Thread>>> {
|
|
|
|
self.ready_list.pop()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Dispatch the CPU to next_thread. Save the state of the old thread
|
|
|
|
/// and load the state of the new thread.
|
|
|
|
///
|
|
|
|
/// We assume the state of the previously running thread has already been changed from running to blocked or ready.
|
|
|
|
///
|
|
|
|
/// Global variable g_current_thread become next_thread
|
|
|
|
///
|
|
|
|
/// ## Parameter
|
|
|
|
///
|
|
|
|
/// **next_thread** thread to dispatch to the CPU
|
|
|
|
pub fn switch_to(&mut self, machine: &mut Machine, next_thread: Rc<RefCell<Thread>>) {
|
|
|
|
match self.get_g_current_thread() {
|
|
|
|
Some(old) => {
|
|
|
|
let old1 = Rc::clone(old);
|
|
|
|
let old2 = Rc::clone(old);
|
|
|
|
self.thread_save_processor_state(machine, old1);
|
|
|
|
// old_thread.save_simulator_state();
|
|
|
|
if old2 != next_thread {
|
|
|
|
self.thread_restore_processor_state(machine, Rc::clone(&next_thread));
|
|
|
|
// next_thread.restore_simulator_state();
|
|
|
|
self.set_g_current_thread(Some(next_thread));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
2023-03-27 22:20:29 +02:00
|
|
|
self.thread_restore_processor_state(machine, Rc::clone(&next_thread));
|
|
|
|
// next_thread.restore_simulator_state();
|
|
|
|
self.set_g_current_thread(Some(next_thread));
|
2023-03-16 14:52:53 +01:00
|
|
|
}
|
2023-03-08 21:10:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start a thread, attaching it to a process
|
2023-04-11 17:47:36 +02:00
|
|
|
pub fn start_thread(&mut self, thread: Rc<RefCell<Thread>>, owner: Rc<RefCell<Process>>, func_pc: u64, sp_loc: u64, argument: i64) {
|
2023-03-09 14:00:42 +01:00
|
|
|
let mut thread_m = thread.borrow_mut();
|
2023-03-13 20:55:46 +01:00
|
|
|
assert_eq!(thread_m.process, Option::None);
|
2023-03-09 14:00:42 +01:00
|
|
|
thread_m.process = Option::Some(owner);
|
2023-04-02 19:55:06 +02:00
|
|
|
let ptr = sp_loc; // todo addrspace
|
2023-03-09 14:00:42 +01:00
|
|
|
thread_m.init_thread_context(func_pc, ptr, argument);
|
2023-04-11 17:47:36 +02:00
|
|
|
if let Some(process) = &thread_m.process {
|
|
|
|
process.borrow_mut().num_thread += 1;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
}
|
2023-03-09 14:00:42 +01:00
|
|
|
self.get_g_alive().push(Rc::clone(&thread));
|
2023-03-16 14:52:53 +01:00
|
|
|
self.ready_to_run(Rc::clone(&thread));
|
2023-03-08 21:10:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait for another thread to finish its execution
|
2023-03-16 14:52:53 +01:00
|
|
|
pub fn thread_join(&mut self, machine: &mut Machine, id_thread: Rc<RefCell<Thread>>) {
|
2023-03-08 21:10:51 +01:00
|
|
|
while self.get_g_alive().contains(&Rc::clone(&id_thread)) {
|
2023-03-16 14:52:53 +01:00
|
|
|
self.thread_yield(machine, Rc::clone(&id_thread));
|
2023-03-08 21:10:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Relinquish the CPU if any other thread is runnable.
|
|
|
|
///
|
|
|
|
/// Cannot use yield as a function name -> reserved name in rust
|
2023-03-16 14:52:53 +01:00
|
|
|
pub fn thread_yield(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
|
|
|
let old_status = machine.interrupt.set_status(crate::simulator::interrupt::InterruptStatus::InterruptOff);
|
2023-03-11 14:48:56 +01:00
|
|
|
|
2023-03-16 14:52:53 +01:00
|
|
|
assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread);
|
|
|
|
let next_thread = self.find_next_to_run();
|
2023-03-15 15:20:20 +01:00
|
|
|
if let Some(next_thread) = next_thread {
|
2023-03-16 14:52:53 +01:00
|
|
|
self.ready_to_run(thread);
|
|
|
|
self.switch_to(machine, next_thread);
|
2023-03-11 14:48:56 +01:00
|
|
|
}
|
2023-03-16 14:52:53 +01:00
|
|
|
machine.interrupt.set_status(old_status);
|
2023-03-08 21:10:51 +01:00
|
|
|
}
|
|
|
|
|
2023-03-09 12:08:33 +01:00
|
|
|
/// Put the thread to sleep and relinquish the processor
|
2023-03-15 17:57:53 +01:00
|
|
|
pub fn thread_sleep(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
2023-04-05 12:01:31 +02:00
|
|
|
debug_assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread);
|
|
|
|
debug_assert_eq!(machine.interrupt.get_status(), InterruptStatus::InterruptOff);
|
2023-03-16 14:52:53 +01:00
|
|
|
|
|
|
|
let mut next_thread = self.find_next_to_run();
|
2023-03-15 15:20:20 +01:00
|
|
|
while next_thread.is_none() {
|
|
|
|
eprintln!("Nobody to run => idle");
|
|
|
|
machine.interrupt.idle();
|
2023-03-16 14:52:53 +01:00
|
|
|
next_thread = self.find_next_to_run();
|
2023-03-13 20:55:46 +01:00
|
|
|
}
|
2023-03-16 14:52:53 +01:00
|
|
|
self.switch_to(machine, Rc::clone(&next_thread.unwrap()));
|
|
|
|
|
2023-03-09 12:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Finish the execution of the thread and prepare its deallocation
|
2023-03-16 14:52:53 +01:00
|
|
|
pub fn thread_finish(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
2023-03-15 15:20:20 +01:00
|
|
|
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
|
2023-04-06 13:48:37 +02:00
|
|
|
self.set_g_thread_to_be_destroyed(Option::Some(Rc::clone(&thread)));
|
2023-03-22 14:30:21 +01:00
|
|
|
self.g_alive.remove(Rc::clone(&thread));
|
2023-04-05 13:07:10 +02:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
println!("Sleeping thread {}", thread.borrow().get_name());
|
2023-03-15 15:20:20 +01:00
|
|
|
// g_objets_addrs->removeObject(self.thread) // a ajouté plus tard
|
2023-03-15 17:57:53 +01:00
|
|
|
self.thread_sleep(machine, Rc::clone(&thread));
|
2023-03-15 15:20:20 +01:00
|
|
|
machine.interrupt.set_status(old_status);
|
2023-03-09 12:08:33 +01:00
|
|
|
}
|
|
|
|
|
2023-03-22 17:17:53 +01:00
|
|
|
/// Save the CPU state of a user program on a context switch.
|
2023-03-15 17:57:53 +01:00
|
|
|
pub fn thread_save_processor_state(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
2023-03-16 14:52:53 +01:00
|
|
|
let mut t = thread.borrow_mut();
|
2023-03-15 15:20:20 +01:00
|
|
|
for i in 0..NUM_INT_REGS {
|
2023-03-15 17:57:53 +01:00
|
|
|
t.thread_context.int_registers[i] = machine.read_int_register(i);
|
2023-03-15 15:20:20 +01:00
|
|
|
}
|
|
|
|
for i in 0..NUM_FP_REGS {
|
2023-03-15 17:57:53 +01:00
|
|
|
t.thread_context.float_registers[i] = machine.read_fp_register(i);
|
2023-03-09 12:08:33 +01:00
|
|
|
}
|
2023-03-27 22:20:29 +02:00
|
|
|
t.thread_context.pc = machine.pc;
|
2023-03-09 12:08:33 +01:00
|
|
|
}
|
|
|
|
|
2023-03-22 17:17:53 +01:00
|
|
|
/// Restore the CPU state of a user program on a context switch.
|
2023-03-15 17:57:53 +01:00
|
|
|
pub fn thread_restore_processor_state(&self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
2023-03-15 15:20:20 +01:00
|
|
|
let t: Ref<_> = thread.borrow();
|
|
|
|
for i in 0..NUM_INT_REGS {
|
|
|
|
machine.write_int_register(i, t.thread_context.int_registers[i]);
|
2023-03-09 12:08:33 +01:00
|
|
|
}
|
2023-03-27 22:20:29 +02:00
|
|
|
machine.pc = t.thread_context.pc;
|
2023-03-09 12:08:33 +01:00
|
|
|
}
|
|
|
|
|
2023-04-12 15:22:22 +02:00
|
|
|
/// Decrement the value, and wait if it becomes < 0. Checking the
|
|
|
|
/// value and decrementing must be done atomically, so we
|
|
|
|
/// need to disable interrupts before checking the value.
|
|
|
|
///
|
|
|
|
/// Note that thread_manager::thread_sleep assumes that interrupts are disabled
|
|
|
|
/// when it is called.
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
/// - *id_sema* id of the semaphore, stored in [`ObjAddr`], id given by user program thought exceptions
|
|
|
|
/// - *machine* Current state of the machine
|
2023-04-12 14:49:08 +02:00
|
|
|
pub fn sem_p(&mut self, id_sema: i32, machine: &mut Machine) -> Result<MachineOk, MachineError> {
|
|
|
|
match self.get_g_current_thread() {
|
|
|
|
Some(thread) => {
|
|
|
|
let rc1_thread = Rc::clone(thread);
|
|
|
|
let rc2_thread = Rc::clone(thread);
|
|
|
|
let sema = self.get_obj_addrs().search_semaphore(id_sema as i32);
|
|
|
|
if let Some(sema) = sema {
|
|
|
|
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
|
|
|
|
sema.counter -= 1;
|
|
|
|
if sema.counter < 0 {
|
|
|
|
sema.waiting_queue.push(rc1_thread);
|
|
|
|
self.thread_sleep(machine, rc2_thread);
|
|
|
|
}
|
|
|
|
machine.interrupt.set_status(old_status);
|
|
|
|
Ok(MachineOk::Ok)
|
|
|
|
} else {
|
|
|
|
Err("Cannot find semaphore")?
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => unreachable!("Current thread should not be None")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 15:22:22 +02:00
|
|
|
/// Increment semaphore value, waking up a waiting thread if any.
|
|
|
|
/// As with P(), this operation must be atomic, so we need to disable
|
|
|
|
/// interrupts.
|
|
|
|
///
|
|
|
|
/// scheduler::ready_to_run() assumes that interrupts
|
|
|
|
/// are disabled when it is called.
|
|
|
|
///
|
|
|
|
/// ### Parameters
|
|
|
|
/// - *id_sema* id of the semaphore, stored in [`ObjAddr`], id given by user program thought exceptions
|
|
|
|
/// - **machine** the machine where the threads are executed
|
2023-04-12 14:49:08 +02:00
|
|
|
pub fn sem_v(&mut self, id_sema: i32, machine: &mut Machine) -> Result<MachineOk, MachineError> {
|
|
|
|
let sema = self.get_obj_addrs().search_semaphore(id_sema as i32);
|
|
|
|
match sema {
|
|
|
|
Some(sema) => {
|
|
|
|
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
|
|
|
|
sema.counter += 1;
|
|
|
|
match sema.waiting_queue.pop() {
|
|
|
|
Some(thread) => self.ready_to_run(thread),
|
|
|
|
None => ()
|
|
|
|
}
|
|
|
|
machine.interrupt.set_status(old_status);
|
|
|
|
Ok(MachineOk::Ok)
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
Err("Cannot find semaphore")?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-08 21:10:51 +01:00
|
|
|
/// Currently running thread
|
2023-03-16 14:52:53 +01:00
|
|
|
pub fn get_g_current_thread(&mut self) -> &Option<Rc<RefCell<Thread>>> {
|
|
|
|
&self.g_current_thread
|
2023-03-08 21:10:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Thread to be destroyed by [...]
|
|
|
|
///
|
|
|
|
/// TODO: Finish the comment with the relevant value
|
2023-03-16 14:52:53 +01:00
|
|
|
pub fn get_g_thread_to_be_destroyed(&mut self) -> &Option<Rc<RefCell<Thread>>> {
|
|
|
|
&self.g_thread_to_be_destroyed
|
2023-03-08 21:10:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// List of alive threads
|
2023-03-09 14:00:42 +01:00
|
|
|
pub fn get_g_alive(&mut self) -> &mut List<Rc<RefCell<Thread>>> {
|
2023-03-08 21:10:51 +01:00
|
|
|
&mut self.g_alive
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set currently running thread
|
2023-03-13 20:55:46 +01:00
|
|
|
pub fn set_g_current_thread(&mut self, thread: Option<Rc<RefCell<Thread>>>) {
|
2023-03-08 21:10:51 +01:00
|
|
|
self.g_current_thread = thread
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set thread to be destroyed next
|
2023-03-13 20:55:46 +01:00
|
|
|
pub fn set_g_thread_to_be_destroyed(&mut self, thread: Option<Rc<RefCell<Thread>>>) {
|
2023-03-08 21:10:51 +01:00
|
|
|
self.g_thread_to_be_destroyed = thread
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:49:08 +02:00
|
|
|
pub fn get_obj_addrs(&mut self) -> &mut ObjAddr {
|
|
|
|
&mut self.obj_addrs
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:52:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2023-03-22 18:30:31 +01:00
|
|
|
use std::{rc::Rc, cell::RefCell};
|
|
|
|
|
2023-04-12 15:32:46 +02:00
|
|
|
use crate::{simulator::{machine::Machine, loader}, kernel::{system::System, thread::Thread, process::Process, thread_manager::ThreadManager, synch::Semaphore}};
|
2023-03-22 18:30:31 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_thread_context() {
|
2023-04-06 13:27:03 +02:00
|
|
|
let mut machine = Machine::new(true);
|
2023-04-05 16:47:43 +02:00
|
|
|
|
2023-04-05 17:07:58 +02:00
|
|
|
let (loader, ptr) = loader::Loader::new("./target/guac/halt.guac", &mut machine, 0).expect("IO Error");
|
2023-04-01 00:14:09 +02:00
|
|
|
let start_pc = loader.elf_header.entrypoint;
|
2023-03-27 22:20:29 +02:00
|
|
|
let system = &mut System::default();
|
2023-03-28 21:26:58 +02:00
|
|
|
|
2023-03-22 18:30:31 +01:00
|
|
|
let thread1 = Thread::new("th1");
|
|
|
|
let thread1 = Rc::new(RefCell::new(thread1));
|
|
|
|
system.get_thread_manager().get_g_alive().push(Rc::clone(&thread1));
|
2023-03-28 21:26:58 +02:00
|
|
|
|
2023-04-04 22:01:49 +02:00
|
|
|
let owner1 = Process { num_thread: 0 };
|
2023-04-11 17:47:36 +02:00
|
|
|
let owner1 = Rc::new(RefCell::new(owner1));
|
2023-04-05 16:47:43 +02:00
|
|
|
system.get_thread_manager().start_thread(Rc::clone(&thread1), owner1, loader.elf_header.entrypoint, ptr, -1);
|
2023-03-28 19:47:42 +02:00
|
|
|
debug_assert_eq!(thread1.borrow_mut().thread_context.pc, start_pc);
|
2023-04-04 22:01:49 +02:00
|
|
|
debug_assert!(system.get_thread_manager().get_g_alive().contains(&Rc::clone(&thread1)));
|
2023-03-28 21:26:58 +02:00
|
|
|
|
2023-03-22 18:30:31 +01:00
|
|
|
let to_run = system.get_thread_manager().find_next_to_run().unwrap();
|
2023-04-05 12:01:31 +02:00
|
|
|
debug_assert_eq!(to_run, Rc::clone(&thread1));
|
2023-03-28 21:26:58 +02:00
|
|
|
|
2023-03-22 18:30:31 +01:00
|
|
|
system.get_thread_manager().switch_to(&mut machine, Rc::clone(&to_run));
|
2023-04-05 12:01:31 +02:00
|
|
|
debug_assert_eq!(system.get_thread_manager().g_current_thread, Option::Some(Rc::clone(&thread1)));
|
|
|
|
debug_assert_eq!(machine.pc, loader.elf_header.entrypoint);
|
2023-03-28 21:26:58 +02:00
|
|
|
|
2023-04-05 12:01:31 +02:00
|
|
|
machine.run(system);
|
2023-03-22 18:30:31 +01:00
|
|
|
}
|
2023-03-22 15:52:48 +01:00
|
|
|
|
2023-04-12 15:22:22 +02:00
|
|
|
#[test]
|
|
|
|
fn test_semaphore_single() {
|
|
|
|
// Init
|
|
|
|
let mut machine = Machine::new(true);
|
|
|
|
let mut thread_manager = ThreadManager::new();
|
|
|
|
let semaphore = Semaphore::new(1);
|
|
|
|
let sema_id = thread_manager.get_obj_addrs().add_semaphore(semaphore);
|
|
|
|
let thread = Rc::new(RefCell::new(Thread::new("test_semaphore")));
|
|
|
|
thread_manager.ready_to_run(Rc::clone(&thread));
|
|
|
|
thread_manager.set_g_current_thread(Some(thread));
|
|
|
|
// P
|
|
|
|
thread_manager.sem_p(sema_id, &mut machine).expect("semaphore P return an error: ");
|
|
|
|
{
|
|
|
|
let semaphore = thread_manager.get_obj_addrs().search_semaphore(sema_id).unwrap();
|
|
|
|
assert_eq!(semaphore.counter, 0);
|
|
|
|
assert!(semaphore.waiting_queue.is_empty());
|
|
|
|
}
|
|
|
|
// V
|
|
|
|
thread_manager.sem_v(sema_id, &mut machine).expect("semaphore V return an error: ");
|
|
|
|
{
|
|
|
|
let semaphore = thread_manager.get_obj_addrs().search_semaphore(sema_id).unwrap();
|
|
|
|
assert_eq!(semaphore.counter, 1);
|
|
|
|
assert!(semaphore.waiting_queue.is_empty());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_semaphore_multiple() {
|
|
|
|
// Init
|
|
|
|
let mut tm = ThreadManager::new();
|
|
|
|
let mut machine = Machine::new(true);
|
2023-04-12 15:32:46 +02:00
|
|
|
let semaphore = Semaphore::new(2);
|
2023-04-12 15:22:22 +02:00
|
|
|
let sema_id = tm.get_obj_addrs().add_semaphore(semaphore);
|
|
|
|
let thread1 = Rc::new(RefCell::new(Thread::new("test_semaphore_1")));
|
|
|
|
let thread2 = Rc::new(RefCell::new(Thread::new("test_semaphore_2")));
|
|
|
|
let thread3 = Rc::new(RefCell::new(Thread::new("test_semaphore_3")));
|
|
|
|
|
|
|
|
// let mut borrow_tm = tm.borrow_mut();
|
|
|
|
// let scheduler = &mut tm.g_scheduler;
|
|
|
|
tm.ready_to_run(Rc::clone(&thread1));
|
|
|
|
tm.ready_to_run(Rc::clone(&thread2));
|
|
|
|
tm.ready_to_run(Rc::clone(&thread3));
|
|
|
|
// P
|
|
|
|
tm.set_g_current_thread(Some(Rc::clone(&thread1)));
|
|
|
|
tm.sem_p(sema_id, &mut machine).expect("semaphore P return an error: ");
|
|
|
|
{
|
|
|
|
let semaphore = tm.get_obj_addrs().search_semaphore(sema_id).unwrap();
|
|
|
|
assert_eq!(semaphore.counter, 1);
|
|
|
|
assert!(semaphore.waiting_queue.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
tm.set_g_current_thread(Some(Rc::clone(&thread2)));
|
|
|
|
tm.sem_p(sema_id, &mut machine).expect("semaphore P return an error: ");
|
|
|
|
{
|
|
|
|
let semaphore = tm.get_obj_addrs().search_semaphore(sema_id).unwrap();
|
|
|
|
assert_eq!(semaphore.counter, 0);
|
|
|
|
assert!(semaphore.waiting_queue.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
tm.set_g_current_thread(Some(Rc::clone(&thread3)));
|
|
|
|
tm.sem_p( sema_id, &mut machine).expect("semaphore P return an error: ");
|
|
|
|
{
|
|
|
|
let semaphore = tm.get_obj_addrs().search_semaphore(sema_id).unwrap();
|
|
|
|
assert_eq!(semaphore.counter, -1);
|
|
|
|
assert!(semaphore.waiting_queue.iter().count() == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// V
|
|
|
|
tm.sem_v(sema_id, &mut machine).expect("semaphore V return an error: ");
|
|
|
|
{
|
|
|
|
let semaphore = tm.get_obj_addrs().search_semaphore(sema_id).unwrap();
|
|
|
|
assert_eq!(semaphore.counter, 0);
|
|
|
|
assert!(semaphore.waiting_queue.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
tm.sem_v(sema_id, &mut machine).expect("semaphore V return an error: ");
|
|
|
|
{
|
|
|
|
let semaphore = tm.get_obj_addrs().search_semaphore(sema_id).unwrap();
|
|
|
|
assert_eq!(semaphore.counter, 1);
|
|
|
|
assert!(semaphore.waiting_queue.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
tm.sem_v(sema_id, &mut machine).expect("semaphore V return an error: ");
|
|
|
|
{
|
|
|
|
let semaphore = tm.get_obj_addrs().search_semaphore(sema_id).unwrap();
|
|
|
|
assert_eq!(semaphore.counter, 2);
|
|
|
|
assert!(semaphore.waiting_queue.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-08 21:10:51 +01:00
|
|
|
}
|