Crate interrupt_ref_cell
source ·Expand description
A RefCell
for sharing data with interrupt handlers or signal handlers on the same thread.
InterruptRefCell
is just like RefCell
, but disables interrupts during borrows.
See std::cell
for a module-level description of cells.
Synchronization
This cell synchronizes the current thread with itself via a compiler_fence
.
A compiler fence is sufficient for sharing a !Sync
type, such as RefCell
, with an interrupt handler on the same thread.
Caveats
Interrupts are disabled on a best-effort basis.
Holding a reference does not guarantee that interrupts are disabled. Dropping shared references in the wrong order might enable interrupts prematurely. Similarly, you can just enable interrupts manually while holding a reference.
Examples
use interrupt_ref_cell::{InterruptRefCell, LocalKeyExt};
thread_local! {
static X: InterruptRefCell<Vec<i32>> = InterruptRefCell::new(Vec::new());
}
fn interrupt_handler() {
X.with_borrow_mut(|v| v.push(1));
}
X.with_borrow(|v| {
// Raise an interrupt
raise_interrupt();
assert_eq!(*v, vec![]);
});
// The interrupt handler runs
X.with_borrow(|v| assert_eq!(*v, vec![1]));
Structs
- Wraps a borrowed reference to a value in a
InterruptRefCell
box. A wrapper type for an immutably borrowed value from aInterruptRefCell<T>
. - A mutable memory location with dynamically checked borrow rules
- A wrapper type for a mutably borrowed value from a
InterruptRefCell<T>
.