Can DMB instructions be safely omitted in ARM Cortex M4

后端 未结 1 1000
萌比男神i
萌比男神i 2021-01-16 02:01

I am going through the assembly generated by GCC for an ARM Cortex M4, and noticed that atomic_compare_exchange_weak gets two DMB instructions inse

相关标签:
1条回答
  • 2021-01-16 02:20

    I'm not aware of whether Cortex M4 can be used in a multi-cpu/multi-core configuration, but in general:

    1. Memory barriers are never necessary (can always be omitted) in single-core systems.
    2. Memory barriers are always necessary (can never be omitted) in multi-core systems where threads/processes operating on the same memory may be running on different cores.

    Presence or lack of reordering memory writes at the hardware level is irrelevant.

    Of course I would expect the DMB instruction to be essentially free on chips that don't support SMP, so I'm not sure why you'd want to try to hack it out.

    Please note that, based on the question's referencing the code the compiler produces for atomic intrinsics, I'm assuming the context is for synchronization of atomics to make them match the high-level specification, not other uses like IO barriers for MMIO, and the above "never" should not be read as applying to this (unrelated) use (though I suspect, for the reasons you already cited, it doesn't apply to Cortex M4).

    0 讨论(0)
提交回复
热议问题