forked from Rativel/BurritOS
Kernel documentation. Still need to do thread.rs
This commit is contained in:
@@ -1,3 +1,11 @@
|
||||
//! # Synchronisation
|
||||
//!
|
||||
//! This module contains some scheduling and synchronisation utilities:
|
||||
//! - **Semaphore**
|
||||
//! - **Lock**
|
||||
//!
|
||||
//! Conditions aren't implemented currently
|
||||
|
||||
use crate::utility::list::List;
|
||||
use crate::kernel::thread::Thread;
|
||||
use crate::simulator::interrupt::InterruptStatus::InterruptOff;
|
||||
@@ -6,20 +14,21 @@ use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use super::thread_manager::ThreadManager;
|
||||
|
||||
/// Structure of a Semaphore used for synchronisation
|
||||
/// Structure of a Semaphore used for synchronisation.
|
||||
/// It use a counter to determine the number of thread that can be executed simultaneously.
|
||||
#[derive(PartialEq)]
|
||||
pub struct Semaphore {
|
||||
|
||||
/// Counter of simultanous Semaphore
|
||||
/// Counter of simultaneous Semaphore
|
||||
pub counter:i32,
|
||||
/// QUeue of Semaphore waiting to be exucated
|
||||
/// QUeue of Semaphore waiting to be executed
|
||||
pub waiting_queue:List<Rc<RefCell<Thread>>>,
|
||||
|
||||
}
|
||||
|
||||
impl Semaphore {
|
||||
|
||||
/// Initializes a semaphore, so that it can be used for synchronization.
|
||||
/// Initializes a semaphore, so that it can be used for synchronization.
|
||||
///
|
||||
/// ### Parameters
|
||||
/// - *counter* initial value of counter
|
||||
@@ -49,7 +58,7 @@ pub struct Lock {
|
||||
impl Lock {
|
||||
|
||||
/// Initialize a Lock, so that it can be used for synchronization.
|
||||
/// The lock is initialy free
|
||||
/// The lock is initially free
|
||||
///
|
||||
/// ### Parameters
|
||||
/// - **thread_manager** Thread manager which managing threads
|
||||
@@ -72,7 +81,7 @@ impl Lock {
|
||||
let old_status = machine.interrupt.set_status(InterruptOff);
|
||||
if self.free {
|
||||
self.free = false;
|
||||
self.owner = Option::Some(match thread_manager.get_g_current_thread() {
|
||||
self.owner = Some(match thread_manager.get_g_current_thread() {
|
||||
Some(th) => {
|
||||
Rc::clone(&th)
|
||||
},
|
||||
@@ -128,8 +137,14 @@ impl Lock {
|
||||
machine.interrupt.set_status(old_status);
|
||||
}
|
||||
|
||||
/// True if the current thread holds this lock.
|
||||
/// Say if the lock is held by the current thread
|
||||
/// Useful for checking in Release, and in Condition operations below.
|
||||
/// ### Parameters
|
||||
/// - **self** The current lock
|
||||
/// - **thread-manager** The thread manager present in the system
|
||||
/// ### Return
|
||||
/// True if the current thread holds this lock.
|
||||
|
||||
pub fn held_by_current_thread(&mut self, thread_manager: &mut ThreadManager) -> bool {
|
||||
match &self.owner {
|
||||
Some(x) =>
|
||||
|
||||
Reference in New Issue
Block a user