Skip to content

Commit

Permalink
thread.rs, scheduler.rs, process.rs: added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
schoettner committed Jun 18, 2024
1 parent 966cc1c commit 5ae0898
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 74 deletions.
5 changes: 3 additions & 2 deletions os/kernel/src/process/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ fn next_process_id() -> usize {

pub struct ProcessManager {
active_processes: Vec<Arc<Process>>,
exited_processes: Vec<Arc<Process>>
exited_processes: Vec<Arc<Process>> // processed by cleanup thread later
}

impl ProcessManager {
pub const fn new() -> Self {
Self { active_processes: Vec::new(), exited_processes: Vec::new() }
}

// MS ?
pub fn create_process(&mut self) -> Arc<Process> {
let address_space = match self.kernel_process() {
Some(kernel_process) => { // Create user address space
Arc::new(AddressSpace::from_other(&kernel_process.address_space()))
}
None => { // Create kernel address space
None => { // Create kernel address space (only once)
let address_space = AddressSpace::new(4);
let max_phys_addr = phys_limit().start_address();
let range = PageRange { start: Page::containing_address(VirtAddr::zero()), end: Page::containing_address(VirtAddr::new(max_phys_addr.as_u64())) };
Expand Down
17 changes: 16 additions & 1 deletion os/kernel/src/process/scheduler.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/* ╔═════════════════════════════════════════════════════════════════════════╗
║ Module: scheduler ║
╟─────────────────────────────────────────────────────────────────────────╢
║ Descr.: Implementation of the scheduler. ║
╟─────────────────────────────────────────────────────────────────────────╢
║ Author: Fabian Ruhland, HHU ║
╚═════════════════════════════════════════════════════════════════════════╝
*/
use crate::process::thread::Thread;
use alloc::collections::VecDeque;
use alloc::format;
Expand All @@ -10,12 +18,14 @@ use smallmap::Map;
use spin::{Mutex, MutexGuard};
use crate::{allocator, apic, scheduler, timer, tss};

// thread IDs
static THREAD_ID_COUNTER: AtomicUsize = AtomicUsize::new(1);

pub fn next_thread_id() -> usize {
THREAD_ID_COUNTER.fetch_add(1, Relaxed)
}

// everything related to the ready state in the scheduler
struct ReadyState {
initialized: bool,
current_thread: Option<Rc<Thread>>,
Expand All @@ -31,7 +41,7 @@ impl ReadyState {
pub struct Scheduler {
ready_state: Mutex<ReadyState>,
sleep_list: Mutex<Vec<(Rc<Thread>, usize)>>,
join_map: Mutex<Map<usize, Vec<Rc<Thread>>>>
join_map: Mutex<Map<usize, Vec<Rc<Thread>>>> // manage which threads are waiting for a thread-id to terminate
}

unsafe impl Send for Scheduler {}
Expand All @@ -44,10 +54,13 @@ pub unsafe extern "C" fn unlock_scheduler() {
}

impl Scheduler {

// create and init scheduler
pub fn new() -> Self {
Self { ready_state: Mutex::new(ReadyState::new()), sleep_list: Mutex::new(Vec::new()), join_map: Mutex::new(Map::new()) }
}

// called during creation of first thread
pub fn set_init(&self) {
self.get_ready_state().initialized = true;
}
Expand All @@ -65,6 +78,7 @@ impl Scheduler {
return Scheduler::current(&state);
}

// get thread for given id
pub fn thread(&self, thread_id: usize) -> Option<Rc<Thread>> {
self.ready_state.lock()
.ready_queue.iter().find(|thread| {
Expand Down Expand Up @@ -154,6 +168,7 @@ impl Scheduler {
}
}

//
pub fn switch_thread_no_interrupt(&self) {
self.switch_thread(false);
}
Expand Down
Loading

0 comments on commit 5ae0898

Please sign in to comment.