[riot-notifications] [RIOT-OS/RIOT] cpu/atmega_common: RTT and RTC support (#8842)
notifications at github.com
Sat Apr 20 12:06:06 CEST 2019
@ZetaR60: I happily admit that my knowledge about the technical details of `volatile` are limited. But I am aware of the consensus among C developers regarding the usage of `volatile` and we should stick to that, unless we have a very, very good reason to not do so.
`volatile` is used by C developers for three things:
1. To force the compiler to actually perform the reads and writes to the variable that is hardware mapped.
2. To get atomic accesses
3. To enforce ordering of memory access
The conses is that 1. is totally valid, as this is exactly what `volatile` was introduced for historically. 2. does not work, as this has nothing to do with atomic accesses. This use of `volatile` is simply a bug. And the conses is to not use it for 3. for various reasons, which are:
- The degree by which ordering is guaranteed when using `volatile` accesses depends on who you ask. This is a very bad feature if compilers give different guarantees on that
- It is totally useless on systems that have out of order execution or multiple processor (cores). This is not yet relevant for RIOT, but it seems to be a matter of time until out of order processors hit the embedded market. Also RIOT is already running on the ESP32 (using only one core), so RIOT might gain support for running on more than one processor (core) soon.
The Linux kernel guys are explicitly forbidding the use of `volatile` variables except for memory directly linked to hardware (and some “stupid legacy” code) https://www.kernel.org/doc/html/latest/process/volatile-considered-harmful.html Unlike me (as you pointed out) , those guys have the required expertise to draw the conclusion that `volatile` is considered harmful.
> Memory barriers should not be used as a replacement for volatile. Volatile prevents only reordering with other volatile accesses, but memory barriers prevent all forms of optimization from passing the memory barrier.
This is not true. `volatile` prevents the reordering by the compiler, which is does work on in-order single-core single-threaded CPUs. Memory barrier work on every system.
Summing up: You are using `volatile` for something it is historically not intended to be used. The consensus among C programmer is to not do what you are doing. Please provide reasonable and strong technical justification for that, or stop using `volatile` there.
Some links to read:
- [Arch D. Robison - Volatile: Almost Useless for Multi-Threaded Programming](https://web.archive.org/web/20181124154026/https://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming)
- [John Regehr - Nine ways to break your systems code using volatile](https://blog.regehr.org/archives/28)
- [Jonathan Corbet et al. - Why the “volatile” type class should not be used](https://www.kernel.org/doc/html/latest/process/volatile-considered-harmful.html)
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the notifications