Skip to content

Commit 64b4027

Browse files
Tropix126max-niederman
authored andcommitted
properly flush serial on abort
1 parent 480361a commit 64b4027

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

library/std/src/sys/pal/vexos/mod.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ pub mod stdio;
1818
pub mod thread;
1919
pub mod time;
2020

21-
use crate::{arch::asm, ptr::{self, addr_of_mut}};
21+
use crate::arch::asm;
2222
use crate::hash::{DefaultHasher, Hasher};
23+
use crate::ptr::{self, addr_of_mut};
24+
use crate::time::{Duration, Instant};
2325

2426
#[cfg(not(test))]
2527
#[no_mangle]
@@ -53,12 +55,8 @@ pub unsafe extern "C" fn _start() -> ! {
5355
#[link_section = ".code_signature"]
5456
#[linkage = "weak"]
5557
#[used]
56-
static CODE_SIGNATURE: vex_sdk::vcodesig = vex_sdk::vcodesig {
57-
magic: u32::from_le_bytes(*b"XVX5"),
58-
r#type: 0,
59-
owner: 2,
60-
options: 0,
61-
};
58+
static CODE_SIGNATURE: vex_sdk::vcodesig =
59+
vex_sdk::vcodesig { magic: u32::from_le_bytes(*b"XVX5"), r#type: 0, owner: 2, options: 0 };
6260

6361
// This function is needed by the panic runtime. The symbol is named in
6462
// pre-link args for the target specification, so keep that in sync.
@@ -94,8 +92,19 @@ pub fn decode_error_kind(_code: i32) -> crate::io::ErrorKind {
9492
}
9593

9694
pub fn abort_internal() -> ! {
95+
let exit_time = Instant::now();
96+
const FLUSH_TIMEOUT: Duration = Duration::from_millis(15);
97+
9798
unsafe {
98-
vex_sdk::vexTasksRun();
99+
// Force the serial buffer to flush
100+
while exit_time.elapsed() < FLUSH_TIMEOUT {
101+
// If the buffer has been fully flushed, exit the loop
102+
if vex_sdk::vexSerialWriteFree(stdio::STDIO_CHANNEL) == (stdio::STDOUT_BUF_SIZE as i32)
103+
{
104+
break;
105+
}
106+
vex_sdk::vexTasksRun();
107+
}
99108
vex_sdk::vexSystemExitRequest();
100109
}
101110

@@ -107,9 +116,7 @@ pub fn abort_internal() -> ! {
107116
fn hash_time() -> u64 {
108117
let mut hasher = DefaultHasher::new();
109118
// The closest we can get to a random number is the time since program start
110-
let time = unsafe {
111-
vex_sdk::vexSystemHighResTimeGet()
112-
};
119+
let time = unsafe { vex_sdk::vexSystemHighResTimeGet() };
113120
hasher.write_u64(time);
114121
hasher.finish()
115122
}

library/std/src/sys/pal/vexos/stdio.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub struct Stdin(());
44
pub struct Stdout(());
55
pub struct Stderr(());
66

7-
const STDIO_CHANNEL: u32 = 1;
7+
pub const STDIO_CHANNEL: u32 = 1;
88

99
impl Stdin {
1010
pub const fn new() -> Stdin {
@@ -81,7 +81,7 @@ impl io::Write for Stderr {
8181
}
8282

8383
pub const STDIN_BUF_SIZE: usize = 4096;
84-
const STDOUT_BUF_SIZE: usize = 2048;
84+
pub const STDOUT_BUF_SIZE: usize = 2048;
8585

8686
pub fn is_ebadf(_err: &io::Error) -> bool {
8787
true

0 commit comments

Comments
 (0)