Atomic operations in ARM without Visual Studio

3
When programming in C / C ++ on the x86 and x86-64 (AMD64) architecture and using the Microsoft compiler that comes with Visual Studio, there are two intrinsic functions to perform atomic operations, _InterlockedCompareExchange32 and _InterlockedIncrement32 , which are implemented through the assembly instructions lock cmpxchg and lock xadd , respectively.

Looking at the documentation on MSDN, in the ARM Intrinsics section, I read that the Microsoft compiler also has these two intrinsic functions for the ARM architecture.

Obviously, these functions in the ARM architecture will not be overridden by the assembly instructions lock cmpxchg and lock xadd , since it does not have such instructions.

How to implement the _InterlockedCompareExchange32 and _InterlockedIncrement32 functions (can it be in C or in assembly), so that it is compileable by the ARM compiler that comes with the Android NDK?

    
asked by anonymous 20.06.2014 / 17:31

1 answer

2

After much research, I found a material made available by Google (impossible to reproduce here, because it is huge) called Symmetric Multi-Processor Primer for Android .

In several parts of the material there are comparisons between the x86 and ARM architectures, timely explaining some possible difference between their behaviors (mainly because the ARM architecture does not have some instructions that the x86 architecture has).

Due to a number of factors, all explained in SMP stuff , the most secure / to implement synchronization mechanisms on Android (in C / C ++) is to use the POSIX Threads library or Phthreads whose documentation is located here .

I mentioned this in the answer, because although I did not say in the question, my final goal with the _InterlockedCompareExchange32 and _InterlockedIncrement32 functions was to implement very trivial synchronization mechanisms.

During the search, I also found which functions compile in the Android NDK and perform the atomic functions analogous to the ones I was looking for: __sync_val_compare_and_swap and __sync_fetch_and_add , respectively.

They are built-in functions of the GCC compiler, and are explained in the GCC documentation itself: Built-in functions for atomic memory access .

According to this Google Groups topic , they all work seamlessly with the Android NDK compiler.

    
27.06.2014 / 15:55