Skip to content

Commit

Permalink
Shell: fix test_threads
Browse files Browse the repository at this point in the history
The `test_threads` command used to display a seemingly random list
of variable number of As and Bs.
This was due to the fact that the `write!` macro internally makes
IPC calls, which reschedules the thread, while it is still holding
the lock.

When the concurrent threads gets to run, it sees that the lock is
held, so it does nothing and skips its turn silently.

We now make sure that every thread prints exactly 10 times,
not counting the rounds that are skipped.

This closes #224
  • Loading branch information
Orycterope committed Jun 17, 2019
1 parent 145e24c commit 497f027
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions shell/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ fn test_threads(terminal: Terminal) -> Terminal {
let terminal = unsafe {
Arc::from_raw(terminal as *const Mutex<Terminal>)
};
for _ in 0..10 {
let mut i = 0;
while i < 10 {
if let Some(mut lock) = terminal.try_lock() {
let _ = writeln!(lock, "A");
i += 1;
}
let _ = libuser::syscalls::sleep_thread(0);
}
Expand All @@ -130,9 +132,11 @@ fn test_threads(terminal: Terminal) -> Terminal {
let terminal = unsafe {
Arc::from_raw(terminal as *const Mutex<Terminal>)
};
for _ in 0..10 {
let mut i = 0;
while i < 10 {
if let Some(mut lock) = terminal.try_lock() {
let _ = writeln!(lock, "B");
i += 1;
}
let _ = libuser::syscalls::sleep_thread(0);
}
Expand Down

0 comments on commit 497f027

Please sign in to comment.