Skip to content

Commit 1441c9d

Browse files
committed
vexos: finish bringing modules up to date
1 parent 5f16d47 commit 1441c9d

File tree

7 files changed

+80
-47
lines changed

7 files changed

+80
-47
lines changed

library/std/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ path = "../windows_targets"
6262
rand = { version = "0.9.0", default-features = false, features = ["alloc"] }
6363
rand_xorshift = "0.4.0"
6464

65-
[target.'cfg(any(all(target_family = "wasm", target_os = "unknown"), target_os = "xous", all(target_vendor = "fortanix", target_env = "sgx")))'.dependencies]
65+
[target.'cfg(any(all(target_family = "wasm", target_os = "unknown"), target_os = "xous", target_os = "vexos", all(target_vendor = "fortanix", target_env = "sgx")))'.dependencies]
6666
dlmalloc = { version = "0.2.10", features = ['rustc-dep-of-std'] }
6767

6868
[target.x86_64-fortanix-unknown-sgx.dependencies]

library/std/src/sys/alloc/vexos.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ mod lock {
5353
if LOCKED.swap(1, Acquire) == 0 {
5454
return DropLock;
5555
}
56-
crate::os::xous::ffi::do_yield();
5756
}
5857
}
5958

library/std/src/sys/fs/vexos.rs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::ffi::{CString, OsString};
22
use crate::fmt;
3+
use crate::fs::TryLockError;
34
use crate::hash::Hash;
45
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
56
use crate::path::{Path, PathBuf};
67
use crate::sys::time::SystemTime;
7-
use crate::sys::unsupported;
8+
use crate::sys::{unsupported, unsupported_err};
89

910
#[derive(Debug)]
1011
struct FileDesc(*mut vex_sdk::FIL);
@@ -19,12 +20,8 @@ pub struct FileAttr {
1920
is_dir: bool,
2021
}
2122

22-
#[derive(Debug)]
23-
pub struct ReadDir {
24-
entries: Vec<DirEntry>,
25-
}
23+
pub struct ReadDir(!);
2624

27-
#[derive(Debug)]
2825
pub struct DirEntry {
2926
path: PathBuf,
3027
}
@@ -39,18 +36,18 @@ pub struct OpenOptions {
3936
}
4037

4138
#[derive(Copy, Clone, Debug, Default)]
42-
pub struct FileTimes(());
39+
pub struct FileTimes {}
4340

44-
#[derive(Clone, Debug, PartialEq, Eq)]
45-
pub struct FilePermissions;
41+
#[derive(Clone, PartialEq, Eq, Debug)]
42+
pub struct FilePermissions {}
4643

47-
#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash)]
44+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
4845
pub struct FileType {
4946
is_dir: bool,
5047
}
5148

5249
#[derive(Debug)]
53-
pub struct DirBuilder(());
50+
pub struct DirBuilder {}
5451

5552
impl FileAttr {
5653
/// Creates a FileAttr by getting data from an opened file.
@@ -79,9 +76,8 @@ impl FileAttr {
7976
let mut opts = OpenOptions::new();
8077
opts.read(true);
8178
let file = File::open(path, &opts)?;
82-
let fd = file.fd.0;
8379

84-
Self::from_fd(fd)
80+
Self::from_fd(file.fd.0)
8581
}
8682
}
8783

@@ -90,7 +86,7 @@ impl FileAttr {
9086
}
9187

9288
pub fn perm(&self) -> FilePermissions {
93-
FilePermissions
89+
FilePermissions {}
9490
}
9591

9692
pub fn file_type(&self) -> FileType {
@@ -135,15 +131,22 @@ impl FileType {
135131
}
136132

137133
pub fn is_symlink(&self) -> bool {
134+
// No symlinks in vexos; entries are either files or directories.
138135
false
139136
}
140137
}
141138

139+
impl fmt::Debug for ReadDir {
140+
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
141+
self.0
142+
}
143+
}
144+
142145
impl Iterator for ReadDir {
143146
type Item = io::Result<DirEntry>;
144147

145148
fn next(&mut self) -> Option<io::Result<DirEntry>> {
146-
self.entries.pop().map(Ok)
149+
self.0
147150
}
148151
}
149152

@@ -191,6 +194,7 @@ impl OpenOptions {
191194
}
192195

