I don’t think “volatile memory” is a concept in the C spec. According to (the last draft of) the C11 spec, the volatile
modifier on a variable/memory location means that expressions involving it are evaluated strictly as required by the abstract machine. That is, as far as I can tell, it just controls uses of the variable, not the actual storage itself.
This is exactly what {read,write}_volatile
offer, explicit control over the uses of a variable. These allow/are designed for building an abstraction like, which is fairly similar to the volatile
qualifier:
struct VolatileCell<T> {
x: T
}
impl<T> VolatileCell<T> {
fn get(&self) -> T {
ptr::read_volatile(&self.x)
}
fn set(&mut self, x: T) {
ptr::write_volatile(&mut self.x, x)
}
}
(In practice it may make more sense to have this use UnsafeCell
internally and &self
for set
.)