forked from Rativel/BurritOS
Shadow the hedgehog
This commit is contained in:
@@ -17,8 +17,6 @@ pub struct Semaphore {
|
||||
counter:i32,
|
||||
/// QUeue of Semaphore waiting to be exucated
|
||||
waiting_queue:List<Rc<RefCell<Thread>>>,
|
||||
/// Thread manager which managing threads
|
||||
thread_manager: Rc<RefCell<ThreadManager>>
|
||||
|
||||
}
|
||||
|
||||
@@ -29,8 +27,8 @@ impl Semaphore {
|
||||
/// ### Parameters
|
||||
/// - *counter* initial value of counter
|
||||
/// - *thread_manager* Thread manager which managing threads
|
||||
pub fn new(counter: i32, thread_manager: Rc<RefCell<ThreadManager>>) -> Semaphore{
|
||||
Semaphore { counter, waiting_queue: List::new(), thread_manager}
|
||||
pub fn new(counter: i32) -> Semaphore{
|
||||
Semaphore { counter, waiting_queue: List::new()}
|
||||
}
|
||||
|
||||
/// Decrement the value, and wait if it becomes < 0. Checking the
|
||||
@@ -87,8 +85,6 @@ pub struct Lock{
|
||||
owner: Option<Rc<RefCell<Thread>>>,
|
||||
/// The queue of threads waiting for execution
|
||||
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
|
||||
free: bool
|
||||
|
||||
@@ -101,8 +97,8 @@ impl Lock {
|
||||
///
|
||||
/// ### Parameters
|
||||
/// - **thread_manager** Thread manager which managing threads
|
||||
pub fn new(thread_manager: Rc<RefCell<ThreadManager>>) -> Lock {
|
||||
Lock { owner: None, waiting_queue: List::new(), thread_manager, free: true }
|
||||
pub fn new() -> Lock {
|
||||
Lock { owner: None, waiting_queue: List::new(), free: true }
|
||||
}
|
||||
|
||||
/// Wait until the lock become free. Checking the
|
||||
@@ -192,8 +188,6 @@ pub struct Condition{
|
||||
|
||||
/// The queue of threads waiting for execution
|
||||
waiting_queue:List<Rc<RefCell<Thread>>>,
|
||||
/// Thread manager which managing threads
|
||||
thread_manager: Rc<RefCell<ThreadManager>>,
|
||||
|
||||
}
|
||||
|
||||
@@ -203,8 +197,8 @@ impl Condition {
|
||||
///
|
||||
/// ### Parameters
|
||||
/// - *thread_manager* Thread manager which managing threads
|
||||
pub fn new(thread_manager: Rc<RefCell<ThreadManager>>) -> Condition {
|
||||
Condition{ waiting_queue: List::new(), thread_manager }
|
||||
pub fn new() -> Condition {
|
||||
Condition{ waiting_queue: List::new()}
|
||||
}
|
||||
|
||||
/// Block the calling thread (put it in the wait queue).
|
||||
@@ -213,13 +207,18 @@ impl Condition {
|
||||
/// ### Parameters
|
||||
/// - **current_thread** the current thread
|
||||
/// - **machine** the machine where threads are executed
|
||||
pub fn wait(&mut self, current_thread: Rc<RefCell<Thread>>, machine: &mut Machine, system: &mut System) {
|
||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||
pub fn wait(&mut self, system: &mut System) {
|
||||
let old_status = system.get_machine().interrupt.set_status(InterruptOff);
|
||||
|
||||
self.waiting_queue.push(Rc::clone(¤t_thread));
|
||||
self.thread_manager.borrow_mut().thread_sleep(system, current_thread);
|
||||
match system.get_thread_manager().get_g_current_thread() {
|
||||
Some(thread) => {
|
||||
self.waiting_queue.push(Rc::clone(thread));
|
||||
system.thread_sleep(Rc::clone(thread));
|
||||
},
|
||||
None => unreachable!()
|
||||
}
|
||||
|
||||
machine.interrupt.set_status(old_status);
|
||||
system.get_machine().interrupt.set_status(old_status);
|
||||
}
|
||||
|
||||
/// Wake up the first thread of the wait queue (if any).
|
||||
@@ -228,14 +227,14 @@ impl Condition {
|
||||
/// ### Parameters
|
||||
/// - **machine** the machine where the code is executed
|
||||
/// - **scheduler** the scheduler which determine which thread to execute
|
||||
pub fn signal(&mut self, machine: &mut Machine, scheduler: &mut Scheduler) {
|
||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||
pub fn signal(&mut self, system: &mut System) {
|
||||
let old_status = system.get_machine().interrupt.set_status(InterruptOff);
|
||||
|
||||
if self.waiting_queue.peek() != None {
|
||||
scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
|
||||
system.get_thread_manager().g_scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
|
||||
}
|
||||
|
||||
machine.interrupt.set_status(old_status);
|
||||
system.get_machine().interrupt.set_status(old_status);
|
||||
|
||||
}
|
||||
|
||||
@@ -245,13 +244,13 @@ impl Condition {
|
||||
/// ### Parameters
|
||||
/// - **machine** the machine where the code is executed
|
||||
/// - **scheduler** the scheduler which determine which thread to execute
|
||||
pub fn broadcast(&mut self, machine: &mut Machine, scheduler: &mut Scheduler) {
|
||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||
pub fn broadcast(&mut self, system: &mut System) {
|
||||
let old_status = system.get_machine().interrupt.set_status(InterruptOff);
|
||||
|
||||
while self.waiting_queue.peek() != None {
|
||||
scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
|
||||
system.get_thread_manager().g_scheduler.ready_to_run(self.waiting_queue.pop().unwrap());
|
||||
}
|
||||
machine.interrupt.set_status(old_status);
|
||||
system.get_machine().interrupt.set_status(old_status);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user