ARM atomic operations without Visual Studio

Asked

Viewed 89 times

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 in the MSDN section ARM Intrinsics, 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 replaced by the Assembly instructions lock cmpxchg and lock xadd, since she has no such instructions.

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

1 answer

2


After a lot of 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 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 the material on SMP, the safest/fastest solution to implement synchronization mechanisms on Android (in C/C++) is using the library POSIX Threads or Pthreads, whose documentation is here.

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

During the research, I also found what are the functions that 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 topic of Google Groups, all of them run smoothly with the Android NDK compiler.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.