Skip to content

Commit 1a63b39

Browse files
Rollup merge of #162257 - vexide:lewisfm/sync-model, r=joboet
Revise the VEXos synchronization model This PR fixes some compilation errors in `std::sync` under VEXos by switching to a simple spinlock-based model that properly handles the "multithreading" provided by this platform through user interrupt handlers and documenting exactly how and how not `std::sync` may be used on this target. Spinlocks are used because they are very simple and the OS on this target is not aware of threading by default and has no documented builtin futex or mutex API we can use. I don't expect this platform to have more than 1 thread blocking on access for a mutex at once (since there is only 1 thread that's allowed to block per the updated target docs). Previously Rust programs running on VEXos were only allowed a single execution context and user ISRs were not allowed to interact with std code whatsoever. After these changes, ISRs are allowed to partially interact with std and `std::sync` can be used to synchronize shared resources between the main thread and interrupts. Using stack-switching schedulers on this platform remains unsound; this is now written explicitly in the target documentation.
2 parents e3c3417 + b024d66 commit 1a63b39

6 files changed

Lines changed: 105 additions & 30 deletions

File tree

‎library/std/src/sys/sync/condvar/no_threads.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ use crate::sys::sync::Mutex;
22
use crate::thread::sleep;
33
use crate::time::Duration;
44

5-
#[cfg(target_has_threads)]
5+
#[cfg(all(
6+
target_has_threads,
7+
// VEXos is only "target_has_threads" because it allows user interrupt handlers which preempt
8+
// the main thread, but they are forbidden from accessing sync primitives (the same as UNIX
9+
// signal handlers).
10+
not(target_os = "vexos"),
11+
))]
612
compile_error!("Using no_threads implementation on a target with threads");
713

814
pub struct Condvar {}

‎library/std/src/sys/sync/mutex/no_threads.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
use crate::cell::Cell;
22

3-
#[cfg(target_has_threads)]
3+
#[cfg(all(
4+
target_has_threads,
5+
// VEXos is only "target_has_threads" because it allows user interrupt handlers which preempt
6+
// the main thread, but they are forbidden from accessing sync primitives (the same as UNIX
7+
// signal handlers).
8+
not(target_os = "vexos"),
9+
))]
410
compile_error!("Using no_threads implementation on a target with threads");
511

612
pub struct Mutex {

‎library/std/src/sys/sync/once/no_threads.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ use crate::cell::Cell;
22
use crate::sync as public;
33
use crate::sync::once::OnceExclusiveState;
44

5-
#[cfg(target_has_threads)]
5+
#[cfg(all(
6+
target_has_threads,
7+
// VEXos is only "target_has_threads" because it allows user interrupt handlers which preempt
8+
// the main thread, but they are forbidden from accessing sync primitives (the same as UNIX
9+
// signal handlers).
10+
not(target_os = "vexos"),
11+
))]
612
compile_error!("Using no_threads implementation on a target with threads");
713

814
pub struct Once {

‎library/std/src/sys/sync/rwlock/no_threads.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
use crate::cell::Cell;
22

3-
#[cfg(target_has_threads)]
3+
#[cfg(all(
4+
target_has_threads,
5+
// VEXos is only "target_has_threads" because it allows user interrupt handlers which preempt
6+
// the main thread, but they are forbidden from accessing sync primitives (the same as UNIX
7+
// signal handlers).
8+
not(target_os = "vexos"),
9+
))]
410
compile_error!("Using no_threads implementation on a target with threads");
511

612
pub struct RwLock {

‎library/std/src/sys/thread_local/no_threads.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ use crate::cell::{Cell, UnsafeCell};
55
use crate::mem::MaybeUninit;
66
use crate::ptr;
77

8-
#[cfg(target_has_threads)]
8+
#[cfg(all(
9+
target_has_threads,
10+
// VEXos is only "target_has_threads" because it allows user interrupt handlers which preempt
11+
// the main thread, but they are forbidden from accessing TLS (like UNIX signal handlers).
12+
// Since there is only one context that's allowed to access thread locals, it's sound to use the
13+
// no_threads implementation.
14+
not(target_os = "vexos"),
15+
))]
916
compile_error!("Using no_threads implementation on a target with threads");
1017

1118
#[doc(hidden)]

