2 ms·
> The CPU itself may reorder things. > Generally you will need assembly code to avoid the bug. What do you mean by this? Compiler outputs binary code the same
by viraptor 9y ago
> The CPU itself may reorder things.
> Generally you will need assembly code to avoid the bug.
What do you mean by this? Compiler outputs binary code the same way assembler does. If the CPU reorders your instructions, it will happen in both cases.
If there are some specific instructions which create memory/reordering barriers, they can be expressed in higher level languages as well.
- monocasa 9y agoA lot of times they aren't barriers, but metadata on the address map. ARM sticks it in the page tables, and x86 sticks it in the MTRRs. PowerPC does treat it like a memory barrier instruction (eieio - Enforce Inorder Execution of I/O), but that's a stronger guarantee than you'd want for all volatile accesses.
- burfog 9y agoYou can not express the needed instructions in standard C or C++ code. Some compilers may have intrinsics. The inline assembly for gcc on PowerPC might be: __asm__ __volatile__("eieio":::"memory"); Normally the CPU will freely reorder loads and stores that go to different addresses. It is not necessarily reordering instructions; the reordering happens on the way to the memory bus. That special instruction helps. It will ensure that previous accesses to IO memory will be done before any that follow. It also has a similar effect in RAM, but sadly not between RAM and IO memory. For that, which might be needed for a DMA engine, you'll also need the "sync" instruction.
- viraptor 9y agoSorry, autocorrect, can't fix it anymore. I wanted to write exposed, not expressed. And yes, either via inline ASM or intrinsics.
- BenFrantzDale 9y ago“You can not express the needed instructions in standard C or C++ code.“ As of 2011, you can. In C++ it’s with std::atomic. http://en.cppreference.com/w/cpp/atomic/atomic http://en.cppreference.com/w/cpp/atomic/atomic C11 offers comparable tools, I believe.
- burfog 9y agoNo, that is only good enough for threads. The compiler is not required to insert special instructions like "eieio" and is even free to optimize things in ways that would cause misbehavior of hardware. For example, consider two values written to the same location. The compiler is free to optimize out the first one because no valid threaded program can depend upon seeing that value briefly appear.
- BenFrantzDale 9y agoIf I understand you and the standard correctly, no: the default behavior is `memory_order_seq_cst` "On weakly-ordered systems (ARM, Itanium, PowerPC), special CPU load or memory fence instructions have to be used." http://en.cppreference.com/w/cpp/atomic/memory_order http://en.cppreference.com/w/cpp/atomic/memory_order I'm not 100% sure what you mean by your second paragraph. I think you are saying that repeated writes to a `std::atomic` can be optimized out, which sounds like it is true since `std::atomic` is not `volatile`. https://stackoverflow.com/questions/45960387/why-dont-compilers-merge-redundant-stdatomic-writes https://stackoverflow.com/questions/45960387/why-dont-compil...