193196
impl File {
197+
// TODO
194198
pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
195199
// Mount sdcard volume as FAT filesystem
196200
map_fresult(unsafe { vex_sdk::vexFileMountSD() })?;
@@ -265,12 +269,12 @@ impl File {
265269
unsupported()
266270
}
267271

268-
pub fn try_lock(&self) -> io::Result<bool> {
269-
unsupported()
272+
pub fn try_lock(&self) -> Result<(), TryLockError> {
273+
Err(TryLockError::Error(unsupported_err()))
270274
}
271275

272-
pub fn try_lock_shared(&self) -> io::Result<bool> {
273-
unsupported()
276+
pub fn try_lock_shared(&self) -> Result<(), TryLockError> {
277+
Err(TryLockError::Error(unsupported_err()))
274278
}
275279

276280
pub fn unlock(&self) -> io::Result<()> {
@@ -333,13 +337,17 @@ impl File {
333337
Ok(())
334338
}
335339

336-
fn tell(&self) -> io::Result<u64> {
340+
pub fn tell(&self) -> io::Result<u64> {
337341
let position = unsafe { vex_sdk::vexFileTell(self.fd.0) };
338342
position.try_into().map_err(|_| {
339343
io::Error::new(io::ErrorKind::InvalidData, "Failed to get current location in file")
340344
})
341345
}
342346

347+
pub fn size(&self) -> Option<io::Result<u64>> {
348+
None
349+
}
350+
343351
pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
344352
const SEEK_SET: i32 = 0;
345353
const SEEK_CUR: i32 = 1;
@@ -412,7 +420,7 @@ impl File {
412420

413421
impl DirBuilder {
414422
pub fn new() -> DirBuilder {
415-
DirBuilder(())
423+
DirBuilder {}
416424
}
417425

418426
pub fn mkdir(&self, _p: &Path) -> io::Result<()> {

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ global_asm!(
2121
);
2222

2323
#[cfg(not(test))]
24-
#[no_mangle]
24+
#[unsafe(no_mangle)]
2525
pub unsafe extern "C" fn _start() -> ! {
26-
extern "C" {
26+
unsafe extern "C" {
2727
static mut __bss_start: u8;
2828
static mut __bss_end: u8;
2929

@@ -40,6 +40,7 @@ pub unsafe extern "C" fn _start() -> ! {
4040

4141
main();
4242

43+
cleanup();
4344
abort_internal()
4445
}
4546

@@ -49,7 +50,22 @@ pub unsafe fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {}
4950

5051
// SAFETY: must be called only once during runtime cleanup.
5152
// NOTE: this is not guaranteed to run, for example when the program aborts.
52-
pub unsafe fn cleanup() {}
53+
pub unsafe fn cleanup() {
54+
let exit_time = Instant::now();
55+
const FLUSH_TIMEOUT: Duration = Duration::from_millis(15);
56+
const STDIO_CHANNEL: u32 = 1;
57+
58+
// Force the serial buffer to flush
59+
while exit_time.elapsed() < FLUSH_TIMEOUT {
60+
vex_sdk::vexTasksRun();
61+
62+
// If the buffer has been fully flushed, exit the loop
63+
if vex_sdk::vexSerialWriteFree(STDIO_CHANNEL) == (crate::sys::stdio::STDOUT_BUF_SIZE as i32)
64+
{
65+
break;
66+
}
67+
}
68+
}
5369

5470
pub fn unsupported<T>() -> crate::io::Result<T> {
5571
Err(unsupported_err())
@@ -68,21 +84,7 @@ pub fn decode_error_kind(_code: i32) -> crate::io::ErrorKind {
6884
}
6985

7086
pub fn abort_internal() -> ! {
71-
let exit_time = Instant::now();
72-
const FLUSH_TIMEOUT: Duration = Duration::from_millis(15);
73-
7487
unsafe {
75-
// Force the serial buffer to flush
76-
while exit_time.elapsed() < FLUSH_TIMEOUT {
77-
vex_sdk::vexTasksRun();
78-
79-
// If the buffer has been fully flushed, exit the loop
80-
if vex_sdk::vexSerialWriteFree(stdio::STDIO_CHANNEL) == (stdio::STDOUT_BUF_SIZE as i32)
81-
{
82-
break;
83-
}
84-
}
85-
8688
vex_sdk::vexSystemExitRequest();
8789

8890
loop {

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ use crate::io;
44
use crate::num::NonZero;
55
use crate::time::{Duration, Instant};
66

7-
pub struct Thread(!);
8-
97
pub const DEFAULT_MIN_STACK_SIZE: usize = 64 * 1024;
108

9+
pub struct Thread(!);
10+
1111
impl Thread {
1212
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
13-
pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>) -> io::Result<Thread> {
13+
pub unsafe fn new(
14+
_stack: usize,
15+
_name: Option<&str>,
16+
_p: Box<dyn FnOnce()>,
17+
) -> io::Result<Thread> {
1418
unsupported()
1519
}
1620

@@ -34,11 +38,23 @@ impl Thread {
3438
}
3539
}
3640

41+
pub fn sleep_until(deadline: Instant) {
42+
let now = Instant::now();
43+
44+
if let Some(delay) = deadline.checked_duration_since(now) {
45+
Self::sleep(delay);
46+
}
47+
}
48+
3749
pub fn join(self) {
3850
self.0
3951
}
4052
}
4153

54+
pub(crate) fn current_os_id() -> Option<u64> {
55+
None
56+
}
57+
4258
pub fn available_parallelism() -> io::Result<NonZero<usize>> {
4359
unsupported()
4460
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,22 @@ impl SystemTime {
3232
panic!("system time not implemented on this platform")
3333
}
3434

35-
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
36-
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
35+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
36+
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
37+
// FIXME: ok_or_else with const closures
38+
match self.0.checked_sub(other.0) {
39+
Some(duration) => Ok(duration),
40+
None => Err(other.0 - self.0),
41+
}
3742
}
3843

39-
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
44+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
45+
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
4046
Some(SystemTime(self.0.checked_add(*other)?))
4147
}
4248

43-
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
49+
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
50+
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
4451
Some(SystemTime(self.0.checked_sub(*other)?))
4552
}
4653
}

library/std/src/sys/thread_local/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pub(crate) mod guard {
9999
target_os = "uefi",
100100
target_os = "zkvm",
101101
target_os = "trusty",
102+
target_os = "vexos",
102103
) => {
103104
pub(crate) fn enable() {
104105
// FIXME: Right now there is no concept of "thread exit" on

0 commit comments

Comments
 (0)