‎src/doc/rustc/src/platform-support/thumbv7a-vex-v5.md‎

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,6 @@ This target is cross-compiled. Dynamic linking is unsupported.
2323

2424
`#![no_std]` crates can be built using `build-std` to build `core` and `panic_abort` and optionally `alloc`. Unwinding panics are not yet supported on this target.
2525

26-
`std` has only partial support due to platform limitations. Notably:
27-
28-
- `std::process` and `std::net` are unimplemented. `std::thread` only supports sleeping and yielding, as this is a single-threaded environment.
29-
- `std::time` has full support for `Instant`, but no support for `SystemTime`.
30-
- `std::io` has full support for `stdin`/`stdout`/`stderr`. `stdout` and `stderr` both write to USB channel 1 on this platform and are not differentiated.
31-
- `std::fs` has limited support for reading or writing to files. The following features are unsupported:
32-
- All directory operations (including `mkdir` and `readdir`), although reading directories is possible through [third-party crates](https://docs.rs/vex-sdk/latest/vex_sdk/file/fn.vexFileDirectoryGet.html)
33-
- Deleting files and directories
34-
- File metadata other than file size and type (that is, file vs. directory)
35-
- Opening files with an uncommon combination of open options, such as read + write at the same time.
36-
The supported modes for opening files are in read-only mode, append mode, or write mode (with or without truncation).
37-
- A global allocator implemented on top of `dlmalloc` is provided.
38-
- Modules that do not need to interact with the OS beyond allocation, such as `std::collections`, `std::hash`, `std::future`, `std::sync`, etc., are fully supported.
39-
- Random number generation and hashing is insecure, as there is no reliable source of entropy on this platform.
40-
41-
When compiling for this target, the "C" calling convention maps to AAPCS with VFP registers (hard float ABI) and the "system" calling convention maps to AAPCS without VFP registers (softfp ABI).
42-
43-
This target generates binaries in the ELF format that may be uploaded to the brain with external tools.
44-
45-
### Platform SDKs
46-
47-
To use most platform-specific APIs, users must configure a supporting runtime SDK for `libstd` to link against. Official *VEXcode* SDKs from VEX can be downloaded and linked via the [`vex-sdk-vexcode`](https://crates.io/crates/vex-sdk-vexcode) crate, but they have a restrictive redistribution policy that might not be suitable for all projects. The suggested SDK for open-source projects is the community-supported [`vex-sdk-jumptable`](https://crates.io/crates/vex-sdk-jumptable) crate. SDK implementations are generally thin wrappers over system calls, so projects should not expect to see significant differences in behavior depending on which SDK they use.
48-
49-
Libraries may access symbols from the active VEX SDK without depending on a specific implementation by using the [`vex-sdk`](https://crates.io/crates/vex-sdk) crate.
50-
5126
## Building the target
5227

5328
You can build Rust with support for this target by adding it to the `target` list in `bootstrap.toml`, and then running `./x build --target thumbv7a-vex-v5 compiler`.
@@ -100,6 +75,75 @@ fn main() {
10075
}
10176
```
10277

78+
## Environment
79+
80+
The `main` function is entered on a Zynq 7000's CPU1 in the *System* processor mode in *Secure* state. Programs or runtimes should periodically call `std::thread::yield_now` to flush buffers and fetch the latest peripheral state.
81+
82+
Developers writing programs for this target should use a high-level runtime such as [vexide](https://vexide.dev) or a lower-level system access crate such as [`vex-sdk`](https://crates.io/crates/vex-sdk) to access peripherals such as motors and sensors.
83+
84+
When compiling for this target, the "C" calling convention maps to AAPCS with VFP registers (hard float ABI) and the "system" calling convention maps to AAPCS without VFP registers (softfp ABI).
85+
86+
This target generates binaries in the ELF format that may be uploaded to the brain with external tools.
87+
88+
See Also: [VEX V5 Environment](https://internals.vexide.dev/technical/environment).
89+
90+
### Platform SDKs
91+
92+
To use most platform-specific APIs, users must configure a supporting runtime SDK for `libstd` to link against. Official *VEXcode* SDKs from VEX can be downloaded and linked via the [`vex-sdk-vexcode`](https://crates.io/crates/vex-sdk-vexcode) crate, but they have a restrictive redistribution policy that might not be suitable for all projects. The suggested SDK for open-source projects is the community-supported [`vex-sdk-jumptable`](https://crates.io/crates/vex-sdk-jumptable) crate. SDK implementations are generally thin wrappers over system calls, so projects should not expect to see significant differences in behavior depending on which SDK they use.
93+
94+
Libraries may access symbols from the active VEX SDK without depending on a specific implementation by using the [`vex-sdk`](https://crates.io/crates/vex-sdk) crate.
95+
96+
### Standard Library Support
97+
98+
`std` has only partial support due to platform limitations. Notably:
99+
100+
- `std::process` and `std::net` are unimplemented. `std::thread` only supports sleeping and yielding.
101+
- `std::time` has full support for `Instant`, but no support for `SystemTime`.
102+
- `std::io` has full support for `stdin`/`stdout`/`stderr`. `stdout` and `stderr` both write to USB channel 1 on this platform and are not differentiated.
103+
- `std::fs` has limited support for reading or writing to files. The following features are unsupported:
104+
- All directory operations (including `mkdir` and `readdir`), although reading directories is possible through [third-party crates](https://docs.rs/vex-sdk/latest/vex_sdk/file/fn.vexFileDirectoryGet.html)
105+
- Deleting files and directories
106+
- File metadata other than file size and type (that is, file vs. directory)
107+
- Opening files with an uncommon combination of open options, such as read + write at the same time.
108+
The supported modes for opening files are in read-only mode, append mode, or write mode (with or without truncation).
109+
- A global allocator implemented on top of `dlmalloc` is provided.
110+
- `std::sync`'s synchronization primitives assume single-threaded execution.
111+
- Modules that do not need to interact with the OS beyond allocation, such as `std::collections`, `std::hash`, `std::future`, etc., are fully supported.
112+
- Random number generation and hashing is insecure, as there is no reliable source of entropy on this platform.
113+
114+
### Thread Safety and Synchronization
115+
116+
*This section only applies to programs that use the `std` crate.*
117+
118+
When executing on this target, the `std` crate only supports one Rust execution context plus any user-installed Armv7-A exception handlers (configured using the `VBAR` register), provided they properly synchronize access to shared resources. `std` on this target is not aware of any execution inside a task scheduler, so `thread_local` globals will always have the same identity.
119+
120+
#### VEXos Task Scheduler
121+
122+
This target begins execution outside of the context of the builtin [VEXos task scheduler](https://internals.vexide.dev/sdk/tasks). Spawning VEXos Full Tasks through VEX's undocumented scheduler API (e.g. using the `vexTaskAdd` function) or using any other stack-switching scheduler (including FreeRTOS) is strictly forbidden on this target and will result in undefined behavior. Rust programs may assume that `vexTasksRun` and `std::thread::yield_now` will never switch stacks.
123+
124+
Code running inside a VEXos Simple Task context (for example, inside a user touchscreen callback) is forbidden from ticking the task scheduler reentrantly. Thus, it is invalid for task code to:
125+
126+
- call `vexTasksRun`, `thread::yield_now`, `thread::sleep`, `thread::park`, or derivatives,
127+
- or access standard I/O streams via `std::io`.
128+
129+
#### Exception Handlers
130+
131+
Interrupt Service Routines and other ARM exception handlers are the only form of preemptive concurrency allowed on this target. Exception handlers may be installed on this target using unsafe code and are conceptually treated akin to UNIX signal handlers.
132+
133+
Exception handlers in programs using `std` *must not*:
134+
135+
- use the data structures in `std::sync` to synchronize access to shared state,
136+
- use `thread_local!` values whatsoever,
137+
- panic, which indirectly accesses thread locals,
138+
- access standard I/O streams via `std::io`,
139+
- access the filesystem,
140+
- allocate/deallocate using the `System` allocator,
141+
- or call `std::thread::current`, which allocates using the `System` allocator,
142+
143+
since these features are not synchronized.
144+
145+
However, exception handlers *may* use types originating from `core::sync` and `alloc::sync`, including atomics, to synchronize state.
146+
103147
## Testing
104148

105149
Binaries built for this target can be run in an emulator (such as [vex-v5-qemu](https://github.com/vexide/vex-v5-qemu)), or uploaded to a physical device over a USB serial connection.

0 commit comments

Comments
 (0)