read/write volatile
read/write volatile
Posted Jan 9, 2026 17:19 UTC (Fri) by mb (subscriber, #50428)Parent article: READ_ONCE(), WRITE_ONCE(), but not for Rust
I experimented with read/write volatile on AVR, where it would be greatly beneficial to use them for certain inter-thread (interrupt) communication.
Just in the same way every AVR C program uses volatile "atomic" variables to do this.
https://github.com/mbuesch/avr-atomic
However, I came to the conclusion that using read/write volatile for inter-thread (interrupt) communication is unsound in Rust.
AVR hardware is not capable of doing a non-atomic read/write for byte sized objects. So the hardware is fine for all of the relevant cases.
But the read/write volatile documentation was pretty clear to me that the Rust virtual machine considers concurrent volatile accesses unsound and is free to optimize it to bits.
https://doc.rust-lang.org/std/ptr/fn.read_volatile.html
> an operation is volatile has no bearing whatsoever on questions involving concurrent accesses from multiple threads
> Volatile accesses behave exactly like non-atomic accesses in that regard.
> This access is still not considered atomic, and as such it cannot be used for inter-thread synchronization.
My implementation worked fine and the generated assembly code was perfect.
However, I changed it to a less optimal inline asm implementation just because I think the Rust documentation considers concurrent read/write volatile without additional synchronization to be unsound.
(Yes, I know there is AtomicXX and no it's not efficient for reasons... And yes, I should fix the compiler's atomic intrinsics instead of working around the problem... I know :)
