Skip to content

Commit

Permalink
wip: tiny change
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed May 18, 2022
1 parent bd59578 commit 056c28c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions pkg/app/dining/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ fn main() -> usize {
CHOPSTICK[i].init(1);
}


for i in 0..5 {
let pid = sys_fork();
if pid == 0 {
Expand All @@ -30,7 +29,7 @@ fn main() -> usize {
println!("#{} holds threads: {:?}", cpid, &pids);

sys_stat();

for i in 0..5 {
println!("#{} Waiting for #{}...", cpid, pids[i]);
sys_wait_pid(pids[i]);
Expand All @@ -40,18 +39,19 @@ fn main() -> usize {
}

fn philosopher(id: usize) -> ! {
let pid = sys_get_pid();
for _ in 0..20 {
if id == 0 {
println!("philosopher #{} is sleeping...", id);
println!("philosopher #{}({}) is sleeping...", id, pid);
core::hint::spin_loop();
}

println!("philosopher #{} is thinking...", id);
println!("philosopher #{}({}) is thinking...", id, pid);

CHOPSTICK[id].acquire();
CHOPSTICK[(id + 1) % 5].acquire();

println!("philosopher #{} is eating...", id);
println!("philosopher #{}({}) is eating...", id, pid);

CHOPSTICK[(id + 1) % 5].release();
CHOPSTICK[id].release();
Expand Down
12 changes: 7 additions & 5 deletions pkg/app/mq/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main() -> usize {


sys_stat();

for i in 0..QUEUE_COUNT {
println!("#{} Waiting for #{}...", cpid, pids[i]);
sys_wait_pid(pids[i]);
Expand All @@ -53,25 +53,27 @@ fn main() -> usize {
}

unsafe fn producer(id: usize) -> ! {
println!("New producer #{}", id);
let pid = sys_get_pid();
println!("New producer #{}({})", id, pid);
for _ in 0..20 {
IS_NOT_FULL.acquire();
MUTEX.acquire();
COUNT += 1;
println!("Produced by #{} count={}", id, &COUNT);
println!("Produced by #{}({}) count={}", id, pid, &COUNT);
MUTEX.release();
IS_NOT_EMPTY.release();
}
sys_exit(0);
}

unsafe fn consumer(id: usize) -> ! {
println!("New consumer #{}", id);
let pid = sys_get_pid();
println!("New consumer #{}({})", id, pid);
for _ in 0..20 {
IS_NOT_EMPTY.acquire();
MUTEX.acquire();
COUNT -= 1;
println!("Consumed by #{} count={}", id, &COUNT);
println!("Consumed by #{}({}) count={}", id, pid, &COUNT);
MUTEX.release();
IS_NOT_FULL.release();
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/kernel/src/process/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl ProcessManager {
}

pub fn print_process_list(&self) {
let mut output = String::from(" PID | PPID | Name | Ticks | Status\n");
let mut output = String::from(" PID | PPID | Name | Ticks | Status\n");
for p in self.processes.iter() {
output = output + format!("{}\n", p).as_str();
}
Expand Down Expand Up @@ -234,8 +234,6 @@ impl ProcessManager {
}

pub fn kill(&mut self, pid: ProcessId, ret: isize) {
trace!("Killing process #{} with ret code: {}", pid, ret);

let p = self.processes.iter().find(|x| x.pid() == pid);

if p.is_none() {
Expand All @@ -245,6 +243,8 @@ impl ProcessManager {

let p = p.unwrap();

debug!("Killing process {}#{} with ret code: {}", p.name(), pid, ret);

let parent = p.parent();
let children = p.children();
let cur_page_table_addr = p.page_table_addr().start_address().as_u64();
Expand Down
2 changes: 1 addition & 1 deletion pkg/kernel/src/process/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ impl core::fmt::Display for Process {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(
f,
" #{:-3} | #{:-3} | {:12} | {:8} | {:?}",
" #{:-3} | #{:-3} | {:13} | {:8} | {:?}",
u16::from(self.pid),
u16::from(self.parent),
self.name,
Expand Down
35 changes: 14 additions & 21 deletions pkg/kernel/src/process/sync.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use alloc::{collections::BTreeMap, vec::Vec};
use spin::Mutex;
use super::ProcessId;

once_mutex!(pub SEMAPHORES: BTreeMap<SemaphoreId, Semaphore>);
Expand All @@ -17,51 +16,45 @@ impl SemaphoreId {
}
}

/// Mutex is provided by the sem manager,
/// We need not to protect the count again
pub struct Semaphore {
count: Mutex<usize>,
count: usize,
wait_queue: Vec<ProcessId>,
}

impl Semaphore {
pub fn new(value: usize) -> Self {
Self {
count: Mutex::new(value),
count: value,
wait_queue: Vec::new()
}
}

pub fn down(&mut self, pid: ProcessId) -> Result<(),()> {
if let Some(mut count) = self.count.try_lock() {
if *count == 0 {
self.wait_queue.push(pid);
Err(())
} else {
*count -= 1;
// trace!("Semaphore down: {}", count);
Ok(())
}
if self.count == 0 {
self.wait_queue.push(pid);
Err(())
} else {
self.count -= 1;
// trace!("Semaphore down: {}", count);
Ok(())
}
}

pub fn up(&mut self) -> Option<ProcessId> {
if let Some(mut count) = self.count.try_lock() {
// trace!("Semaphore up: {}", count);
if !self.wait_queue.is_empty() {
Some(self.wait_queue.pop().unwrap())
} else {
*count += 1;
None
}
// trace!("Semaphore up: {}", count);
if !self.wait_queue.is_empty() {
Some(self.wait_queue.pop().unwrap())
} else {
self.count += 1;
None
}
}
}

impl core::fmt::Display for Semaphore {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Semaphore({}) {:?}", self.count.lock(), self.wait_queue)
write!(f, "Semaphore({}) {:?}", self.count, self.wait_queue)
}
}

0 comments on commit 056c28c

Please sign in to comment.