Skip to content

Commit

Permalink
playwav: Yield cpu while waiting for audio to catch up
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalmiel committed Sep 24, 2024
1 parent 02e9cc4 commit 089307a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
17 changes: 15 additions & 2 deletions userspace/playwav/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ fn play(song: &str) -> Result<(), ExitCode> {
)
.map_err(|_e| ExitCode::from(4))?;

for addr in (wav_map..wav_map + wav_size).step_by(4096) {
let _ = unsafe {
// prefault to read the file into memory
(addr as *const u64).read_volatile()
};
}

//println!("file size: {}", wav_size);

let get_current_hda_block =
Expand All @@ -52,7 +59,10 @@ fn play(song: &str) -> Result<(), ExitCode> {

let chunk = std::cmp::min(rem, 2048);

while ((get_current_hda_block() + 3) % buf_block_count) != wp_block {}
while ((get_current_hda_block() + 3) % buf_block_count) != wp_block {
// yield cpu for other tasks
let _ = syscall_user::yield_execution();
}

hda_data[wp_block * 2048..wp_block * 2048 + chunk]
.copy_from_slice(&wav_data[file_block * 2048..file_block * 2048 + chunk]);
Expand All @@ -61,7 +71,10 @@ fn play(song: &str) -> Result<(), ExitCode> {
file_block += 1;
}

while get_current_hda_block() != wp_block {}
while get_current_hda_block() != wp_block {
// yield cpu for other tasks
let _ = syscall_user::yield_execution();
}

hda_data.fill(0); //silence

Expand Down
2 changes: 1 addition & 1 deletion userspace/shell/src/nc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn recv(fd: usize) -> bool {

fn start(fd: usize) {
unsafe {
SENT = 0;
(&raw mut SENT).write(0);
}

let mut read_fds: FdSet = FdSet::new();
Expand Down
4 changes: 4 additions & 0 deletions userspace/syscall-user/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ pub fn ioctl(fd: usize, cmd: usize, arg: usize) -> SyscallResult {
unsafe { syscall3(SYS_IOCTL, fd, cmd, arg) }
}

pub fn yield_execution() -> SyscallResult {
unsafe { syscall0(SYS_YIELD) }
}

pub fn sigaction(
sig: usize,
mut sigaction: Option<&mut syscall_defs::signal::SigAction>,
Expand Down

0 comments on commit 089307a

Please sign in to comment.