| commit | 2fac3f4accadca19d4a1cb75dcbd323983d87e89 | [log] [tgz] |
|---|---|---|
| author | Android Build Coastguard Worker <[email protected]> | Thu Aug 08 01:15:22 2024 +0000 |
| committer | Android Build Coastguard Worker <[email protected]> | Thu Aug 08 01:15:22 2024 +0000 |
| tree | a5df3daa1f1135d9222597e2890951db3cce7467 | |
| parent | 2c4e36602c2941d50d0d7d65fc3d656cd77c444c [diff] | |
| parent | 6428dd86e2a510e9a35314fedcfeb114ecc2a198 [diff] |
Snap for 12199801 from 6428dd86e2a510e9a35314fedcfeb114ecc2a198 to 25D4-release Change-Id: If73930d70a8d3e4416651c896f8a0ae3af15f564
Safe per-CPU core mutable state on no_std platforms through exception masking.
This crate provides two main wrapper types: PerCore to provide an instance of a value per CPU core, where each core can access only its instance, and ExceptionLock to guard a value so that it can only be accessed while exceptions are masked. These may be combined with RefCell to provide safe per-core mutable state.
ExceptionLock may also be combined with a spinlock-based mutex (such as one provided by the spin crate) to avoid deadlocks when accessing global mutable state from exception handlers.
use core::cell::RefCell; use percore::{exception_free, Cores, ExceptionLock, PerCore}; /// The total number of CPU cores in the target system. const CORE_COUNT: usize = 2; struct CoresImpl; unsafe impl Cores for CoresImpl { fn core_index() -> usize { todo!("Return the index of the current CPU core, 0 or 1") } } struct CoreState { // Your per-core mutable state goes here... foo: u32, } const EMPTY_CORE_STATE: ExceptionLock<RefCell<CoreState>> = ExceptionLock::new(RefCell::new(CoreState { foo: 0 })); static CORE_STATE: PerCore<ExceptionLock<RefCell<CoreState>>, CoresImpl, CORE_COUNT> = PerCore::new([EMPTY_CORE_STATE; CORE_COUNT]); fn main() { // Mask exceptions while accessing mutable state. exception_free(|token| { // `token` proves that interrupts are masked, so we can safely access per-core mutable // state. CORE_STATE.get().borrow_mut(token).foo = 42; }); }
This is not an officially supported Google product.
Currently only aarch64 is fully supported. The crate will build for other architectures, but you'll need to provide your own implementation of the exception_free function. Patches are welcome to add support for other architectures.
Licensed under either of
at your option.
If you want to contribute to the project, see details of how we accept contributions.