On a ARM Cortex M 32 bit, under C, I need to maintain a 64 bit counter. I want to avoid a race condition like:
start: count = 0x00000000 ffffffffthreadA: increment count_low count = 0x00000000 00000000threadB: increment count_low count = 0x00000000 00000001threadA: if (!count_low) increment count_high count = 0x00000000 00000001threadB: if (!count_low) increment count_high count = 0x00000000 00000001
It's a simple counter which will be used only two ways: 1. Read val and 2. Increment.
In ARM assembly, this can be done without introducing any locks, via LDREX
and STREX
. However, I'd like to avoid assembly and use C.
Is the best way to do this simply to use atomic_uint_least64_t
? Will that work?
When I try making my counter atomic_uint_least64_t
, I get linker errors undefined reference to
__atomic_load_8', and when I try adding
-latomic, I get
cannot find "-latomic:" No such file or directory`.
I'm using arm-none-eabi-gcc
on Linux to cross compile for Cortex M.