pub unsafe fn read_volatile<O, T>(src: BitPtr<Const, O, T>) -> bool where
    O: BitOrder,
    T: BitStore
Expand description

Performs a volatile read of the bit from src.

Volatile operations are intended to act on I/O memory, and are guaranteed to not be elided or reördered by the compiler across other volatile operations.

Original

ptr::read_volatile

Notes

Rust does not curretnly have a rigorously and formally defined memory model, so the precise semantics of what “volatile” means here is subject to change over time. That being said, the semantics will almost always end up pretty similar to C11’s definition of volatile.

The compiler shouldn’t change the relative order or number of volatile memory operations.

Safety

Behavior is undefined if any of the following conditions are violated:

  • dst must be valid for reads
  • dst must point to a properly initialized value of type T
  • no other pointer must race dst to view or modify the referent location unless T is capable of ensuring race safety.

Just like in C, whether an operation is volatile has no bearing whatsoëver on questions involving concurrent access from multiple threads. Volatile accesses behave exactly like non-atomic accesses in that regard. In particular, a race between a read_volatile and any write operation on the same location is undefined behavior.

This is true even for atomic types! This instruction is an ordinary load that the compiler will not remove. It is not an atomic instruction.

Examples

use bitvec::prelude::*;

let data = 4u8;
let ptr = BitPtr::<_, Lsb0, _>::from_ref(&data);
unsafe {
  assert!(bitvec::ptr::read_volatile(ptr.add(2)));
}