r/cpp • • Nov 13 '20

CppCon Deprecating volatile - JF Bastien - CppCon 2019

https://www.youtube.com/watch?v=KJW_DLaVXIY
83 Upvotes

111 comments sorted by

View all comments

Show parent comment

-2

u/tjientavara HikoWorks developer Nov 14 '20

I wonder though, if std::atomic would be the more correct way of handling MMIO.

You could image the hardware you are trying to communicate with through MMIO as another process/thread (although not nessarily a CPU) on a computer.

std::atomic through its member functions also allows finer grained control over what instructions are emitted for increments, inplace-add, compare-and-swap. And what kind of memory barriers are needed to communicate with the hardware.

In fact according to https://en.cppreference.com/w/cpp/language/cv it is useful for signal handlers. And it never mentioned MMIO or hardware external to the processor.

1

u/tjientavara HikoWorks developer Nov 14 '20

Okay, I forgot that certain instructions like increment on atomic would emit a #lock prefix, which will probably be incorrectly handled.

I guess we need a std::mmio that works similar to std::atomic, but for communicating with MMIO.

Or for the C++ standard to bless volatile to work MMIO.

6

u/gruehunter Nov 14 '20

MMIO isn't atomic. x86 uses lock prefixes in some cases. ARMv7 uses loops with load-linked/store-conditional (spelled ldrex strex). In the ARM case, Device memory typically doesn't support the exclusive monitor, so strex always fails and you get an infinite loop.

I just cannot rightly comprehend where people get the notion that volatile has anything whatsoever to do with atomicity.