`

【翻译】(25)ANDROID ATOMICS OPERATIONS

 
阅读更多

-----------------

英文文档见android-ndk-r7b的documentation.html

属于Android Native Development Kit (NDK)的一部分

http://developer.android.com/sdk/ndk/index.html

翻译仅个人见解

-----------------

 

ANDROID ATOMICS OPERATIONS

 

Android原子操作

 

The problem:

 

问题:

============

 

If your application native code was generated with a NDK release older than r7b and uses any of the following functions defined in the <sys/atomics.h> header:

 

如果你的应用程序原生代码被一个比r7b旧的NDK发布版生成并且使用以下定义在<sys/atomics.h>头文件中的任意函数:

 

  __atomic_cmpxchg (注:猜测:原子地比较old和_new,如果不相等的话加载_new到ptr)

  __atomic_inc (注:原子地把ptr指向的内容加一)

  __atomic_dec (注:原子地把ptr指向的内容减一)

  __atomic_swap (注:猜测:原子地把_new加载进ptr)

 

(注:摘录自sys/atomics.h:

extern int __atomic_cmpxchg(int old, int _new, volatile int *ptr);

extern int __atomic_swap(int _new, volatile int *ptr);

extern int __atomic_dec(volatile int *ptr);

extern int __atomic_inc(volatile int *ptr);

 

Then the corresponding machine code is not guaranteed to work properly on some multi-core Android ARM-based devices (x86 ones are not affected).

 

那么相应的机器代码不保证在一些多核Android的基于ARM设备上工作正常(x86的设备不受影响)。

 

The solution:

 

解决方案:

=============

 

The <sys/atomics.h> header has been updated in NDK r7b. Simply recompiling your _unmodified_ sources with this version of the NDK should be enough to completely eliminate the problem.

 

<sys/atomics.h>头文件已经在NDK r7b中更新。简单地用这个版本的NDK来重新编译你的“未被修改”的代码应该足够地完全消除该问题。

 

If you can't use NDK r7b or later for some reason, read the section below.

 

如果你因为一些原因不能使用NDK r7b或更新,请阅读以下的章节。

 

More details:

 

更多细节:

=============

 

The main issue is that the implementation of these functions, as provided by the C library, did not provide any associated memory barriers. This is by design, because the platform code that uses them does insert explicit barriers around these operations.

 

主要问题是这些函数的实现,正如C库提供的那样,不提供任何被关联的内存壁垒。这是设计造成的,因为使用它们的平台代码在这些操作的周围插入显式的壁垒。

 

The functions were only exposed through the NDK by mistake, they were not supposed to be used from applications. Any application code that use them without inserting its own barriers may experiment incorrect behaviour, which can result in bugs that are very hard to reproduce and diagnose.

 

这些函数只通过NDK错误地被暴露,它们不被暴露以从应用程序中被使用。任意使用它们但不插入它自己的壁垒的应用程序代码可能试验出不正确的行为,它可以导致非常难重现和诊断的缺陷。

 

Not all multi-core devices are affected though. Certain OEMs enforce a policy where all threads of a single process are forced to run on the same core. In this case, the bug cannot occur, unless you're directly accessing shared memory between two processes.

 

不过不是所有多核设备都被影响。某些OEM(注:Original Equipment Manufacturer,原始设备制造商)实施一种策略,单一进程的所有线程被强制运行在相同的核上。在这种情况下,缺陷不能发生,除非你正在直接地在两个进程之间访问共享内存。

 

The problem is only likely to be seen on devices running Android 3.0 to Android 4.1. The C library implementation in 4.1 has been updated to provide full memory barriers as well. This ensures existing native code keeps working correctly on this platform and future ones, even if they were not recompiled.

 

该问题可能只在运行Android 3.0到Android 4.1的设备上被发现。在4.1中的C库实现也已经被升级以提供完全内存壁垒。这确保现存原生代码保持在这个平台和未来的平台上工作正常,即便它们不被重新编译。

 

We still strongly recommend recompiling your native code to ensure you'll never have to debug this issue (life is short). In the case where this would not be possible (e.g. you're using an older version of the NDK for some reason, or a custom build system / toolchain), we recommend stopping from using these functions entirely. Very fortunately, GCC provides handy intrinsics functions that work with very reasonable performance and always provide a full barrier.

 

我们仍然强烈建议重新编译你的原生代码以确保你将不必调试此问题(人生是短暂的)。在它不可能做到的情况下(例如,你因为一些原因正在使用一个旧版本的NDK,或者一个自定义构建系统/工具链),我们建议完全地禁止使用这些函数。非常幸运地,GCC提供便利的本质函数,它可以用非常合理的性能来工作并且总是提供一个完全的壁垒。

 

  __sync_fetch_and_add         instead of __atomic_inc          __sync_fetch_and_add取代__atomic_inc

  __sync_fetch_and_sub         instead of __atomic_sub          __sync_fetch_and_sub取代__atomic_sub

  __sync_val_compare_and_swap  instead of __atomic_cmpxchg      __sync_val_compare_and_swap取代__atomic_cmpxchg

 

See the content of platforms/android-3/arch-arm/usr/include/sys/atomics.h to see how these can be used.

 

参见platforms/android-3/arch-arm/usr/include/sys/atomics.h的内容以查看它们可以如何被使用。

 

See the following URL for more GCC-related information:

 

参见以下URL以获得更多GCC相关信息:

 

  http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics