Merge branch 'thread_rework' into thread_scheduler
This commit is contained in:
commit
da37e0657c
@ -1,6 +1,5 @@
|
|||||||
mod process;
|
mod process;
|
||||||
pub mod thread;
|
pub mod thread;
|
||||||
pub mod scheduler;
|
|
||||||
pub mod mgerror;
|
pub mod mgerror;
|
||||||
pub mod system;
|
pub mod system;
|
||||||
mod ucontext;
|
mod ucontext;
|
||||||
|
@ -1,74 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use crate::utility::list::List;
|
|
||||||
use crate::kernel::thread::Thread;
|
|
||||||
use super::thread_manager::ThreadManager;
|
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
|
||||||
pub struct Scheduler {
|
|
||||||
ready_list: List<Rc<RefCell<Thread>>>,
|
|
||||||
pub thread_manager: Option<Rc<RefCell<ThreadManager>>>
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Scheduler {
|
|
||||||
|
|
||||||
/// Constructor
|
|
||||||
///
|
|
||||||
/// Initilize the list of ready thread
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
ready_list: List::new(),
|
|
||||||
thread_manager: Option::None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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, next_thread: Rc<RefCell<Thread>>) {
|
|
||||||
if let Some(tm) = &self.thread_manager {
|
|
||||||
let rc = Rc::clone(&tm);
|
|
||||||
if let Some(old_thread) = tm.borrow_mut().get_g_current_thread() {
|
|
||||||
rc.borrow_mut().thread_save_processor_state(Rc::clone(&old_thread));
|
|
||||||
// old_thread.save_simulator_state();
|
|
||||||
|
|
||||||
if old_thread != &next_thread {
|
|
||||||
rc.borrow_mut().thread_restore_processor_state(Rc::clone(&next_thread));
|
|
||||||
// next_thread.restore_simulator_state();
|
|
||||||
rc.borrow_mut().set_g_current_thread(Option::Some(next_thread));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
panic!("thread manager shouldn't be none");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,3 +1,4 @@
|
|||||||
|
use crate::kernel::thread_manager;
|
||||||
use crate::utility::list::List;
|
use crate::utility::list::List;
|
||||||
use crate::kernel::thread::Thread;
|
use crate::kernel::thread::Thread;
|
||||||
use crate::simulator::interrupt::InterruptStatus::InterruptOff;
|
use crate::simulator::interrupt::InterruptStatus::InterruptOff;
|
||||||
@ -5,8 +6,6 @@ use crate::simulator::machine::Machine;
|
|||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
|
||||||
use super::scheduler::Scheduler;
|
|
||||||
use super::system::System;
|
use super::system::System;
|
||||||
use super::thread_manager::ThreadManager;
|
use super::thread_manager::ThreadManager;
|
||||||
|
|
||||||
@ -17,8 +16,6 @@ pub struct Semaphore {
|
|||||||
counter:i32,
|
counter:i32,
|
||||||
/// QUeue of Semaphore waiting to be exucated
|
/// QUeue of Semaphore waiting to be exucated
|
||||||
waiting_queue:List<Rc<RefCell<Thread>>>,
|
waiting_queue:List<Rc<RefCell<Thread>>>,
|
||||||
/// Thread manager which managing threads
|
|
||||||
thread_manager: Rc<RefCell<ThreadManager>>
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,8 +26,8 @@ impl Semaphore {
|
|||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
/// - *counter* initial value of counter
|
/// - *counter* initial value of counter
|
||||||
/// - *thread_manager* Thread manager which managing threads
|
/// - *thread_manager* Thread manager which managing threads
|
||||||
pub fn new(counter: i32, thread_manager: Rc<RefCell<ThreadManager>>) -> Semaphore{
|
pub fn new(counter: i32) -> Semaphore{
|
||||||
Semaphore { counter, waiting_queue: List::new(), thread_manager}
|
Semaphore { counter, waiting_queue: List::default() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decrement the value, and wait if it becomes < 0. Checking the
|
/// Decrement the value, and wait if it becomes < 0. Checking the
|
||||||
@ -40,17 +37,24 @@ impl Semaphore {
|
|||||||
/// Note that thread_manager::thread_sleep assumes that interrupts are disabled
|
/// Note that thread_manager::thread_sleep assumes that interrupts are disabled
|
||||||
/// when it is called.
|
/// when it is called.
|
||||||
///
|
///
|
||||||
/// ### Parameters
|
/// ### Parameters TODO Refaire
|
||||||
/// - *current_thread* the current thread
|
/// - *current_thread* the current thread
|
||||||
/// - *machine* the machine where the threads are executed
|
/// - *machine* the machine where the threads are executed
|
||||||
pub fn p(&mut self, current_thread: Rc<RefCell<Thread>>, system: Rc<RefCell<System>>) {
|
pub fn p(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) {
|
||||||
let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
|
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||||
self.counter -= 1;
|
self.counter -= 1;
|
||||||
if self.counter < 0 {
|
if self.counter < 0 {
|
||||||
self.waiting_queue.push(Rc::clone(¤t_thread));
|
match thread_manager.get_g_current_thread() {
|
||||||
self.thread_manager.borrow_mut().thread_sleep(current_thread);
|
Some(thread) => {
|
||||||
|
let rc1_thread = Rc::clone(thread);
|
||||||
|
let rc2_thread = Rc::clone(thread);
|
||||||
|
self.waiting_queue.push(rc1_thread);
|
||||||
|
thread_manager.thread_sleep(machine, rc2_thread);
|
||||||
|
},
|
||||||
|
None => unreachable!("Current thread should not be None")
|
||||||
}
|
}
|
||||||
system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(old_status);
|
}
|
||||||
|
machine.interrupt.set_status(old_status);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Increment semaphore value, waking up a waiting thread if any.
|
/// Increment semaphore value, waking up a waiting thread if any.
|
||||||
@ -63,13 +67,13 @@ impl Semaphore {
|
|||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
/// - **machine** the machine where the threads are executed
|
/// - **machine** the machine where the threads are executed
|
||||||
/// - **scheduler** the scheduler which determine which thread to execute
|
/// - **scheduler** the scheduler which determine which thread to execute
|
||||||
pub fn v(&mut self, system: Rc<RefCell<System>>){
|
pub fn v(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager){
|
||||||
let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
|
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||||
self.counter += 1;
|
self.counter += 1;
|
||||||
if self.waiting_queue.peek() != None {
|
if self.waiting_queue.peek() != None {
|
||||||
system.borrow_mut().get_thread_manager().borrow_mut().g_scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
|
thread_manager.ready_to_run(self.waiting_queue.pop().unwrap());
|
||||||
}
|
}
|
||||||
system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(old_status);
|
machine.interrupt.set_status(old_status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,8 +86,6 @@ pub struct Lock{
|
|||||||
owner: Option<Rc<RefCell<Thread>>>,
|
owner: Option<Rc<RefCell<Thread>>>,
|
||||||
/// The queue of threads waiting for execution
|
/// The queue of threads waiting for execution
|
||||||
waiting_queue:List<Rc<RefCell<Thread>>>,
|
waiting_queue:List<Rc<RefCell<Thread>>>,
|
||||||
/// Thread manager which managing threads
|
|
||||||
thread_manager: Rc<RefCell<ThreadManager>>,
|
|
||||||
/// A boolean definig if the lock is free or not
|
/// A boolean definig if the lock is free or not
|
||||||
free: bool
|
free: bool
|
||||||
|
|
||||||
@ -96,8 +98,8 @@ impl Lock {
|
|||||||
///
|
///
|
||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
/// - **thread_manager** Thread manager which managing threads
|
/// - **thread_manager** Thread manager which managing threads
|
||||||
pub fn new(thread_manager: Rc<RefCell<ThreadManager>>) -> Lock {
|
pub fn new() -> Lock {
|
||||||
Lock { owner: None, waiting_queue: List::new(), thread_manager, free: true }
|
Lock { owner: None, waiting_queue: List::default(), free: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wait until the lock become free. Checking the
|
/// Wait until the lock become free. Checking the
|
||||||
@ -111,23 +113,28 @@ impl Lock {
|
|||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
/// - **current_thread** the current thread
|
/// - **current_thread** the current thread
|
||||||
/// - **machine** the machine where the threads are executed
|
/// - **machine** the machine where the threads are executed
|
||||||
pub fn acquire(&mut self, current_thread: Option<Rc<RefCell<Thread>>>, system: Rc<RefCell<System>>) {
|
pub fn acquire(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) {
|
||||||
let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
|
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||||
|
|
||||||
if self.free {
|
if self.free {
|
||||||
self.free = false;
|
self.free = false;
|
||||||
self.owner = current_thread;
|
self.owner = Option::Some(match thread_manager.get_g_current_thread() {
|
||||||
} else {
|
Some(th) => {
|
||||||
match current_thread {
|
Rc::clone(&th)
|
||||||
Some(x) => {
|
|
||||||
self.waiting_queue.push(Rc::clone(&x));
|
|
||||||
self.thread_manager.borrow_mut().thread_sleep(x)
|
|
||||||
},
|
},
|
||||||
None => ()
|
None => unreachable!()
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
match thread_manager.get_g_current_thread() {
|
||||||
|
Some(x) => {
|
||||||
|
let x = Rc::clone(&x);
|
||||||
|
self.waiting_queue.push(Rc::clone(&x));
|
||||||
|
thread_manager.thread_sleep(machine, Rc::clone(&x));
|
||||||
|
},
|
||||||
|
None => unreachable!("Current thread should not be None")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(old_status);
|
machine.interrupt.set_status(old_status);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wake up a waiter if necessary, or release it if no thread is waiting.
|
/// Wake up a waiter if necessary, or release it if no thread is waiting.
|
||||||
@ -139,17 +146,16 @@ impl Lock {
|
|||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
/// - **machine** the machine where the code is executed
|
/// - **machine** the machine where the code is executed
|
||||||
/// - **scheduler** the scheduler which determine which thread to execute
|
/// - **scheduler** the scheduler which determine which thread to execute
|
||||||
pub fn release(&mut self, system: Rc<RefCell<System>>, current_thread: Rc<RefCell<Thread>>) {
|
pub fn release(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) {
|
||||||
let old_status = system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(InterruptOff);
|
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||||
|
|
||||||
if self.held_by_current_thread(current_thread) {
|
match thread_manager.get_g_current_thread() {
|
||||||
|
Some(thread) => {
|
||||||
|
if self.held_by_current_thread(thread_manager) {
|
||||||
if self.waiting_queue.peek() != None {
|
if self.waiting_queue.peek() != None {
|
||||||
self.owner = Some(self.waiting_queue.pop().unwrap());
|
self.owner = Some(self.waiting_queue.pop().unwrap());
|
||||||
let sys = system.borrow_mut();
|
|
||||||
let tm = sys.get_thread_manager();
|
|
||||||
let scheduler = &mut tm.borrow_mut().g_scheduler;
|
|
||||||
match &self.owner {
|
match &self.owner {
|
||||||
Some(x) => scheduler.ready_to_run(Rc::clone(&x)),
|
Some(x) => thread_manager.ready_to_run(Rc::clone(&x)),
|
||||||
None => ()
|
None => ()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -157,13 +163,20 @@ impl Lock {
|
|||||||
self.owner = None;
|
self.owner = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
system.borrow_mut().get_g_machine().borrow_mut().interrupt.set_status(old_status);
|
None => ()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn held_by_current_thread(&mut self, current_thread: Rc<RefCell<Thread>>) -> bool {
|
machine.interrupt.set_status(old_status);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn held_by_current_thread(&mut self, thread_manager: &mut ThreadManager) -> bool {
|
||||||
match &self.owner {
|
match &self.owner {
|
||||||
Some(x) => Rc::ptr_eq(&x, ¤t_thread),
|
Some(x) =>
|
||||||
|
match thread_manager.get_g_current_thread() {
|
||||||
|
Some(thread) => Rc::ptr_eq(&x, &thread),
|
||||||
|
None => false
|
||||||
|
}
|
||||||
None => false
|
None => false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -174,8 +187,6 @@ pub struct Condition{
|
|||||||
|
|
||||||
/// The queue of threads waiting for execution
|
/// The queue of threads waiting for execution
|
||||||
waiting_queue:List<Rc<RefCell<Thread>>>,
|
waiting_queue:List<Rc<RefCell<Thread>>>,
|
||||||
/// Thread manager which managing threads
|
|
||||||
thread_manager: Rc<RefCell<ThreadManager>>,
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,8 +196,8 @@ impl Condition {
|
|||||||
///
|
///
|
||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
/// - *thread_manager* Thread manager which managing threads
|
/// - *thread_manager* Thread manager which managing threads
|
||||||
pub fn new(thread_manager: Rc<RefCell<ThreadManager>>) -> Condition {
|
pub fn new() -> Condition {
|
||||||
Condition{ waiting_queue: List::new(), thread_manager }
|
Condition{ waiting_queue: List::default()}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Block the calling thread (put it in the wait queue).
|
/// Block the calling thread (put it in the wait queue).
|
||||||
@ -195,11 +206,17 @@ impl Condition {
|
|||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
/// - **current_thread** the current thread
|
/// - **current_thread** the current thread
|
||||||
/// - **machine** the machine where threads are executed
|
/// - **machine** the machine where threads are executed
|
||||||
pub fn wait(&mut self, current_thread: Rc<RefCell<Thread>>, machine: &mut Machine) {
|
pub fn wait(&mut self, machine: &mut Machine, thread_manager: &mut ThreadManager) {
|
||||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||||
|
match thread_manager.get_g_current_thread() {
|
||||||
self.waiting_queue.push(Rc::clone(¤t_thread));
|
Some(thread) => {
|
||||||
self.thread_manager.borrow_mut().thread_sleep(current_thread);
|
let rc1 = Rc::clone(thread);
|
||||||
|
let rc2 = Rc::clone(thread);
|
||||||
|
self.waiting_queue.push(rc1);
|
||||||
|
thread_manager.thread_sleep(machine, rc2);
|
||||||
|
},
|
||||||
|
None => unreachable!()
|
||||||
|
}
|
||||||
|
|
||||||
machine.interrupt.set_status(old_status);
|
machine.interrupt.set_status(old_status);
|
||||||
}
|
}
|
||||||
@ -210,14 +227,14 @@ impl Condition {
|
|||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
/// - **machine** the machine where the code is executed
|
/// - **machine** the machine where the code is executed
|
||||||
/// - **scheduler** the scheduler which determine which thread to execute
|
/// - **scheduler** the scheduler which determine which thread to execute
|
||||||
pub fn signal(&mut self, machine: &mut Machine, scheduler: &mut Scheduler) {
|
pub fn signal(&mut self, system: &mut System) {
|
||||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
let old_status = system.get_machine().interrupt.set_status(InterruptOff);
|
||||||
|
|
||||||
if self.waiting_queue.peek() != None {
|
if self.waiting_queue.peek() != None {
|
||||||
scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
|
system.get_thread_manager().ready_to_run(self.waiting_queue.pop().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
machine.interrupt.set_status(old_status);
|
system.get_machine().interrupt.set_status(old_status);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,13 +244,13 @@ impl Condition {
|
|||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
/// - **machine** the machine where the code is executed
|
/// - **machine** the machine where the code is executed
|
||||||
/// - **scheduler** the scheduler which determine which thread to execute
|
/// - **scheduler** the scheduler which determine which thread to execute
|
||||||
pub fn broadcast(&mut self, machine: &mut Machine, scheduler: &mut Scheduler) {
|
pub fn broadcast(&mut self, system: &mut System) {
|
||||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
let old_status = system.get_machine().interrupt.set_status(InterruptOff);
|
||||||
|
|
||||||
while self.waiting_queue.peek() != None {
|
while self.waiting_queue.peek() != None {
|
||||||
scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
|
system.get_thread_manager().ready_to_run(self.waiting_queue.pop().unwrap());
|
||||||
}
|
}
|
||||||
machine.interrupt.set_status(old_status);
|
system.get_machine().interrupt.set_status(old_status);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,66 +260,68 @@ impl Condition {
|
|||||||
mod test {
|
mod test {
|
||||||
use std::{rc::Rc, cell::RefCell};
|
use std::{rc::Rc, cell::RefCell};
|
||||||
|
|
||||||
use crate::{kernel::{thread::Thread, synch::{Semaphore, Lock}}, init_system, simulator::machine::Machine};
|
use crate::{kernel::{thread::Thread, synch::{Semaphore, Lock}, thread_manager::ThreadManager}, init_system, simulator::machine::Machine};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_semaphore_single() {
|
fn test_semaphore_single() {
|
||||||
// Init
|
// Init
|
||||||
let system = init_system!();
|
let mut machine = Machine::init_machine();
|
||||||
let mut semaphore = Semaphore::new(1, Rc::clone(&system.borrow_mut().get_thread_manager()));
|
let mut thread_manager = ThreadManager::new();
|
||||||
|
let mut semaphore = Semaphore::new(1);
|
||||||
let thread = Rc::new(RefCell::new(Thread::new("test_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
|
// P
|
||||||
semaphore.p(thread, Rc::clone(&system));
|
semaphore.p(&mut machine, &mut thread_manager);
|
||||||
assert_eq!(semaphore.counter, 0);
|
assert_eq!(semaphore.counter, 0);
|
||||||
assert!(semaphore.waiting_queue.is_empty());
|
assert!(semaphore.waiting_queue.is_empty());
|
||||||
// V
|
// V
|
||||||
semaphore.v(Rc::clone(&system));
|
semaphore.v(&mut machine, &mut thread_manager);
|
||||||
assert_eq!(semaphore.counter, 1);
|
assert_eq!(semaphore.counter, 1);
|
||||||
assert!(semaphore.waiting_queue.is_empty());
|
assert!(semaphore.waiting_queue.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_semaphore_multiple() {
|
fn test_semaphore_multiple() {
|
||||||
// Init
|
// Init
|
||||||
let system = init_system!();
|
let mut tm = ThreadManager::new();
|
||||||
let tm = system.borrow_mut().get_thread_manager();
|
let mut machine = Machine::init_machine();
|
||||||
let mut semaphore = Semaphore::new(2, Rc::clone(&tm));
|
let mut semaphore = Semaphore::new(2);
|
||||||
let thread1 = Rc::new(RefCell::new(Thread::new("test_semaphore_1")));
|
let thread1 = Rc::new(RefCell::new(Thread::new("test_semaphore_1")));
|
||||||
let thread2 = Rc::new(RefCell::new(Thread::new("test_semaphore_2")));
|
let thread2 = Rc::new(RefCell::new(Thread::new("test_semaphore_2")));
|
||||||
let thread3 = Rc::new(RefCell::new(Thread::new("test_semaphore_3")));
|
let thread3 = Rc::new(RefCell::new(Thread::new("test_semaphore_3")));
|
||||||
|
|
||||||
let mut borrow_tm = tm.borrow_mut();
|
// let mut borrow_tm = tm.borrow_mut();
|
||||||
let scheduler = &mut borrow_tm.g_scheduler;
|
// let scheduler = &mut tm.g_scheduler;
|
||||||
scheduler.ready_to_run(Rc::clone(&thread1));
|
tm.ready_to_run(Rc::clone(&thread1));
|
||||||
scheduler.ready_to_run(Rc::clone(&thread2));
|
tm.ready_to_run(Rc::clone(&thread2));
|
||||||
scheduler.ready_to_run(Rc::clone(&thread3));
|
tm.ready_to_run(Rc::clone(&thread3));
|
||||||
// P
|
// P
|
||||||
borrow_tm.set_g_current_thread(Some(Rc::clone(&thread1)));
|
tm.set_g_current_thread(Some(Rc::clone(&thread1)));
|
||||||
semaphore.p(thread1, Rc::clone(&system));
|
semaphore.p(&mut machine, &mut tm);
|
||||||
assert_eq!(semaphore.counter, 1);
|
assert_eq!(semaphore.counter, 1);
|
||||||
assert!(semaphore.waiting_queue.is_empty());
|
assert!(semaphore.waiting_queue.is_empty());
|
||||||
|
|
||||||
borrow_tm.set_g_current_thread(Some(Rc::clone(&thread2)));
|
tm.set_g_current_thread(Some(Rc::clone(&thread2)));
|
||||||
semaphore.p(thread2, Rc::clone(&system));
|
semaphore.p(&mut machine, &mut tm);
|
||||||
assert_eq!(semaphore.counter, 0);
|
assert_eq!(semaphore.counter, 0);
|
||||||
assert!(semaphore.waiting_queue.is_empty());
|
assert!(semaphore.waiting_queue.is_empty());
|
||||||
|
|
||||||
borrow_tm.set_g_current_thread(Some(Rc::clone(&thread3)));
|
tm.set_g_current_thread(Some(Rc::clone(&thread3)));
|
||||||
semaphore.p(thread3, Rc::clone(&system));
|
semaphore.p(&mut machine, &mut tm);
|
||||||
assert_eq!(semaphore.counter, -1);
|
assert_eq!(semaphore.counter, -1);
|
||||||
assert!(semaphore.waiting_queue.iter().count() == 1);
|
assert!(semaphore.waiting_queue.iter().count() == 1);
|
||||||
|
|
||||||
// V
|
// V
|
||||||
semaphore.v(Rc::clone(&system));
|
semaphore.v(&mut machine, &mut tm);
|
||||||
assert_eq!(semaphore.counter, 0);
|
assert_eq!(semaphore.counter, 0);
|
||||||
assert!(semaphore.waiting_queue.is_empty());
|
assert!(semaphore.waiting_queue.is_empty());
|
||||||
|
|
||||||
semaphore.v(Rc::clone(&system));
|
semaphore.v(&mut machine, &mut tm);
|
||||||
assert_eq!(semaphore.counter, 1);
|
assert_eq!(semaphore.counter, 1);
|
||||||
assert!(semaphore.waiting_queue.is_empty());
|
assert!(semaphore.waiting_queue.is_empty());
|
||||||
|
|
||||||
semaphore.v(Rc::clone(&system));
|
semaphore.v(&mut machine, &mut tm);
|
||||||
assert_eq!(semaphore.counter, 2);
|
assert_eq!(semaphore.counter, 2);
|
||||||
assert!(semaphore.waiting_queue.is_empty());
|
assert!(semaphore.waiting_queue.is_empty());
|
||||||
}
|
}
|
||||||
@ -310,61 +329,61 @@ mod test {
|
|||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_lock_simple() {
|
fn test_lock_simple() {
|
||||||
let system = init_system!();
|
let mut machine = Machine::init_machine();
|
||||||
let sys = system.borrow_mut();
|
let mut tm = ThreadManager::new();
|
||||||
let tm = sys.get_thread_manager();
|
|
||||||
let thread = Rc::new(RefCell::new(Thread::new("test_lock")));
|
let thread = Rc::new(RefCell::new(Thread::new("test_lock")));
|
||||||
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread)));
|
tm.ready_to_run(Rc::clone(&thread));
|
||||||
let mut lock = Lock::new(Rc::clone(&tm));
|
tm.set_g_current_thread(Some(Rc::clone(&thread)));
|
||||||
|
let mut lock = Lock::new();
|
||||||
|
|
||||||
assert!(lock.free);
|
assert!(lock.free);
|
||||||
lock.acquire(Some(Rc::clone(&thread)), Rc::clone(&system));
|
lock.acquire(&mut machine, &mut tm);
|
||||||
assert!(lock.held_by_current_thread(Rc::clone(&thread)));
|
assert!(lock.held_by_current_thread(&mut tm));
|
||||||
|
|
||||||
assert!(!lock.free);
|
assert!(!lock.free);
|
||||||
lock.release(Rc::clone(&system), Rc::clone(&thread));
|
lock.release(&mut machine, &mut tm);
|
||||||
assert!(!lock.held_by_current_thread(thread));
|
assert!(!lock.held_by_current_thread(&mut tm));
|
||||||
assert!(lock.free);
|
assert!(lock.free);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_lock_multiple() {
|
fn test_lock_multiple() {
|
||||||
let system = init_system!();
|
|
||||||
let thread1 = Rc::new(RefCell::new(Thread::new("test_lock1")));
|
let thread1 = Rc::new(RefCell::new(Thread::new("test_lock1")));
|
||||||
let thread2 = Rc::new(RefCell::new(Thread::new("test_lock2")));
|
let thread2 = Rc::new(RefCell::new(Thread::new("test_lock2")));
|
||||||
let thread3 = Rc::new(RefCell::new(Thread::new("test_lock3")));
|
|
||||||
|
|
||||||
let tm = system.borrow_mut().get_thread_manager();
|
let mut machine = Machine::init_machine();
|
||||||
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1)));
|
let mut tm = ThreadManager::new();
|
||||||
let mut lock = Lock::new(Rc::clone(&tm));
|
|
||||||
|
tm.ready_to_run(Rc::clone(&thread1));
|
||||||
|
tm.ready_to_run(Rc::clone(&thread2));
|
||||||
|
|
||||||
|
tm.set_g_current_thread(Some(Rc::clone(&thread1)));
|
||||||
|
let mut lock = Lock::new();
|
||||||
|
|
||||||
assert!(lock.free);
|
assert!(lock.free);
|
||||||
lock.acquire(Some(Rc::clone(&thread1)), Rc::clone(&system));
|
lock.acquire(&mut machine, &mut tm);
|
||||||
assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
|
assert!(lock.held_by_current_thread(&mut tm));
|
||||||
assert!(!lock.free);
|
assert!(!lock.free);
|
||||||
|
|
||||||
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2)));
|
tm.set_g_current_thread(Some(Rc::clone(&thread2)));
|
||||||
lock.acquire(Some(Rc::clone(&thread2)), Rc::clone(&system));
|
lock.acquire(&mut machine, &mut tm);
|
||||||
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread1)));
|
|
||||||
|
|
||||||
|
|
||||||
assert!(lock.held_by_current_thread(Rc::clone(&thread1)));
|
tm.set_g_current_thread(Some(Rc::clone(&thread1)));
|
||||||
|
assert!(lock.held_by_current_thread(&mut tm));
|
||||||
assert!(lock.waiting_queue.iter().count() == 1);
|
assert!(lock.waiting_queue.iter().count() == 1);
|
||||||
assert!(!lock.free);
|
assert!(!lock.free);
|
||||||
|
|
||||||
lock.release(Rc::clone(&system), Rc::clone(&thread1));
|
lock.release(&mut machine, &mut tm);
|
||||||
assert!(!lock.held_by_current_thread(thread1));
|
assert!(!lock.held_by_current_thread(&mut tm));
|
||||||
assert!(lock.held_by_current_thread(Rc::clone(&thread2)));
|
|
||||||
|
tm.set_g_current_thread(Some(Rc::clone(&thread2)));
|
||||||
|
assert!(lock.held_by_current_thread(&mut tm));
|
||||||
assert!(!lock.free);
|
assert!(!lock.free);
|
||||||
|
|
||||||
tm.borrow_mut().set_g_current_thread(Some(Rc::clone(&thread2)));
|
lock.release(&mut machine, &mut tm);
|
||||||
|
assert!(!lock.held_by_current_thread(&mut tm));
|
||||||
|
|
||||||
lock.release(Rc::clone(&system), Rc::clone(&thread2));
|
|
||||||
assert!(!lock.held_by_current_thread(thread2));
|
|
||||||
assert!(lock.free);
|
assert!(lock.free);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,11 +2,9 @@
|
|||||||
//!
|
//!
|
||||||
//! Module containing structs and methods pertaining to the state of the operating system
|
//! Module containing structs and methods pertaining to the state of the operating system
|
||||||
|
|
||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use crate::simulator::machine::Machine;
|
use crate::simulator::machine::Machine;
|
||||||
|
|
||||||
use super::thread_manager::ThreadManager;
|
use super::{thread_manager::ThreadManager};
|
||||||
|
|
||||||
/// This macro properly initializes the system
|
/// This macro properly initializes the system
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
@ -16,10 +14,7 @@ macro_rules! init_system {
|
|||||||
init_system!(m)
|
init_system!(m)
|
||||||
}};
|
}};
|
||||||
($a:expr) => {{
|
($a:expr) => {{
|
||||||
let sys = std::rc::Rc::new(std::cell::RefCell::new(crate::System::new($a)));
|
$crate::System::new($a)
|
||||||
crate::System::freeze(std::rc::Rc::clone(&sys));
|
|
||||||
sys
|
|
||||||
|
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,8 +30,8 @@ macro_rules! init_system {
|
|||||||
/// - The scheduler which acts upon these threads
|
/// - The scheduler which acts upon these threads
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
pub struct System {
|
pub struct System {
|
||||||
g_machine: RefCell<Machine>,
|
machine: Machine,
|
||||||
thread_manager: Rc<RefCell<ThreadManager>>
|
thread_manager: ThreadManager
|
||||||
}
|
}
|
||||||
|
|
||||||
impl System {
|
impl System {
|
||||||
@ -44,37 +39,29 @@ impl System {
|
|||||||
/// System constructor
|
/// System constructor
|
||||||
pub fn new(machine: Machine) -> System {
|
pub fn new(machine: Machine) -> System {
|
||||||
Self {
|
Self {
|
||||||
g_machine: RefCell::new(machine),
|
machine,
|
||||||
thread_manager: Rc::new(RefCell::new(ThreadManager::new()))
|
thread_manager: ThreadManager::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// use thread_manager setter to send it system instance
|
|
||||||
pub fn freeze(this: Rc<RefCell<System>>) {
|
|
||||||
let copy = Rc::clone(&this);
|
|
||||||
let tm = &this.borrow_mut().thread_manager;
|
|
||||||
tm.borrow_mut().system = Option::Some(copy);
|
|
||||||
ThreadManager::freeze(tm);
|
|
||||||
}
|
|
||||||
|
|
||||||
// GETTERS
|
// GETTERS
|
||||||
|
|
||||||
/// Returns the Machine
|
/// Returns the Machine
|
||||||
///
|
///
|
||||||
/// Useful to access RAM, devices, ...
|
/// Useful to access RAM, devices, ...
|
||||||
pub fn get_g_machine(&self) -> &RefCell<Machine> {
|
pub fn get_machine(&mut self) -> &mut Machine {
|
||||||
&self.g_machine
|
&mut self.machine
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_thread_manager(&self) -> Rc<RefCell<ThreadManager>> {
|
pub fn get_thread_manager(&mut self) -> &mut ThreadManager {
|
||||||
Rc::clone(&self.thread_manager)
|
&mut self.thread_manager
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setters
|
// Setters
|
||||||
|
|
||||||
/// Assign a machine to the system
|
/// Assign a machine to the system
|
||||||
pub fn set_g_machine(&mut self, machine: RefCell<Machine>) {
|
pub fn set_machine(&mut self, machine: Machine) {
|
||||||
self.g_machine = machine
|
self.machine = machine
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -92,7 +79,7 @@ pub enum ObjectType {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
use crate::{System, Machine};
|
use crate::Machine;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_init_system() {
|
fn test_init_system() {
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use std::{rc::Rc, cell::{RefCell, RefMut, Ref}};
|
use std::{rc::Rc, cell::{RefCell, Ref}};
|
||||||
|
|
||||||
use crate::{utility::list::List, simulator::{machine::{NUM_INT_REGS, NUM_FP_REGS}, interrupt::InterruptStatus}};
|
use crate::{utility::list::List, simulator::{machine::{NUM_INT_REGS, NUM_FP_REGS, Machine}, interrupt::InterruptStatus}};
|
||||||
|
|
||||||
use super::{scheduler::Scheduler, thread::Thread, system::System, mgerror::ErrorCode, process::Process};
|
use super::{thread::Thread, mgerror::ErrorCode, process::Process};
|
||||||
|
|
||||||
pub const SIMULATORSTACKSIZE: usize = 32 * 1024;
|
pub const SIMULATORSTACKSIZE: usize = 32 * 1024;
|
||||||
|
|
||||||
@ -17,10 +17,8 @@ pub struct ThreadManager {
|
|||||||
pub g_thread_to_be_destroyed: Option<Rc<RefCell<Thread>>>,
|
pub g_thread_to_be_destroyed: Option<Rc<RefCell<Thread>>>,
|
||||||
/// The list of alive threads
|
/// The list of alive threads
|
||||||
pub g_alive: List<Rc<RefCell<Thread>>>,
|
pub g_alive: List<Rc<RefCell<Thread>>>,
|
||||||
/// The thread scheduler
|
/// Thread in ready state waiting to become active
|
||||||
pub g_scheduler: Scheduler,
|
ready_list: List<Rc<RefCell<Thread>>>,
|
||||||
/// The system owning the thread manager
|
|
||||||
pub system: Option<Rc<RefCell<System>>>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ThreadManager {
|
impl ThreadManager {
|
||||||
@ -30,15 +28,59 @@ impl ThreadManager {
|
|||||||
Self {
|
Self {
|
||||||
g_current_thread: Option::None,
|
g_current_thread: Option::None,
|
||||||
g_thread_to_be_destroyed: Option::None,
|
g_thread_to_be_destroyed: Option::None,
|
||||||
g_alive: List::new(),
|
g_alive: List::default(),
|
||||||
g_scheduler: Scheduler::new(),
|
ready_list: List::default(),
|
||||||
system: Option::None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn freeze(this: &Rc<RefCell<ThreadManager>>) {
|
/// Mark a thread as aready, but not necessarily running yet.
|
||||||
let copy = Rc::clone(this);
|
///
|
||||||
this.borrow_mut().g_scheduler.thread_manager = Option::Some(copy);
|
/// 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 => {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start a thread, attaching it to a process
|
/// Start a thread, attaching it to a process
|
||||||
@ -52,111 +94,84 @@ impl ThreadManager {
|
|||||||
thread_m.init_simulator_context(base_stack_addr);
|
thread_m.init_simulator_context(base_stack_addr);
|
||||||
thread_m.process.as_mut().unwrap().num_thread += 1;
|
thread_m.process.as_mut().unwrap().num_thread += 1;
|
||||||
self.get_g_alive().push(Rc::clone(&thread));
|
self.get_g_alive().push(Rc::clone(&thread));
|
||||||
self.g_scheduler.ready_to_run(Rc::clone(&thread));
|
self.ready_to_run(Rc::clone(&thread));
|
||||||
Result::Ok(())
|
Result::Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wait for another thread to finish its execution
|
/// Wait for another thread to finish its execution
|
||||||
pub fn thread_join(&mut self, id_thread: Rc<RefCell<Thread>>) {
|
pub fn thread_join(&mut self, machine: &mut Machine, id_thread: Rc<RefCell<Thread>>) {
|
||||||
while self.get_g_alive().contains(&Rc::clone(&id_thread)) {
|
while self.get_g_alive().contains(&Rc::clone(&id_thread)) {
|
||||||
self.thread_yield(Rc::clone(&id_thread));
|
self.thread_yield(machine, Rc::clone(&id_thread));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Relinquish the CPU if any other thread is runnable.
|
/// Relinquish the CPU if any other thread is runnable.
|
||||||
///
|
///
|
||||||
/// Cannot use yield as a function name -> reserved name in rust
|
/// Cannot use yield as a function name -> reserved name in rust
|
||||||
pub fn thread_yield(&mut self, thread: Rc<RefCell<Thread>>) {
|
pub fn thread_yield(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
||||||
if let Some(system) = &self.system {
|
|
||||||
let sys = system.borrow_mut();
|
|
||||||
let mut machine = sys.get_g_machine().borrow_mut();
|
|
||||||
let old_status = machine.interrupt.set_status(crate::simulator::interrupt::InterruptStatus::InterruptOff);
|
let old_status = machine.interrupt.set_status(crate::simulator::interrupt::InterruptStatus::InterruptOff);
|
||||||
|
|
||||||
assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread);
|
assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread);
|
||||||
let next_thread = self.g_scheduler.find_next_to_run();
|
let next_thread = self.find_next_to_run();
|
||||||
if let Some(next_thread) = next_thread {
|
if let Some(next_thread) = next_thread {
|
||||||
let scheduler = &mut self.g_scheduler;
|
self.ready_to_run(thread);
|
||||||
scheduler.ready_to_run(thread);
|
self.switch_to(machine, next_thread);
|
||||||
scheduler.switch_to(next_thread);
|
|
||||||
}
|
}
|
||||||
machine.interrupt.set_status(old_status);
|
machine.interrupt.set_status(old_status);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Put the thread to sleep and relinquish the processor
|
/// Put the thread to sleep and relinquish the processor
|
||||||
pub fn thread_sleep(&mut self, thread: Rc<RefCell<Thread>>) {
|
pub fn thread_sleep(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
||||||
|
|
||||||
assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread);
|
assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread);
|
||||||
if let Some(system) = &self.system {
|
|
||||||
let sys = system.borrow_mut();
|
|
||||||
let machine = sys.get_g_machine().borrow_mut();
|
|
||||||
assert_eq!(machine.interrupt.get_status(), InterruptStatus::InterruptOff);
|
assert_eq!(machine.interrupt.get_status(), InterruptStatus::InterruptOff);
|
||||||
|
|
||||||
let mut next_thread = self.g_scheduler.find_next_to_run();
|
let mut next_thread = self.find_next_to_run();
|
||||||
while next_thread.is_none() {
|
while next_thread.is_none() {
|
||||||
eprintln!("Nobody to run => idle");
|
eprintln!("Nobody to run => idle");
|
||||||
machine.interrupt.idle();
|
machine.interrupt.idle();
|
||||||
next_thread = self.g_scheduler.find_next_to_run();
|
next_thread = self.find_next_to_run();
|
||||||
}
|
}
|
||||||
self.g_scheduler.switch_to(Rc::clone(&next_thread.unwrap()));
|
self.switch_to(machine, Rc::clone(&next_thread.unwrap()));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Finish the execution of the thread and prepare its deallocation
|
/// Finish the execution of the thread and prepare its deallocation
|
||||||
pub fn thread_finish(&mut self, thread: Rc<RefCell<Thread>>) {
|
pub fn thread_finish(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
||||||
if let Some(system) = &self.system {
|
|
||||||
let sys = Rc::clone(system);
|
|
||||||
let sys = sys.borrow_mut();
|
|
||||||
let mut machine = sys.get_g_machine().borrow_mut();
|
|
||||||
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
|
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
|
||||||
self.g_thread_to_be_destroyed = Option::Some(Rc::clone(&thread));
|
self.g_thread_to_be_destroyed = Option::Some(Rc::clone(&thread));
|
||||||
self.g_alive.remove(Rc::clone(&thread));
|
self.g_alive.remove(Rc::clone(&thread));
|
||||||
// g_objets_addrs->removeObject(self.thread) // a ajouté plus tard
|
// g_objets_addrs->removeObject(self.thread) // a ajouté plus tard
|
||||||
self.thread_sleep(Rc::clone(&thread));
|
self.thread_sleep(machine, Rc::clone(&thread));
|
||||||
machine.interrupt.set_status(old_status);
|
machine.interrupt.set_status(old_status);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn thread_save_processor_state(&mut self, thread: Rc<RefCell<Thread>>) {
|
pub fn thread_save_processor_state(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
||||||
if let Some(system) = &self.system {
|
let mut t = thread.borrow_mut();
|
||||||
let mut t: RefMut<_> = thread.borrow_mut();
|
|
||||||
let system = system.borrow_mut();
|
|
||||||
for i in 0..NUM_INT_REGS {
|
for i in 0..NUM_INT_REGS {
|
||||||
t.thread_context.int_registers[i] = system.get_g_machine().borrow().read_int_register(i);
|
t.thread_context.int_registers[i] = machine.read_int_register(i);
|
||||||
}
|
}
|
||||||
for i in 0..NUM_FP_REGS {
|
for i in 0..NUM_FP_REGS {
|
||||||
t.thread_context.float_registers[i] = system.get_g_machine().borrow().read_fp_register(i);
|
t.thread_context.float_registers[i] = machine.read_fp_register(i);
|
||||||
}
|
|
||||||
} else {
|
|
||||||
unreachable!("System is None")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn thread_restore_processor_state(&self, thread: Rc<RefCell<Thread>>) {
|
pub fn thread_restore_processor_state(&self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
||||||
if let Some(system) = &self.system {
|
|
||||||
let system = system.borrow_mut();
|
|
||||||
let t: Ref<_> = thread.borrow();
|
let t: Ref<_> = thread.borrow();
|
||||||
for i in 0..NUM_INT_REGS {
|
for i in 0..NUM_INT_REGS {
|
||||||
let machine = system.get_g_machine();
|
|
||||||
let mut machine = machine.borrow_mut();
|
|
||||||
machine.write_int_register(i, t.thread_context.int_registers[i]);
|
machine.write_int_register(i, t.thread_context.int_registers[i]);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
unreachable!("System is None")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Currently running thread
|
/// Currently running thread
|
||||||
pub fn get_g_current_thread(&mut self) -> &mut Option<Rc<RefCell<Thread>>> {
|
pub fn get_g_current_thread(&mut self) -> &Option<Rc<RefCell<Thread>>> {
|
||||||
&mut self.g_current_thread
|
&self.g_current_thread
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Thread to be destroyed by [...]
|
/// Thread to be destroyed by [...]
|
||||||
///
|
///
|
||||||
/// TODO: Finish the comment with the relevant value
|
/// TODO: Finish the comment with the relevant value
|
||||||
pub fn get_g_thread_to_be_destroyed(&mut self) -> &mut Option<Rc<RefCell<Thread>>> {
|
pub fn get_g_thread_to_be_destroyed(&mut self) -> &Option<Rc<RefCell<Thread>>> {
|
||||||
&mut self.g_thread_to_be_destroyed
|
&self.g_thread_to_be_destroyed
|
||||||
}
|
}
|
||||||
|
|
||||||
/// List of alive threads
|
/// List of alive threads
|
||||||
|
@ -21,6 +21,4 @@ use simulator::machine::Machine;
|
|||||||
fn main() {
|
fn main() {
|
||||||
let machine = Machine::init_machine();
|
let machine = Machine::init_machine();
|
||||||
let system = Rc::new(RefCell::new(System::new(machine)));
|
let system = Rc::new(RefCell::new(System::new(machine)));
|
||||||
|
|
||||||
System::freeze(system);
|
|
||||||
}
|
}
|
||||||
|
@ -1,62 +1,88 @@
|
|||||||
//! Data structure and definition of a genericsingle-linked LIFO list.
|
//! Data structure and definition of a genericsingle-linked LIFO list.
|
||||||
|
|
||||||
|
use std::ptr;
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
pub struct List<T: PartialEq> {
|
pub struct List<T: PartialEq> {
|
||||||
head: Link<T>,
|
head: Link<T>,
|
||||||
|
tail: *mut Node<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Link<T> = *mut Node<T>;
|
||||||
|
|
||||||
type Link<T> = Option<Box<Node<T>>>;
|
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
struct Node<T> {
|
struct Node<T> {
|
||||||
elem: T,
|
elem: T,
|
||||||
next: Link<T>,
|
next: Link<T>,
|
||||||
}
|
}
|
||||||
|
/// Iterator structure for use in a for loop, pop elements before returning it
|
||||||
|
pub struct IntoIter<T: PartialEq>(List<T>);
|
||||||
|
|
||||||
|
/// Iterator structure for use in a for loop, dereference before returning it
|
||||||
|
pub struct Iter<'a, T> {
|
||||||
|
next: Option<&'a Node<T>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Same as Iter structure, returned item are mutable
|
||||||
|
pub struct IterMut<'a, T> {
|
||||||
|
next: Option<&'a mut Node<T>>,
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: PartialEq> List<T> {
|
impl<T: PartialEq> List<T> {
|
||||||
|
|
||||||
/// Create an empty list
|
|
||||||
pub fn new() -> Self {
|
|
||||||
List { head: None }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Push an item at the end of the list
|
/// Push an item at the end of the list
|
||||||
pub fn push(&mut self, elem: T) {
|
pub fn push(&mut self, elem: T) {
|
||||||
let new_node = Box::new(Node {
|
unsafe {
|
||||||
elem: elem,
|
let new_tail = Box::into_raw(Box::new(Node {
|
||||||
next: self.head.take(),
|
elem,
|
||||||
});
|
next: ptr::null_mut(),
|
||||||
|
}));
|
||||||
|
|
||||||
self.head = Some(new_node);
|
if !self.tail.is_null() {
|
||||||
|
(*self.tail).next = new_tail;
|
||||||
|
} else {
|
||||||
|
self.head = new_tail;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieve and remove the item at the end of the list.
|
self.tail = new_tail;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Retrieve and remove the item at the head of the list.
|
||||||
///
|
///
|
||||||
/// Return None if list is empty
|
/// Return None if list is empty
|
||||||
pub fn pop(&mut self) -> Option<T> {
|
pub fn pop(&mut self) -> Option<T> {
|
||||||
self.head.take().map(|node| {
|
unsafe {
|
||||||
self.head = node.next;
|
if self.head.is_null() {
|
||||||
node.elem
|
None
|
||||||
})
|
} else {
|
||||||
|
let head = Box::from_raw(self.head);
|
||||||
|
self.head = head.next;
|
||||||
|
if self.head.is_null() {
|
||||||
|
self.tail = ptr::null_mut();
|
||||||
|
}
|
||||||
|
Some(head.elem)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieve without removing the item at the end of the list
|
/// Retrieve without removing the item at the head of the list
|
||||||
///
|
///
|
||||||
/// Return None if list is empty
|
/// Return None if list is empty
|
||||||
pub fn peek(&self) -> Option<&T> {
|
pub fn peek(&self) -> Option<&T> {
|
||||||
self.head.as_ref().map(|node| {
|
unsafe {
|
||||||
&node.elem
|
self.head.as_ref().map(|node| &node.elem)
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieve without removing the item at the end of the list as mutable
|
/// Retrieve without removing the item at the head of the list as mutable
|
||||||
///
|
///
|
||||||
/// Return None if lsit is empty
|
/// Return None if lsit is empty
|
||||||
pub fn peek_mut(&mut self) -> Option<&mut T> {
|
pub fn peek_mut(&mut self) -> Option<&mut T> {
|
||||||
self.head.as_mut().map(|node| {
|
unsafe {
|
||||||
&mut node.elem
|
self.head.as_mut().map(|node| &mut node.elem)
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Search for an element in the list
|
/// Search for an element in the list
|
||||||
@ -66,10 +92,12 @@ impl<T: PartialEq> List<T> {
|
|||||||
/// Worst case complexity of this function is O(n)
|
/// Worst case complexity of this function is O(n)
|
||||||
pub fn contains(&self, elem: &T) -> bool {
|
pub fn contains(&self, elem: &T) -> bool {
|
||||||
let mut iter = self.iter();
|
let mut iter = self.iter();
|
||||||
let element = iter.next();
|
let mut element = iter.next();
|
||||||
while element.is_some() {
|
while element.is_some() {
|
||||||
if element.unwrap() == elem {
|
if element.unwrap() == elem {
|
||||||
return true;
|
return true;
|
||||||
|
} else {
|
||||||
|
element = iter.next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
@ -81,26 +109,26 @@ impl<T: PartialEq> List<T> {
|
|||||||
///
|
///
|
||||||
/// Worst-case complexity is O(n)
|
/// Worst-case complexity is O(n)
|
||||||
pub fn remove(&mut self, item: T)-> bool {
|
pub fn remove(&mut self, item: T)-> bool {
|
||||||
let mut found = false;
|
unsafe {
|
||||||
let mut tmp_list: List<T> = List::new();
|
let mut current: *mut Node<T> = self.head;
|
||||||
while !self.is_empty() {
|
let mut previous: *mut Node<T> = ptr::null_mut();
|
||||||
let current = self.pop().unwrap();
|
while !current.is_null() {
|
||||||
if current != item {
|
if (*current).elem == item {
|
||||||
tmp_list.push(current);
|
(*previous).next = (*current).next;
|
||||||
|
drop(Box::from_raw(current).elem);
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
found = true;
|
previous = current;
|
||||||
break;
|
current = (*current).next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while !tmp_list.is_empty() {
|
|
||||||
self.push(tmp_list.pop().unwrap());
|
|
||||||
}
|
}
|
||||||
found
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return true if the list is empty, false otherwise
|
/// Return true if the list is empty, false otherwise
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.head.is_none()
|
self.head.is_null()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Turn the list into an iterator for use in a for loop per example.
|
/// Turn the list into an iterator for use in a for loop per example.
|
||||||
@ -114,26 +142,33 @@ impl<T: PartialEq> List<T> {
|
|||||||
///
|
///
|
||||||
/// When you iter using this method, elements are dereferenced
|
/// When you iter using this method, elements are dereferenced
|
||||||
pub fn iter(&self) -> Iter<'_, T> {
|
pub fn iter(&self) -> Iter<'_, T> {
|
||||||
Iter { next: self.head.as_deref() }
|
unsafe {
|
||||||
|
Iter { next: self.head.as_ref() }
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Same as iter but make the iterator mutable
|
/// Same as iter but make the iterator mutable
|
||||||
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
|
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
|
||||||
IterMut { next: self.head.as_deref_mut() }
|
unsafe {
|
||||||
|
IterMut { next: self.head.as_mut() }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: PartialEq> Default for List<T> {
|
||||||
|
/// Create an empty list
|
||||||
|
fn default() -> Self {
|
||||||
|
Self { head: ptr::null_mut(), tail: ptr::null_mut() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: PartialEq> Drop for List<T> {
|
impl<T: PartialEq> Drop for List<T> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
let mut cur_link = self.head.take();
|
while self.pop().is_some() {} // removing every item from list (necessary as we using unsafe function)
|
||||||
while let Some(mut boxed_node) = cur_link {
|
|
||||||
cur_link = boxed_node.next.take();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Iterator structure for use in a for loop, pop elements before returning it
|
|
||||||
pub struct IntoIter<T: PartialEq>(List<T>);
|
|
||||||
|
|
||||||
impl<T: PartialEq> Iterator for IntoIter<T> {
|
impl<T: PartialEq> Iterator for IntoIter<T> {
|
||||||
type Item = T;
|
type Item = T;
|
||||||
@ -143,35 +178,32 @@ impl<T: PartialEq> Iterator for IntoIter<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterator structure for use in a for loop, dereference before returning it
|
|
||||||
pub struct Iter<'a, T> {
|
|
||||||
next: Option<&'a Node<T>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, T> Iterator for Iter<'a, T> {
|
impl<'a, T> Iterator for Iter<'a, T> {
|
||||||
type Item = &'a T;
|
type Item = &'a T;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
unsafe {
|
||||||
self.next.map(|node| {
|
self.next.map(|node| {
|
||||||
self.next = node.next.as_deref();
|
self.next = node.next.as_ref();
|
||||||
&node.elem
|
&node.elem
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Same as Iter structure, returned item are mutable
|
|
||||||
pub struct IterMut<'a, T> {
|
|
||||||
next: Option<&'a mut Node<T>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> Iterator for IterMut<'a, T> {
|
impl<'a, T> Iterator for IterMut<'a, T> {
|
||||||
type Item = &'a mut T;
|
type Item = &'a mut T;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
unsafe {
|
||||||
self.next.take().map(|node| {
|
self.next.take().map(|node| {
|
||||||
self.next = node.next.as_deref_mut();
|
self.next = node.next.as_mut();
|
||||||
&mut node.elem
|
&mut node.elem
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -180,7 +212,7 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn basics() {
|
fn basics() {
|
||||||
let mut list = List::new();
|
let mut list = List::default();
|
||||||
|
|
||||||
// Check empty list behaves right
|
// Check empty list behaves right
|
||||||
assert_eq!(list.pop(), None);
|
assert_eq!(list.pop(), None);
|
||||||
@ -191,7 +223,7 @@ mod test {
|
|||||||
list.push(3);
|
list.push(3);
|
||||||
|
|
||||||
// Check normal removal
|
// Check normal removal
|
||||||
assert_eq!(list.pop(), Some(3));
|
assert_eq!(list.pop(), Some(1));
|
||||||
assert_eq!(list.pop(), Some(2));
|
assert_eq!(list.pop(), Some(2));
|
||||||
|
|
||||||
// Push some more just to make sure nothing's corrupted
|
// Push some more just to make sure nothing's corrupted
|
||||||
@ -199,63 +231,128 @@ mod test {
|
|||||||
list.push(5);
|
list.push(5);
|
||||||
|
|
||||||
// Check normal removal
|
// Check normal removal
|
||||||
assert_eq!(list.pop(), Some(5));
|
assert_eq!(list.pop(), Some(3));
|
||||||
assert_eq!(list.pop(), Some(4));
|
assert_eq!(list.pop(), Some(4));
|
||||||
|
|
||||||
// Check exhaustion
|
// Check exhaustion
|
||||||
assert_eq!(list.pop(), Some(1));
|
assert_eq!(list.pop(), Some(5));
|
||||||
assert_eq!(list.pop(), None);
|
assert_eq!(list.pop(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn peek() {
|
fn peek() {
|
||||||
let mut list = List::new();
|
let mut list = List::default();
|
||||||
assert_eq!(list.peek(), None);
|
assert_eq!(list.peek(), None);
|
||||||
assert_eq!(list.peek_mut(), None);
|
assert_eq!(list.peek_mut(), None);
|
||||||
list.push(1); list.push(2); list.push(3);
|
list.push(1);
|
||||||
|
list.push(2);
|
||||||
|
list.push(3);
|
||||||
|
|
||||||
assert_eq!(list.peek(), Some(&3));
|
assert_eq!(list.peek(), Some(&1));
|
||||||
assert_eq!(list.peek_mut(), Some(&mut 3));
|
assert_eq!(list.peek_mut(), Some(&mut 1));
|
||||||
|
|
||||||
list.peek_mut().map(|value| {
|
|
||||||
*value = 42
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_eq!(list.peek(), Some(&42));
|
|
||||||
assert_eq!(list.pop(), Some(42));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn into_iter() {
|
fn into_iter() {
|
||||||
let mut list = List::new();
|
let mut list = List::default();
|
||||||
list.push(1); list.push(2); list.push(3);
|
list.push(1);
|
||||||
|
list.push(2);
|
||||||
|
list.push(3);
|
||||||
|
|
||||||
let mut iter = list.into_iter();
|
let mut iter = list.into_iter();
|
||||||
assert_eq!(iter.next(), Some(3));
|
|
||||||
assert_eq!(iter.next(), Some(2));
|
|
||||||
assert_eq!(iter.next(), Some(1));
|
assert_eq!(iter.next(), Some(1));
|
||||||
|
assert_eq!(iter.next(), Some(2));
|
||||||
|
assert_eq!(iter.next(), Some(3));
|
||||||
assert_eq!(iter.next(), None);
|
assert_eq!(iter.next(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn iter() {
|
fn iter() {
|
||||||
let mut list = List::new();
|
let mut list = List::default();
|
||||||
list.push(1); list.push(2); list.push(3);
|
list.push(1);
|
||||||
|
list.push(2);
|
||||||
|
list.push(3);
|
||||||
|
|
||||||
let mut iter = list.iter();
|
let mut iter = list.iter();
|
||||||
assert_eq!(iter.next(), Some(&3));
|
|
||||||
assert_eq!(iter.next(), Some(&2));
|
|
||||||
assert_eq!(iter.next(), Some(&1));
|
assert_eq!(iter.next(), Some(&1));
|
||||||
|
assert_eq!(iter.next(), Some(&2));
|
||||||
|
assert_eq!(iter.next(), Some(&3));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn iter_mut() {
|
fn iter_mut() {
|
||||||
let mut list = List::new();
|
let mut list = List::default();
|
||||||
list.push(1); list.push(2); list.push(3);
|
list.push(1);
|
||||||
|
list.push(2);
|
||||||
|
list.push(3);
|
||||||
|
|
||||||
let mut iter = list.iter_mut();
|
let mut iter = list.iter_mut();
|
||||||
assert_eq!(iter.next(), Some(&mut 3));
|
|
||||||
assert_eq!(iter.next(), Some(&mut 2));
|
|
||||||
assert_eq!(iter.next(), Some(&mut 1));
|
assert_eq!(iter.next(), Some(&mut 1));
|
||||||
|
assert_eq!(iter.next(), Some(&mut 2));
|
||||||
|
assert_eq!(iter.next(), Some(&mut 3));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn contains_test() {
|
||||||
|
let mut list = List::default();
|
||||||
|
assert_eq!(list.peek(), None);
|
||||||
|
list.push(1);
|
||||||
|
list.push(2);
|
||||||
|
list.push(3);
|
||||||
|
|
||||||
|
assert_eq!(list.contains(&1), true);
|
||||||
|
assert_eq!(list.contains(&4), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn remove_test() {
|
||||||
|
let mut list = List::default();
|
||||||
|
assert_eq!(list.peek(), None);
|
||||||
|
list.push(1);
|
||||||
|
list.push(2);
|
||||||
|
list.push(3);
|
||||||
|
|
||||||
|
assert_eq!(list.contains(&2), true);
|
||||||
|
list.remove(2);
|
||||||
|
assert_eq!(list.contains(&2), false);
|
||||||
|
assert_eq!(list.pop(), Option::Some(1));
|
||||||
|
assert_eq!(list.pop(), Option::Some(3));
|
||||||
|
assert_eq!(list.peek(), Option::None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn miri_test() {
|
||||||
|
let mut list = List::default();
|
||||||
|
|
||||||
|
list.push(1);
|
||||||
|
list.push(2);
|
||||||
|
list.push(3);
|
||||||
|
|
||||||
|
assert!(list.pop() == Some(1));
|
||||||
|
list.push(4);
|
||||||
|
assert!(list.pop() == Some(2));
|
||||||
|
list.push(5);
|
||||||
|
|
||||||
|
assert!(list.peek() == Some(&3));
|
||||||
|
list.push(6);
|
||||||
|
list.peek_mut().map(|x| *x *= 10);
|
||||||
|
assert!(list.peek() == Some(&30));
|
||||||
|
assert!(list.pop() == Some(30));
|
||||||
|
|
||||||
|
for elem in list.iter_mut() {
|
||||||
|
*elem *= 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut iter = list.iter();
|
||||||
|
assert_eq!(iter.next(), Some(&400));
|
||||||
|
assert_eq!(iter.next(), Some(&500));
|
||||||
|
assert_eq!(iter.next(), Some(&600));
|
||||||
|
assert_eq!(iter.next(), None);
|
||||||
|
assert_eq!(iter.next(), None);
|
||||||
|
|
||||||
|
assert!(list.pop() == Some(400));
|
||||||
|
list.peek_mut().map(|x| *x *= 10);
|
||||||
|
assert!(list.peek() == Some(&5000));
|
||||||
|
list.push(7);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user